hdu 1166 敌兵布阵

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

可以直接暴力过:

View Code
 1 #include <stdio.h>
2 int main()
3 {
4 int a[50005],n,j,p,q,t,i,sum;
5 char ch[10];
6 scanf("%d",&t);
7 for(j = 1;j <= t;j ++)
8 {
9 scanf("%d",&n);
10 for(i = 1;i <= n;i ++)
11 scanf("%d",&a[i]);
12 printf("Case %d:\n",j);
13 while(scanf("%s",ch) != EOF)
14 {
15 if(ch[0] == 'E') break;
16 scanf("%d %d",&p,&q);
17 if(ch[0] == 'A')
18 a[p] += q;
19 if(ch[0] == 'S')
20 a[p] -= q;
21 if(ch[0] == 'Q')
22 {
23 sum = 0;
24 for(i = p;i <= q;i ++)
25 sum += a[i];
26 printf("%d\n",sum);
27 }
28 }
29 }
30 return 0;
31 }
也可以用树状数组过:
#include <stdio.h> 
#include <memory.h>
#define lowbit(x) ((x)&(-x)) 
int a[50005],b[50005];

void update(int x,int y,int n) 
{ 
    while(x <= n) 
    {
        a[x] += y;
        x += lowbit(x);
    } 
}
int getsum(int x)
{
    int ret = 0;
    while(x)
    {
        ret += a[x];
        x -= lowbit(x);
    }
    return ret;
}
int main()
{
    int t,i,n,p,q,j,tmp;
    char ch[10];
    scanf("%d",&t);
    for(j = 1;j <= t;j ++)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        for(i = 1;i <= n;i ++)
        {
            scanf("%d",&tmp);
            b[i] = b[i-1] + tmp;
            a[i] = b[i] - b[i-lowbit(i)];
        }
        printf("Case %d:\n",j);
        while(scanf("%s",ch) != EOF)
        {
            if(ch[0] == 'E') break;
            scanf("%d %d",&p,&q);
            if(ch[0] == 'A')
            update(p,q,n);
            if(ch[0] == 'S')
            update(p,-q,n);
            if(ch[0] == 'Q')
            printf("%d\n",getsum(q) - getsum(p-1));
        }
        }
    return 0;
}
还可以用线段树过:
View Code
 1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 using namespace std;
5 struct node
6 {
7 int l,r,sum;
8 node *lc,*rc;
9 void fun()
10 {
11 sum = l = r = 0;
12 lc = rc = NULL;
13 }
14 }*tree;
15 node *build(int a,int b,node *tree)
16 {
17 node *root = new node;
18 root->fun();
19 root->l=a,root->r=b;
20 tree=root;
21 if(a != b)
22 {
23 tree->lc=build(a,(a+b)>>1,tree->lc);
24 tree->rc=build(1+(a+b)>>1,b,tree->rc);
25 }
26 return tree;
27 }
28 void update(int way,int num,node *tree)
29 {
30 tree->sum += num;
31 if(tree->l != tree->r)
32 {
33 if(way<=(tree->l+tree->r)>>1)
34 update(way,num,tree->lc);
35 if(way>(tree->l+tree->r)>>1)
36 update(way,num,tree->rc);
37 }
38 }
39 int query(int a,int b,node *tree)
40 {
41 int sum = 0;
42 if(a<=tree->l && b>=tree->r)
43 sum += tree->sum;
44 else
45 {
46 if(a<=(tree->l+tree->r)>>1)
47 sum += query(a,b,tree->lc);
48 if(b>(tree->l+tree->r)>>1)
49 sum += query(a,b,tree->rc);
50 }
51 return sum;
52 }
53 void release(node *tree)
54 {
55 if(tree->lc) release(tree->lc);
56 if(tree->rc) release(tree->rc);
57 free(tree);
58 }
59 int main()
60 {
61 int t,n,m,p,q,i,j;
62 char ch[10];
63 scanf("%d",&t);
64 for(j = 1;j <= t;j ++)
65 {
66 scanf("%d",&n);
67 tree=build(1,n,tree);
68 for(i = 1;i <= n;i ++)
69 {
70 scanf("%d",&m);
71 update(i,m,tree);
72 }
73 printf("Case %d:\n",j);
74 while(scanf("%s",ch) != EOF && ch[0]!='E')
75 {
76 scanf("%d %d",&p,&q);
77 if(ch[0] == 'A')
78 update(p,q,tree);
79 if(ch[0] == 'S')
80 update(p,-q,tree);
81 if(ch[0] == 'Q')
82 printf("%d\n",query(p,q,tree));
83 }
84 release(tree);
85 }
86 return 0;
87 }

 1 #include <stdio.h>
2 #include <memory.h>
3 #define lowbit(x) ((x)&(-x))
4 int a[50005],b[50005];
5
6 void update(int x,int y,int n)
7 {
8 while(x <= n)
9 {
10 a[x] += y;
11 x += lowbit(x);
12 }
13 }
14 int getsum(int x)
15 {
16 int ret = 0;
17 while(x)
18 {
19 ret += a[x];
20 x -= lowbit(x);
21 }
22 return ret;
23 }
24 int main()
25 {
26 int t,i,n,p,q,j,tmp;
27 char ch[10];
28 scanf("%d",&t);
29 for(j = 1;j <= t;j ++)
30 {
31 memset(a,0,sizeof(a));
32 memset(b,0,sizeof(b));
33 scanf("%d",&n);
34 for(i = 1;i <= n;i ++)
35 {
36 scanf("%d",&tmp);
37 b[i] = b[i-1] + tmp;
38 a[i] = b[i] - b[i-lowbit(i)];
39 }
40 printf("Case %d:\n",j);
41 while(scanf("%s",ch) != EOF)
42 {
43 if(ch[0] == 'E') break;
44 scanf("%d %d",&p,&q);
45 if(ch[0] == 'A')
46 update(p,q,n);
47 if(ch[0] == 'S')
48 update(p,-q,n);
49 if(ch[0] == 'Q')
50 printf("%d\n",getsum(q) - getsum(p-1));
51 }
52 }
53 return 0;
54 }
posted @ 2011-07-15 08:59  zhangteng  阅读(185)  评论(0编辑  收藏  举报