标签:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 18187 | Accepted: 8401 |
Description
Input
Output
Sample Input
0 4 1 1 2 3 2 0 0 2 2 1 1 1 2 1 1 2 -1 2 1 1 2 3 3
Sample Output
3 4
Source
明天不编程,今天多做几道补上~
题目大意:给一个矩阵,初始全赋0,
1,a,b,c表示f[a][b]+=c;
2,a,b,c,d表示输出Σf[i][j] (i=a…c , j=b…d)
本蒟蒻刚学树状数组就给我来个二维树状数组的题真的好吗……= =
注意:他给的矩阵操作是从下标0开始的,所以每次要将x++,y++;
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdlib> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <iostream> 9 #include "algorithm" 10 using namespace std; 11 typedef long long LL; 12 const int MAX=1050; 13 int n; 14 int f[MAX][MAX]; 15 void add(int x,int y,int z){ 16 int i,j; 17 for (i=x;i<MAX;i+=(i&-i)) 18 for (j=y;j<MAX;j+=(j&-j)) 19 f[i][j]+=z; 20 } 21 int search(int x,int y){ 22 int ans(0); 23 int i,j; 24 for (i=x;i>0;i-=(i&-i)) 25 for (j=y;j>0;j-=(j&-j)) 26 ans+=f[i][j]; 27 return ans; 28 } 29 int main(){ 30 freopen ("mobile.in","r",stdin); 31 freopen ("mobile.out","w",stdout); 32 int i,j; 33 int a,b,c,d,e; 34 memset(f,0,sizeof(f)); 35 scanf("%d%d",&i,&n); 36 while (1) 37 {scanf("%d",&a); 38 if (a==3) 39 break; 40 if (a==1) 41 {scanf("%d%d%d",&b,&c,&d); 42 b++, c++; 43 add(b,c,d); 44 } 45 if (a==2) 46 {scanf("%d%d%d%d",&b,&c,&d,&e); 47 b++, c++, d++, e++; 48 printf("%d\n",search(d,e)-search(b-1,e)-search(d,c-1)+search(b-1,c-1)); 49 } 50 } 51 return 0; 52 }
POJ-1195 Mobile Phones(二维树状数组)
标签:
原文地址:http://www.cnblogs.com/Michaelzzn/p/5727713.html