HDU 4027 Can you answer these queries?(线段树)【The 36th ACM/ICPC Asia Regional 上海站网赛】

来源:互联网 发布:单片机直流电机调速 编辑:程序博客网 时间:2024/06/05 17:48

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 13662 Accepted Submission(s): 3137

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
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8

Sample Output
Case #1:
19
7
6

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

一道比较经典的线段树题目。
给你n个数字。
然后有两种操作:
0.a,b
把[a,b]区间的每个数字变为它开方之后的数字。
1.a,b
求出[a,b]这个区间的所有数字之和。
然后可以把他变为单点修改的线段树来操作,然后再加上剪枝优化就OK了。
下面是AC代码:

#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<stack>using namespace std;#define ll long longstruct node{    int left,right;    ll val;}c[400005];void build_tree(int l,int r,int root){    c[root].left=l;    c[root].right=r;    if(c[root].left==c[root].right)    {        scanf("%I64d",&c[root].val);        return ;    }    int mid=(c[root].left+c[root].right)/2;    build_tree(l,mid,root*2);    build_tree(mid+1,r,root*2+1);    c[root].val=c[root*2].val+c[root*2+1].val;}void update_tree(int l,int r,int root){    if(c[root].val==(c[root].right-c[root].left+1))//剪枝优化,此时不需要再操作    {        return ;    }    if(c[root].left==c[root].right)//单点修改    {        c[root].val=sqrt(c[root].val);        return ;    }    int mid=(c[root].left+c[root].right)/2;    if(l>mid)    {        update_tree(l,r,root*2+1);    }    else if(r<=mid)    {        update_tree(l,r,root*2);    }    else    {        update_tree(l,mid,root*2);        update_tree(mid+1,r,root*2+1);    }    c[root].val=c[root*2].val+c[root*2+1].val;}ll search_tree(int l,int r ,int root){    if(c[root].left==l&&c[root].right==r)    {        return c[root].val;    }    int mid=(c[root].left+c[root].right)/2;    if(l>mid)    {        return search_tree(l,r,root*2+1);    }    else  if(r<=mid)    {        return search_tree(l,r,root*2);    }    else    {        return search_tree(l,mid,root*2)+search_tree(mid+1,r,root*2+1);    }}int main(){    int n,m,iCase=0;    while(~scanf("%d",&n))    {        iCase++;        printf("Case #%d:\n",iCase);        memset(c,0,sizeof(c));        build_tree(1,n,1);        scanf("%d",&m);        int op,a,b;        for(int i=0;i<m;i++)        {            scanf("%d",&op);            if(op==0)            {                scanf("%d%d",&a,&b);                update_tree(min(a,b),max(a,b),1);//这块需要注意            }            else if(op==1)            {                scanf("%d%d",&a,&b);                printf("%I64d\n",search_tree(min(a,b),max(a,b),1));//这块需要注意,坑了好几发            }        }        printf("\n");//格式需要注意    }    return 0;}
0 0
原创粉丝点击