树状数组

 
 

BST

 
Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until the last level, and we can also find the maximum number by going down the right node. Now you are given some queries as "What are the minimum and maximum numbers in the subtree whose root node is X?" Please try to find answers for there queries. 
Input
In the input, the first line contains an integer N, which represents the number of queries. In the next N lines, each contains a number representing a subtree with root number X (1 <= X <= 2 31 - 1).
Output
There are N lines in total, the i-th of which contains the answer for the i-th query.
Sample Input
2
8
10
Sample Output
1 15
9 11


这个就是树状数组的lowbit原理:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define lowbit(x) (x&(-x))
 5 using namespace std;
 6 
 7 int main(){
 8     int n,x;
 9     scanf("%d",&n);
10     while(n--){
11         scanf("%d",&x);
12         printf("%d %d\n",x-lowbit(x)+1,x+lowbit(x)-1);
13     }
14     return 0;
15 }

 

 

 

敌兵布阵

 
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 
Sample Output
Case 1:
6
33
59




一个树状数组的模板题:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 #define lowbit(x) x&(-x)
 6 #define mem(a,x) memset(a,x,sizeof(a))
 7 const int MAX = 50010;
 8 int c[MAX];
 9 void update(int x, int y){
10     while(x < MAX){
11         c[x] += y;
12         x += lowbit(x);
13     }
14 }
15 
16 int query(int x){
17     int sum = 0;
18     while(x > 0){
19         sum += c[x];
20         x -= lowbit(x);
21     }
22     return sum;
23 }
24 int main(){
25     int t,n,num,k=1;
26     scanf("%d",&t);
27     while(t--){
28         mem(c,0);
29         printf("Case %d:\n",k++);
30         scanf("%d",&n);
31         for(int i = 1; i <= n; i ++){
32             scanf("%d",&num);
33             update(i,num);
34         }
35         char s[10];
36         int x,y;
37         while(scanf("%s",s)!=EOF){
38             if(s[0] == 'E')break;
39             scanf("%d %d",&x,&y);
40             if(s[0] == 'Q'){
41                 printf("%d\n",query(y)-query(x-1));
42             }else if(s[0] == 'A'){
43                 update(x,y);
44             }else update(x,-y);
45         }
46     }
47     return 0;
48 }

 

Color the ball

 

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

Input每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。 
当N = 0,输入结束。
Output每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。Sample Input

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0

Sample Output

1 1 1
3 2 1

因为是线性的,所以可以用数组直接写,当然树状数组也行,类似的在l位置加1,r+1位置-1就行。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 const int MAX = 100010;
 6 int n,a[MAX];
 7 int main(){
 8     while(scanf("%d",&n)&&n){
 9         for(int i = 1; i <= n; i ++){
10             int l, r;
11             scanf("%d %d",&l,&r);
12             a[l]++;
13             a[r+1]--;
14         }
15         int ans = 0;
16         for(int i = 1; i <= n; i ++){
17             ans += a[i];
18             printf("%d%c",ans,(i==n)?'\n':' ');
19         }
20         memset(a,0,sizeof(a));
21     }    
22     return 0;
23 }

Matrix

 

 

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N). 

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
2. Q x y (1 <= x, y <= n) querys A[x, y].

Input

The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case. 

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 

Output

For each querying output one line, which has an integer representing A[x, y]. 

There is a blank line between every two continuous test cases. 

Sample Input

1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

Sample Output

1
0
0
1


在(x1,y1)至(x2,y2)把1变为0,把0变为1,就是树状数组的二维化。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define lowbit(x) x&(-x)
 5 using namespace std;
 6 const int MAX = 1010;
 7 int c[MAX][MAX];
 8 void add(int x,int y,int k){
 9     for(int i = x; i < MAX; i += lowbit(i)){
10         for(int j = y; j < MAX; j += lowbit(j)){
11             c[i][j] += k;
12         }
13     }
14 }
15 int query(int x, int y){
16     int sum = 0;
17     for(int i = x; i > 0; i -= lowbit(i)){
18         for(int j = y; j > 0; j -= lowbit(j)){
19             sum += c[i][j];
20         }
21     }
22     return sum;
23 }
24 int main(){
25     int x,n,t,x1,x2,y1,y2;
26     scanf("%d",&x);
27     while(x--){
28         scanf("%d %d",&n,&t);
29         memset(c,0,sizeof(c));
30         while(t--){
31             char str[10];
32             scanf("%s",str);
33             if(str[0] == 'C'){
34                 scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
35                 add(x1,y1,1);
36                 add(x1,y2+1,-1);
37                 add(x2+1,y1,-1);
38                 add(x2+1,y2+1,-1);
39             }else {
40                 scanf("%d %d",&x1,&y1);
41                 printf("%d\n",query(x1,y1)&1);
42             }
43         }
44         if(x)printf("\n");
45     }
46     return 0;
47 }

 

 Cube

 

Given an N*N*N cube A, whose elements are either 0 or 1. Ai,j,ki,j,k means the number in the i-th row , j-th column and k-th layer. Initially we have Ai,j,ki,j,k = 0 (1 <= i, j, k <= N). 
We define two operations, 1: “Not” operation that we change the Ai,j,ki,j,k=!Ai,j,ki,j,k. that means we change Ai,j,ki,j,k from 0->1,or 1->0. (x1<=i<=x2,y1<=j<=y2,z1<=k<=z2). 
0: “Query” operation we want to get the value of Ai,j,ki,j,k.

InputMulti-cases. 
First line contains N and M, M lines follow indicating the operation below. 
Each operation contains an X, the type of operation. 1: “Not” operation and 0: “Query” operation. 
If X is 1, following x1, y1, z1, x2, y2, z2. 
If X is 0, following x, y, z. 
OutputFor each query output Ax,y,zx,y,z in one line. (1<=n<=100 sum of m <=10000)Sample Input

2 5
1 1 1 1  1 1 1
0 1 1 1
1 1 1 1  2 2 2
0 1 1 1
0 2 2 2

Sample Output

1
0
1


这个就是树状数组的三维化了,在(x1,y1,z1)至(x2,y2,z2)把0变为1,把1变为0.
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define lowbit(x) x&(-x)
 5 using namespace std;
 6 const int MAX = 110;
 7 int a[MAX][MAX][MAX],x1,x2,y1,y2,z1,z2,n,m;
 8 void add(int x, int y, int z,int num){
 9     for(;x < MAX; x+=lowbit(x)){
10         for(int j = y; j < MAX; j += lowbit(j)){
11             for(int k = z; k < MAX; k +=lowbit(k)){
12                 a[x][j][k] += num;
13             }
14         }
15     }
16 }
17 int query(int x, int y, int z){
18     int sum = 0;
19     for(; x > 0; x -= lowbit(x)){
20         for(int j = y; j > 0; j -=lowbit(j)){
21             for(int k = z; k > 0; k -= lowbit(k)){
22                 sum += a[x][j][k];
23             }
24         }
25     }
26     return sum;
27 }
28 int main(){
29     while(scanf("%d %d",&n,&m)!=EOF){
30         memset(a,0,sizeof(a));
31         while(m--){
32             scanf("%d",&x1);
33             if(x1 == 1){
34                 scanf("%d %d %d %d %d %d",&x1,&y1,&z1,&x2,&y2,&z2);
35                 add(x1,y1,z1,1);add(x1,y1,z2+1,1);  
36                 add(x1,y2+1,z1,1);add(x1,y2+1,z2+1,1);  
37                 add(x2+1,y1,z1,1);add(x2+1,y1,z2+1,1);  
38                 add(x2+1,y2+1,z1,1);add(x2+1,y2+1,z2+1,1);  
39             }else if(x1 == 0){
40                 scanf("%d %d %d",&x1,&y1,&z1);
41                 printf("%d\n",query(x1,y1,z1)&1);
42             }
43         }
44         
45     }
46     return 0;
47 }

 

posted @ 2017-07-13 08:47  starry_sky  阅读(194)  评论(0编辑  收藏  举报