线段树

题目 A-Curious Robin Hood

Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.

Now each time he can he can do one of the three tasks.

1)                  Give all the money of the ith sack to the poor, leaving the sack empty.

2)                  Add new amount (given in input) in the ith sack.

3)                  Find the total amount of money from ith sack to jth sack.

Since he is not a programmer, he seeks your help.

 Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in the ith sack (0 ≤ i < n).

Each of the next q lines contains a task in one of the following form:

1 i        Give all the money of the ith (0 ≤ i < n) sack to the poor.

2 i v     Add money v (1 ≤ v ≤ 1000) to the ith (0 ≤ i < n) sack.

3 i j      Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).

Output

  For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.

Sample Input

1

5 6

3 2 1 4 5

1 4

2 3 4

3 0 3

1 2

3 0 4

1 1

Sample Output

Case 1:

5

14

1

13

2

 

简单来说就是设置一个集合,集合里有若干的元素,要求你写出三个对集合中元素的操作。本题如果直接暴力模拟会超时,所以使用线段树。

简单的说就是把这个集合的区间用二分的方式一层一层的分直到分到不能分为止,然后查找区间操作不需要每个元素逐个相加,如果线段树

上有一段区间是待查找区间的子集,就可以直接使用,减少程序用时。

本题中 操作一 把一个袋子内的钱清空并且打印袋子内原先有的钱。

          操作二 向第i个袋子里增加v元钱。

    操作三 查找第i个袋子到第j个袋子之间所有袋子内的总钱数。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #define maxn 5000005
  5 using namespace std;
  6 
  7 int sum[maxn];
  8 
  9 void Pushup(int rt)//点更新 及时每一个区间的值
 10 {
 11     sum[rt] = sum[rt*2] + sum[rt*2+1];
 12 }
 13 
 14 void Build (int l, int r, int rt) //构造函数 用来录入数据和构造线段树
 15 { // l是左边界  r是右边界
 16     if(l == r) //递归的最底部
 17     {
 18         scanf("%d",&sum[rt]);
 19     }
 20     else
 21     {
 22         int midd = (l + r)/2; //每次取中间值 把一个区间一分为二 然后构建左儿子和右儿子
 23         Build(l,midd,rt*2);//left son
 24         Build(midd+1,r,rt*2+1);//right  son
 25         Pushup(rt);//点更新
 26     }
 27 }
 28 
 29 void Update(int no,int money,int l,int r,int rt) //更新数据的函数 
 30 { //no是袋子的序号 rt是线段树 每一个儿子的编号
 31     int m = (l + r)/2;
 32     if(no == l && no == r) //递归最低层 
 33     {
 34         sum[rt] = sum[rt] + money;
 35     }
 36     else if ( no >= l && no <= m)//left
 37     {
 38         Update(no,money,l,m,rt*2);
 39         Pushup(rt);
 40     }
 41     else if(no <=r && no >= m+1)//right
 42     {
 43         Update(no,money,m+1,r,rt*2+1);
 44         Pushup(rt);
 45     }
 46 
 47 }
 48 
 49 /*void Makzero(int no, int l, int r, int rt) //错误代码
 50 {
 51     int m = (l+r)/2;
 52     if (no == l && no == m)
 53     {
 54         printf("%d\n",sum[rt]);
 55         sum[rt] = 0;
 56     }
 57     else if(no >=l && no <=m)
 58     {
 59         Makzero(no,l,m,rt*2);
 60     }
 61     else if(no >= m+1 && no <=r)
 62     {
 63         Makzero(no,m+1,r,rt*2+1);
 64     }
 65       Pushup(rt);
 66 }*/
 67 int finum(int L, int R,int l,int r,int rt)//查找函数
 68 {// L R是待查找区间
 69     if (L<=l && r <= R) //如果分出来的区间是待查找区间的子集 则直接加
 70     {
 71         return sum[rt];
 72     }
 73     int m = (l + r) /2;
 74     int ans = 0; 
 75     if (L <= m) //一个不断分的过程 待查找区间的答案是很多小区间的和
 76         ans += finum(L, R, l, m, rt*2);
 77     if (R>m)
 78         ans += finum(L, R, m+1, r, rt*2+1);
 79         //printf("rt=%d sum[rt]=%d ans=%d\n",rt,sum[rt],ans);
 80     return ans;
 81 }
 82 
 83 int main()
 84 {
 85     int casen;
 86     scanf("%d",&casen); 
 87     for(int cas = 1;cas<=casen;cas++)
 88     {
 89         printf("Case %d:\n", cas);
 90         int n, linen;
 91         scanf("%d%d",&n,&linen);
 92         Build(0,n-1,1); //袋子编号是到 n-1
 93         int t, q, mone,a,b;
 94         for(int i=0;i<linen;i++){
 95         scanf("%d",&t);
 96         if(t == 1) //操作一
 97         {
 98             scanf("%d",&q);
 99             int x =  finum(q, q, 0, n-1, 1);
100              printf("%d\n",x);
101              Update(q,-x,0,n-1,1);
102         }
103         else if(t == 2) //
104         {
105             scanf("%d%d",&q,&mone);
106             Update(q,mone,0,n-1,1);
107         }
108         else if(t == 3) //
109         {
110             scanf("%d%d", &a, &b);
111             printf("%d\n", finum(a, b, 0, n-1, 1));
112         }
113 
114         }
115     }
116     return 0;
117 }

 

posted @ 2017-03-05 16:12  码农CHQ  阅读(158)  评论(0编辑  收藏  举报