HDU 4027 Can you answer these queries?(线段树单点更新)

来源:互联网 发布:净水器推荐知乎 编辑:程序博客网 时间:2024/06/03 10:45

Can you answer these queries?

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


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 

http://acm.hdu.edu.cn/showproblem.php?pid=4027

http://blog.csdn.net/libin56842/article/details/15028533   大神链接

//真TMD无语
//又写一道 单点更新的线段树
//本来 以为 Soeasy TMD 写出来又 TEL
// 用延迟标记了啊
// 原来这道题 就是 开根号到 1 就不需要在 update下去了


#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>using namespace std;const int L = 100000+10;__int64 sum[L<<2],cnt[L<<2];void add(int i){    sum[i] = sum[2*i]+sum[2*i+1];    cnt[i] = cnt[2*i]&&cnt[2*i+1];}void init(int l,int r,int i){    if(l == r)    {        scanf("%I64d",&sum[i]);        return;    }    int mid = (l+r)>>1;    init(l,mid,2*i);    init(mid+1,r,2*i+1);    add(i);}void insert(int L,int R,int l,int r,int i){    if(l == r)    {        sum[i] = sqrt(sum[i]);//向下取整        if(sum[i]<=1)//如果为0,则标记不能再访问了            cnt[i] = 1;        return ;    }    int mid = (l+r)>>1;    if(L<=mid && !cnt[2*i])//孩子已经破坏了则不需再访问        insert(L,R,l,mid,2*i);    if(R>mid && !cnt[2*i+1])        insert(L,R,mid+1,r,2*i+1);    add(i);}__int64 query(int L,int R,int l,int r,int i)//查询区间和{    if(L<=l && r<=R)        return sum[i];    int mid = (l+r)>>1;    __int64 ans = 0;    if(L<=mid)        ans+=query(L,R,l,mid,2*i);    if(R>mid)        ans+=query(L,R,mid+1,r,2*i+1);    return ans;}int main(){    int n,m,i,j,x,l,r,cas = 1,flag;    while(~scanf("%d",&n))    {        memset(sum,0,sizeof(sum));        memset(cnt,0,sizeof(cnt));        init(1,n,1);        printf("Case #%d:\n",cas++);        scanf("%d",&m);        while(m--)        {            scanf("%d%d%d",&flag,&l,&r);            if(l>r)//这里要注意                swap(l,r);            if(flag)                printf("%I64d\n",query(l,r,1,n,1));            else                insert(l,r,1,n,1);        }        printf("\n");    }    return 0;}






0 0
原创粉丝点击