hdu4027

来源:互联网 发布:mpi编程 编辑:程序博客网 时间:2024/06/06 09:12

Can you answer these queries?

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

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

Recommend
lcy
这个题吧,它说区间开方,我一开始想tag lazy思想来区间更新,但是那个包,中午想了好久好久也没想出来,后来看kuangbin大神的博客才知道,他开方几次之后就成了1,最多开8次,数据大小可以过复杂度,所以直接单点去修改就行了,没用模板,手写,还是有错,我感觉手写才练代码能力,而且对算法的理解更加深

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int MaxN = 5000100;typedef long long LL;struct node{    int l , r;    LL sum;}segTree[MaxN * 4];void Build(int i , int l , int r){    segTree[i].l = l;    segTree[i].r = r;    if(l == r){        scanf("%I64d",&segTree[i].sum);        return;    }    int mid = (l + r) >> 1 ;    Build(i << 1 , l , mid);    Build(i << 1 | 1 , mid + 1 , r);    segTree[i].sum = segTree[i << 1].sum + segTree[i << 1 | 1].sum;}void action(int i , int l , int r){    if(segTree[i].l ==l && segTree[i].r == r && segTree[i].sum == (r - l + 1))//本题精髓,其实开方几次之后就是1,不用再开方了。        return;    if(segTree[i].l == segTree[i].r){        segTree[i].sum = sqrt(segTree[i].sum*1.0);        return;    }    int mid = (segTree[i].l + segTree[i].r) >> 1;    if(r <= mid) action(i << 1 , l , r);    else if(l > mid) action(i << 1 | 1 , l , r);    else{        action(i << 1 , l , mid);        action(i << 1 | 1 , mid + 1 , r);        //segTree[i].sum = segTree[i << 1].sum + segTree[i << 1 | 1].sum;//一开始我就以为在这里就行,因为其他的都是分半,后来才知道,每个点都有两个,如果在这里就内个的话是错的    }    segTree[i].sum = segTree[i << 1].sum + segTree[i << 1 | 1].sum;//每个点都有两个。。。}LL sum(int i , int l , int r){    if(segTree[i].l == l && r == segTree[i].r){        return segTree[i].sum;    }    int mid = (segTree[i].l + segTree[i].r) >> 1;    LL ans = 0;    if(r <= mid) ans = sum(i << 1 , l , r);    else if(l > mid) ans = sum(i << 1 | 1 , l , r);    else{         ans += sum(i << 1 , l , mid);         ans += sum(i << 1 | 1 , mid + 1 , r);         //ans += (segTree[i << 1].sum + segTree[i << 1 | 1].sum);//一开始在这里写了一个,但是有坑,可能儿子节点没更新好,所以是错的    }    return ans;//函数都是有返回值的,用函数返回值做就好了}int main(){    int n , cnt = 1 , m;    while(~scanf("%d",&n)){        Build(1 , 1 , n);        scanf("%d",&m);        printf("Case #%d:\n",cnt++);        while(m--){            int t , x , y;            scanf("%d %d %d", &t , &x , &y);            if(x > y) swap(x , y);            if(t == 0) action(1 , x , y);            else printf("%I64d\n",sum(1 , x , y));        }        printf("\n");    }    return 0;}

人一我百,人十我万,穷尽毕生,只为最强,不要输给wyy

0 0