标签:... com cube iostream bsp logs nod har 模板
2 8 10Sample 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 }
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 EndSample 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 }
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 }
Input
Output
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 }
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 }
标签:... com cube iostream bsp logs nod har 模板
原文地址:http://www.cnblogs.com/xingkongyihao/p/7158614.html