HDU 4027 Can you answer these queries?【线段树+区间更新】

来源:互联网 发布:大逃杀网络延迟检测 编辑:程序博客网 时间:2024/05/22 01:51

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027

分析:一个区间包含n个数,q次询问(两个操作):
           0 x y 把区间(x,y)内的没个数开方
           1 x y 查询区间(x,y)的和


我们可以开个标记记录一下当前要更新的区间是否全为1(1开方还是1,属于无用操作,不用更新),不全为1,我们就向下更新子区间。
查询的时候需要注意l>r时,需交换一下,还有最终结果的记录,可能会爆int。(PS:输出的时候要注意格式)

CODE:

#include<stdio.h>#include<algorithm>#include<string.h>#include<iostream>#include<map>#include<queue>#include<stack>#include<math.h>#define INF 0x3f3f3f3f#define lson l,m,root<<1#define rson m+1,r,root<<1|1typedef long long LL;using namespace std;const int maxn=100005;struct node{    int flag;    LL sum;    node()    {        flag=1;    }}tree[maxn<<2];void pushdown(int root){    tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;    tree[root].flag=tree[root<<1].flag|tree[root<<1|1].flag;}void build(int l,int r,int root){    if(l==r)    {        scanf("%lld",&tree[root].sum);        tree[root].flag=1;        if(tree[root].sum<=1)            tree[root].flag=0;        return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushdown(root);}void update(int L,int R,int l,int r,int root){    if(l==r)    {        tree[root].sum=(LL)sqrt(1.0*tree[root].sum);       // cout<<tree[root].sum<<"++ ";        if(tree[root].sum<=1)            tree[root].flag=0;        return;    }    int m=(l+r)>>1;    if(L<=m&&tree[root<<1].flag)        update(L,R,lson);    if(R>m&&tree[root<<1|1].flag)        update(L,R,rson);        pushdown(root);}LL query(int L,int R,int l,int r,int root){    if(L<=l&&r<=R)    {        return tree[root].sum;    }    LL summ=0;    int m=(l+r)>>1;    if(L<=m)        summ+=query(L,R,lson);    if(R>m)        summ+=query(L,R,rson);    return summ;}int main(){    int n,q,T=0;    while(~scanf("%d",&n))    {        printf("Case #%d:\n",++T);        build(1,n,1);        scanf("%d",&q);//        for(int i=1;i<=n;i++)//            cout<<query(i,i,1,n,1)<<"* ";        while(q--)        {            int op,x,y;            scanf("%d%d%d",&op,&x,&y);            if(x>y)                swap(x,y);            if(op==0)                update(x,y,1,n,1);            else            {                //int ans=0;               // for(int i=x;i<=y;i++)                   // ans+=query(i,i,1,n,1);               // printf("%d\n",ans);                printf("%lld\n",query(x,y,1,n,1));            }        }        printf("\n");    }    return 0;}

Can you answer these queries?

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


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
 

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


阅读全文
0 0
原创粉丝点击