POJ 1195 Mobile phones (二维树状数组或线段树)
偶然发现这题还没A掉............速速解决了.............
树状数组和线段树比较下,线段树是在是太冗余了,以后能用树状数组还是尽量用.........
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <climits>//形如INT_MAX一类的 #define MAX 1111 #define INF 0x7FFFFFFF #define REP(i,s,t) for(int i=(s);i<=(t);++i) #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define mp(a,b) make_pair(a,b) #define L(x) x<<1 #define R(x) x<<1|1 # define eps 1e-5 //#pragma comment(linker, "/STACK:36777216") ///传说中的外挂 using namespace std; int c[MAX][MAX],n; int lowbit(int x) { return x & (-x); } void update(int x1,int y1,int va) { for(int i=x1; i<=n+1; i += lowbit(i)) { for(int j=y1; j<=n+1; j += lowbit(j)) { c[i][j] += va; } } } int query(int x1,int y1) { int sum = 0; for(int i=x1; i>0; i -= lowbit(i)) { for(int j=y1; j>0; j -= lowbit(j)) { sum += c[i][j]; } } return sum; } int main() { int op,x1,y1,x2,y2,va; scanf("%d%d",&op,&n); memset(c,0,sizeof(c)); while(scanf("%d",&op) ) { if(op == 3) break; if(op == 1) { scanf("%d%d%d",&x1,&y1,&va); update(x1+1,y1+1,va); } if(op == 2) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("%d\n",query(x2+1,y2+1) - query(x1,y2+1) + query(x1,y1) - query(x2+1,y1)); } } return 0; }