It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent.
The continent is divided into square regions, each has a center with integer coordinates (x,y). Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region.
Input
Your program must read inputs from the standard input. Since there are up to 32768 different regions and possibly even more queries, please use "scanf" and "printf" instead of "cin" and "cout" to avoid timeout.
The character "I" in a line signals the coming in of new groups of people. In the following lines, each line contains three integers:X, Y, and N, where X and Y (1 <= X, Y <= 20000) are the coordinates of the region's center, and N (1 <=N <= 10000) is the number of people coming in.
The character "Q" in a line signals the query of population. The following lines each contains four numbers:Xmin, Xmax, Ymin, Ymax, where (Xmin,Ymin) and (Xmax, Ymax) are the integer coordinates of the lower left corner and the upper right corner of the rectangle, respectively.
The character "E" signals the end of a test case. Process to the end of file.
Output
For each "Q" case, print to the standard output in a line the population in the given rectangle region. That is, you are supposed to count the number of people living at all the positions (x,y) such that Xmin <= x <= Xmax, and Ymin <=y <= Ymax.
Sample Input
I 8 20 1 4 5 1 10 11 1 12 10 1 18 14 1 Q 8 10 5 15 8 20 10 14 I 7 6 1 10 3 2 7 2 1 2 3 2 10 3 1 Q 2 20 2 20 E
Sample Output
1 3 12
Author: CHEN, Yue
Source: ZOJ Monthly, August 2008
有一个20000*20000的矩阵,初始时矩阵各元素均为0。有两种操作,一是把矩阵中某个元素增加一个数,二是求给定的子矩阵各元素之和。
二维线段树。由于是20000*20000的矩阵,所以不能一开始就建一颗完全四叉树,应该在插入的同时动态的建树,这样可以节省很多内存空间。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 20000;
const int maxm = 110010;
int idx;
struct SegNode {
int l, r, b, t;
int sum;
int ch[4];
};
SegNode seg[maxm];
int newNode(int l, int r, int b, int t)
{
idx++;
seg[idx].l = l;
seg[idx].r = r;
seg[idx].b = b;
seg[idx].t = t;
seg[idx].sum = 0;
// 将孩子节点的id都初始化为0
memset(seg[idx].ch, 0, sizeof(seg[idx].ch));
return idx;
}
void update(int x, int y, int rt, int val)
{
if (seg[rt].l == seg[rt].r && seg[rt].b == seg[rt].t) {
seg[rt].sum += val;
return ;
}
int mx = (seg[rt].l + seg[rt].r) >> 1;
int my = (seg[rt].b + seg[rt].t) >> 1;
if (x <= mx && y <= my) {
if (seg[rt].ch[0] == 0) {
// 节点不存在时就新建,下同。
seg[rt].ch[0] = newNode(seg[rt].l, mx, seg[rt].b, my);
}
update(x, y, seg[rt].ch[0], val);
} else if (x > mx && y <= my) {
if (seg[rt].ch[1] == 0) {
seg[rt].ch[1] = newNode(mx + 1, seg[rt].r, seg[rt].b, my);
}
update(x, y, seg[rt].ch[1], val);
} else if (x <= mx && y > my) {
if (seg[rt].ch[2] == 0) {
seg[rt].ch[2] = newNode(seg[rt].l, mx, my + 1, seg[rt].t);
}
update(x, y, seg[rt].ch[2], val);
} else if (x > mx && y > my) {
if (seg[rt].ch[3] == 0) {
seg[rt].ch[3] = newNode(mx + 1, seg[rt].r, my + 1, seg[rt].t);
}
update(x, y, seg[rt].ch[3], val);
}
seg[rt].sum = 0;
for (int i = 0; i < 4; ++i) {
seg[rt].sum += seg[seg[rt].ch[i]].sum;
}
}
int query(int x1, int y1, int x2, int y2, int rt)
{
if (x1 <= seg[rt].l && x2 >= seg[rt].r && y1 <= seg[rt].b && y2 >= seg[rt].t) {
return seg[rt].sum;
}
int mx = (seg[rt].l + seg[rt].r) >> 1;
int my = (seg[rt].b + seg[rt].t) >> 1;
int ret = 0;
if (x1 <= mx && y1 <= my) {
// 若节点不存在,则该节点所表示的矩形sum为零,可以忽略
if (seg[rt].ch[0] != 0) {
ret += query(x1, y1, x2, y2, seg[rt].ch[0]);
}
}
if (x2 > mx && y1 <= my) {
if (seg[rt].ch[1] != 0) {
ret += query(x1, y1, x2, y2, seg[rt].ch[1]);
}
}
if (x1 <= mx && y2 > my) {
if (seg[rt].ch[2] != 0) {
ret += query(x1, y1, x2, y2, seg[rt].ch[2]);
}
}
if (x2 > mx && y2 > my) {
if (seg[rt].ch[3] != 0) {
ret += query(x1, y1, x2, y2, seg[rt].ch[3]);
}
}
return ret;
}
int str2int(char *s)
{
int len = strlen(s);
int tmp = 0;
for (int i = 0; i < len; ++i) {
tmp = tmp * 10 + (s[i] - '0');
}
return tmp;
}
int main()
{
char op[3];
while (scanf("%s", op) != EOF) {
idx = 0;
newNode(1, maxn, 1, maxn);
seg[0].sum = 0;
char s[20];
int xx, yy, val;
int x1, y1, x2, y2;
while (scanf("%s", s)) {
if(s[0] == 'E') {
break;
}
if (s[0] == 'I') {
op[0] = 'I';
continue;
}
if (s[0] == 'Q') {
op[0] = 'Q';
continue;
}
if (op[0] == 'I') {
xx = str2int(s);
scanf("%d%d", &yy, &val);
update(xx, yy, 1, val);
} else if (op[0] == 'Q') {
x1 = str2int(s);
scanf("%d%d%d", &x2, &y1, &y2);
printf("%d\n", query(x1, y1, x2, y2, 1));
}
}
}
return 0;
}