码迷,mamicode.com
首页 > 其他好文 > 详细

树状数组Hdu1166

时间:2014-06-07 20:24:57      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   color   

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

标签:c   class   blog   code   a   color   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!