题目传送:Mobile phones
AC代码:
/************************************************ > Auther : zzuspy > Mail : [email protected] > Created Time : 2015/8/7 15:24:00 ************************************************/
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1050;
int sum[maxn * 3][maxn * 3];
int S;//区间长度
int X, Y, A;
int L, B, R, T;
void add_x(int rty, int rtx, int L, int R, int x, int a) {//往rty这个区间找x轴的区间更新a
sum[rty][rtx] += a;
if(L == R) {
return;
}
int mid = (L + R) >> 1;
if(x <= mid) {
add_x(rty, rtx << 1, L, mid, x, a);
}
else {
add_x(rty, rtx << 1 | 1, mid + 1, R, x, a);
}
}
void add_y(int rty, int L, int R, int x, int y, int a) {//在y轴上更新a
add_x(rty, 1, 1, S, x, a);
if(L == R) {
return;
}
int mid = (L + R) >> 1;
if(y <= mid) {
add_y(rty << 1, L, mid, x, y, a);
}
else {
add_y(rty << 1 | 1, mid + 1, R, x, y, a);
}
}
int query_x(int rty, int rtx, int L, int R, int x1, int x2) {
if(L >= x1 && R <= x2) {
return sum[rty][rtx];
}
int ret = 0;
int mid = (L + R) >> 1;
if(x1 <= mid) ret += query_x(rty, rtx << 1, L, mid, x1, x2);
if(x2 > mid) ret += query_x(rty, rtx << 1 | 1, mid + 1, R, x1, x2);
return ret;
}
int query_y(int rty, int L, int R, int y1, int y2, int x1, int x2) {
if(L >= y1 && R <= y2) {
return query_x(rty, 1, 1, S, x1, x2);
}
int ret = 0;
int mid = (L + R) >> 1;
if(y1 <= mid) ret += query_y(rty << 1, L, mid, y1, y2, x1, x2);
if(y2 > mid) ret += query_y(rty << 1 | 1, mid + 1, R, y1, y2, x1, x2);
return ret;
}
int main() {
int op;
while(true) {
scanf("%d", &op);
switch(op) {
case 0:
scanf("%d", &S);
memset(sum, 0, sizeof(sum));
break;
case 1:
scanf("%d %d %d", &X, &Y, &A);
add_y(1, 1, S, X + 1, Y + 1, A);
break;
case 2:
scanf("%d %d %d %d", &L, &B, &R, &T);
L ++, B ++, R ++, T++;
printf("%d\n", query_y(1, 1, S, B, T, L, R));
break;
case 3:
return 0;
}
}
return 0;
}