ZOJ 3018 Population【二维线段树四分动态建树】
来源:互联网 发布:java的jvm 编辑:程序博客网 时间:2024/06/04 19:32
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 thatXmin <= x <= Xmax, and Ymin <=y <= Ymax.
Sample Input
I8 20 14 5 110 11 112 10 118 14 1Q8 10 5 158 20 10 14I7 6 110 3 27 2 12 3 210 3 1Q2 20 2 20E
Sample Output
1312
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;}
- ZOJ 3018 Population【二维线段树四分动态建树】
- ZOJ 3018 Population(二维线段树?矩形树?)
- hdu 5367 线段树动态建树
- 【hdu4325】【线段树】Flowers 动态建树
- ZOJ 2859 二维线段树
- ZOJ 1859 Matrix Searching(二维线段树)
- zoj 2859 二维线段树 插点求线
- zoj 2859(二维线段树)
- ZOJ-2859 Matrix Searching (二维线段树)
- ZOJ 2859 二维RMQ 二维线段树(树套树)
- ZOJ 2859 Matrix Searching 二维线段树 || 二维RMQ
- Haskell线段树建树
- 线段树的建树
- [POJ 3468] A Simple Problem with Integers [线段树-动态建树版]
- POJ 2503 动态建树
- trie动态建树与静态建树
- 字典树 静态建树与动态建树
- 【线段树】入门--引用建树
- 微软等面试100题答案V0.2版[第1-20题答案]
- window.opener用法
- window.showModalDialog
- 微软等面试100题答案V0.3版[第21-40题答案]
- VC 透明绘图
- ZOJ 3018 Population【二维线段树四分动态建树】
- c语言 fopen,fgets,fputc,putchar函数的简单使用
- Java SecurityManager相关概念理解
- 外部系统集成BIEE 11g页面同时去掉顶部工具栏和仪表盘菜单
- SACC 2012 第一天
- 使用sql profle进行偷梁换柱的小例子
- Linux常用命令记录
- js基础语法。
- shutil模块