HDU

来源:互联网 发布:免费的会计软件 编辑:程序博客网 时间:2024/05/29 18:19
Can you answer these queries?

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


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



题目大意:有N艘军舰排成一排,每艘军舰有一个耐力值。每个样例都会有M次操作,当T = 1时输出L~R区间内所有军舰的耐力值的总和,当T = 0时将L~R区间内所有军舰的耐力值开平方。
题目思路:题目一看起来好像挺吓人的,100000个值,进行100000次更新和查询,而且开平方的话是无法用懒惰标记来进行区间更新的。可是仔细想想,每个值的最大值为2^63次方,我们只要开7次平方,它就变为1了,当一个区间内所有的值都为1的时候我们就不需要再去更新它了,只需要每次单点更新那些值不为1的区间即可。


AC代码如下:


#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define FIN freopen("in.txt","r",stdin)#define fuck(x) cout<<'['<<x<<']'<<endl#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;typedef long long LL;typedef pair<int,int>pii;const int MX = 1e5 + 10;LL sum[MX<<2];void Push_Up(int rt){    sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void build(int l,int r,int rt){    sum[rt] = 0;    if(l == r){        scanf("%I64d",&sum[rt]);        return;    }    int m = (l + r) >> 1;    build(lson);    build(rson);    Push_Up(rt);}void update(int L,int R,int l,int r,int rt){    if(l == r){        sum[rt] = sqrt(sum[rt]);//单点更新;        return;    }    if(L <= l && r <= R && sum[rt] == r-l+1) return;//sum[rt] = r - l + 1时证明整个区间的值都为1了,那么我们就可以跳过这个区间的更新;    int m = (l + r) >> 1;    if(L <= m) update(L,R,lson);    if(R > m) update(L,R,rson);    Push_Up(rt);}LL query(int L,int R,int l,int r,int rt){    if(L <= l && r <= R)        return sum[rt];    int m = (l + r) >> 1;    LL res = 0;    if(L <= m) res += query(L,R,lson);    if(R > m) res += query(L,R,rson);    return res;}int n,q;int main(){    // FIN;    int cas = 1;    while(~scanf("%d",&n)){        build(1,n,1);        scanf("%d",&q);        printf("Case #%d:\n",cas++);        while(q--){            int t,x,y;            scanf("%d%d%d",&t,&x,&y);            int xx = min(x,y);            int yy = max(x,y);//本题略坑爹的一点,它并没有告诉你x和y的大小关系,所以得小处理下;            if(t == 1)                printf("%I64d\n",query(xx,yy,1,n,1));            else{                update(xx,yy,1,n,1);            }        }        puts("");//注意下格式;    }    return 0;}


原创粉丝点击