hdu Can you answer these queries? 线段树成段更新

来源:互联网 发布:windows安装mysql服务 编辑:程序博客网 时间:2024/06/11 02:15

Can you answer these queries?

                                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Problem Description
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
 

题目大意:给定100000个数,两种操作,0 i j表示将i j这段的数字都开根号(向下取整),1 i j表示查询i j之间的所有值的和。。。(所有的和都不超过64位)..

思路:仔细观察后,其实我们可以很容易的发现,一个数k(k<=2^63-1)在经过最多6,7次的开平方根后,必然会变成1,而且当1的平方根也是1;

也就是说当一个数为1的时候,我们没有必要对它进行操作和更新;而且一个很大的数仅仅经过6,7次就可以变成1;
所以到这里我们因该就可以形成一个解题的大体思路了:
每当我们要进行更新操作的时候,我们先判断一下这个区间是否有必要进行更新(若全都是1就没有更新的必要了);
判断的方法很简单:就是看该区间的长度和该区间内的总值是否相等

代码:

#include <iostream>#include <stdio.h>#include <math.h>#define N 100010#define LL long longusing namespace std;LL num[N];struct Tree{    int l,r;    LL sum,len;}tree[N*4];void PushUp(int root){    tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;}void PushDown(int l,int r,int root){    if(tree[root].l==tree[root].r)    {        tree[root].sum=(LL)(sqrt(tree[root].sum));        return;    }    int mid=(tree[root].l+tree[root].r)/2;    PushDown(l,mid,root<<1);    PushDown(mid+1,r,root<<1|1);    PushUp(root);}void build(int l,int r,int root){    tree[root].l=l;    tree[root].r=r;    tree[root].len=r-l+1;    tree[root].sum=0;    if(tree[root].l==tree[root].r)    {        tree[root].sum=num[l];        return;    }    int mid=(tree[root].l+tree[root].r)>>1;    build(l,mid,root<<1);    build(mid+1,r,root<<1|1);    PushUp(root);}void update(int l,int r,int root){    if(tree[root].l==l&&tree[root].r==r)    {        if(tree[root].len==tree[root].sum)        return;        PushDown(l,r,root);        return;    }    int mid=(tree[root].l+tree[root].r)>>1;//    if(mid>=l) update(l,mid,root<<1);//    if(mid<r) update(mid+1,r,root<<1|1);     if(mid>=r) update(l,r,root<<1);     else if(mid<l) update(l,r,root<<1|1);     else     {         update(l,mid,root<<1);         update(mid+1,r,root<<1|1);     }    PushUp(root);}LL query(int l,int r,int root){    if(tree[root].l>=l&&tree[root].r<=r)    return tree[root].sum;    int mid=(tree[root].l+tree[root].r)>>1;    LL ret=0;    if(mid>=l)  ret+=query(l,r,root<<1);    if(mid<r)   ret+=query(l,r,root<<1|1);    return ret;//    if(r<=mid) return query(l,r,root<<1);//    else if(l>mid) return query(l,r,root<<1|1);//    else//    {//        return query(l,mid,root<<1)+query(mid+1,r,root<<1|1);//    }}int main(){    int n;    int cas=1;    while(scanf("%d",&n)!=EOF)    {        for(int i=1;i<=n;i++)        scanf("%I64d",&num[i]);        build(1,n,1);        int m;        scanf("%d",&m);        int a,b,c;        printf("Case #%d:\n",cas++);        while(m--)        {            scanf("%d%d%d",&c,&a,&b);            if(a>b) swap(a,b);            if(c==1)            printf("%I64d\n",query(a,b,1));            else            update(a,b,1);        }        cout<<endl;    }    return 0;}


0 0