首页 > 其他 > 详细

树状数组Hdu1166

时间:2014-06-07 20:23:56      阅读:371      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
int c[222222];
int n;
int lowbit(int x)
{
    return x&(-x);
}
 
int sum(int n)
{
    int sum=0;
    while(n>0){
        sum+=c[n];
        n-=lowbit(n);
    }
    return sum;
}
 
void change(int k,int add)
{
    while(k<=n){
        c[k]+=add;
        k+=lowbit(k);
    }
}
int main()
{
    int t;
    char str[100];int pos,add,l,r;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            int ret;scanf("%d",&ret);
            change(i,ret);
        }
        printf("Case %d:\n",i);
        for(;;){
            scanf("%s",str);
            if(strcmp(str,"End")==0)
                break;
            if(strcmp(str,"Add")==0){
                scanf("%d%d",&pos,&add);
                change(pos,add);
            }
            if(strcmp(str,"Query")==0){
                scanf("%d%d",&l,&r);
                printf("%d\n",sum(r)-sum(l-1));
            }
            if(strcmp(str,"Sub")==0){
                cin>>pos>>add;
                change(pos,-add);
            }
        }
    }
    return 0;
}

  

树状数组Hdu1166,布布扣,bubuko.com

树状数组Hdu1166

原文:http://www.cnblogs.com/yigexigua/p/3773715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!