Can you answer these queries? HDU

来源:互联网 发布:火星时代淘宝美工 编辑:程序博客网 时间:2024/05/16 16:06
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.
Input
The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
Sample Output
Case #1:1976
相比较比较裸的线段树的lazy更新是有区别的,比较裸的可以在某一个区间当场就求出该区间的正确答案,而这种题是不行的,必须更新到叶子节点才可以。
然而如果用线段树暴力更新,肯定是超时的。但是如果你对数字的感觉比较好的话,可以发现,就算一个比较大的数字开方到1也是用不到几次的。其实LONGLONG_MAX只需要开6次之后都是1了。这样的话,记住一个区间更新的次数,如果大于6,直接返回区间长度就可以了。
#include<bits/stdc++.h>using namespace std;const int M=1e5+10;long long save[M];struct aa{    int add,cnt;    long long sum[7];} tree[4*M];void downfall(int now)//更新次数向下传递{    tree[now*2+1].add+=tree[now].add;    tree[now*2+1].cnt+=tree[now].add;    tree[now*2+2].add+=tree[now].add;    tree[now*2+2].cnt+=tree[now].add;    tree[now].add=0;}void creat(int now,int l,int r){    tree[now].add=tree[now].cnt=0;    if(l==r)    {        tree[now].sum[0]=save[l];        for(int i=1; i<=6; i++)        {            tree[now].sum[i]=sqrt(tree[now].sum[i-1]);        }        return ;    }    int mid=(l+r)/2;    creat(now*2+1,l,mid);    creat(now*2+2,mid+1,r);}void update(int now,int l,int r,int al,int ar){    if(al>r||ar<l)        return ;    if(al<=l&&ar>=r)    {        tree[now].add++;        tree[now].cnt++;        return ;    }    downfall(now);    int mid=(l+r)/2;    update(now *2+1,l,mid,al,ar);    update(now*2+2,mid+1,r,al,ar);}long long query(int now,int l,int r,int al,int ar){    if(al>r||ar<l)        return 0;    int mid=(l+r)/2;    if(al<=l&&ar>=r)    {        if(tree[now].cnt>=6)//更新次数大于6,返回区间长度        {            return r-l+1;        }        if(l==r)//叶子节点        {            if(tree[now].cnt>=6)                return 1;            else                return tree[now].sum[tree[now].cnt];        }        downfall(now);        return query(now *2+1,l,mid,al,ar)+query(now*2+2,mid+1,r,al,ar);    }    downfall(now);    return query(now *2+1,l,mid,al,ar)+query(now*2+2,mid+1,r,al,ar);}int main(){    int n,m,kase=1;    while(scanf("%d",&n)!=EOF)    {        for(int i=0; i<n; i++)        {            scanf("%I64d",&save[i]);        }        creat(0,0,n-1);        scanf("%d",&m);        printf("Case #%d:\n",kase++);        while(m--)        {            int type,a,b;            scanf("%d%d%d",&type,&a,&b);            if(a>b)//注意坑点                swap(a,b);            if(type)            {                printf("%I64d\n",query(0,0,n-1,a-1,b-1));            }            else            {                update(0,0,n-1,a-1,b-1);            }        }        putchar('\n');    }}

原创粉丝点击