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

HDU1166敌兵布阵【线段树入门】

时间:2018-09-03 20:30:55      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:upd   ons   printf   \n   for   cout   har   col   span   

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn = 50010;
 6 int sum[maxn];
 7 int t,n;
 8 void update(int pos,int val)
 9 {
10     while(pos<=n)
11     {
12         sum[pos] += val;
13         pos += (pos&(-pos));
14     }
15 }
16 int Query(int pos)
17 {
18     int rec = 0;
19     while(pos > 0)
20     {
21         rec += sum[pos];
22         pos -= (pos&(-pos));
23     }
24     return rec;
25 } 
26 int main(void)
27 {
28     int x;
29     scanf("%d",&t);
30     int kase = 0;
31     while(t--)
32     {
33         memset(sum,0,sizeof(sum));
34         cin>>n;
35         for(int i = 1; i <= n; i++)
36         {
37             cin>>x;
38             update(i,x);
39         }
40         char s[10];
41         printf("Case %d:\n",++kase);
42         while(scanf("%s",s))
43         {
44             if(s[0]==E)
45                 break;
46             if(s[0]==A)
47             {
48                 int i,j;
49                 cin>>i>>j;
50                 update(i,j);
51             }
52             else if(s[0]==S)
53             {
54                 int i,j;
55                 cin>>i>>j;
56                 update(i,-j);
57             }
58             else
59             {
60                 int i,j;
61                 cin>>i>>j;
62                 cout<<Query(j)-Query(i-1)<<endl;
63             }
64         }
65     }
66     return 0;
67 }

 

HDU1166敌兵布阵【线段树入门】

标签:upd   ons   printf   \n   for   cout   har   col   span   

原文地址:https://www.cnblogs.com/AC-AC/p/9580584.html

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