bzoj4066&2683简单题 K-Dtree

来源:互联网 发布:首付不够怎么买房知乎 编辑:程序博客网 时间:2024/06/10 10:09

4066: 简单题

Time Limit: 50 Sec  Memory Limit: 20 MB
Submit: 3197  Solved: 847
[Submit][Status][Discuss]

Description

你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

 

命令

参数限制

内容

1 x y A

1<=x,y<=N,A是正整数

将格子x,y里的数字加上A

2 x1 y1 x2 y2

1<=x1<= x2<=N

1<=y1<= y2<=N

输出x1 y1 x2 y2这个矩形内的数字和

3

终止程序

Input

输入文件第一行一个正整数N。
接下来每行一个操作。每条命令除第一个数字之外,
均要异或上一次输出的答案last_ans,初始时last_ans=0。

Output

对于每个2操作,输出一个对应的答案。

Sample Input

4
1 2 3 3
2 1 1 3 3
1 1 1 1
2 1 1 0 7
3

Sample Output

3
5

HINT

数据规模和约定

1<=N<=500000,操作数不超过200000个,内存限制20M,保证答案在int范围内并且解码之后数据仍合法。

样例解释见OJ2683


新加数据一组,但未重测----2015.05.24


Source

By wjy1998

KDtree真的伤啊,这道又是调了一个小时的。

这次的原因是,重构树的时候忘记吧左右子树清零了。。GG

这题算是很裸的K-Dtree吧,还是套路,xy二维,记录最大最小xy和数字和信息,然后评估函数就是矩形包含最大最小xy就全部加,如果完全不包含就全部pass,否则进去递归就是了,很裸。

重构树也很裸,就是把每个子树上的节点平衡地重新插入这棵KDtree中,我看hzwer大神(orz)10000次重构一遍,所以我也10000次重构一遍,效果很不赖,比大多数人快:-)

还有bzoj2864和4006一道是强制在线一道是可离线,听说可离线有其他奇奇怪怪的解法,不是很懂,不过KDtree暴力貌似都水过了。。

#include<iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#define maxn 220000#define ls t[p].l#define rs t[p].rusing namespace std;long long read(){    char ch = getchar(); long long x = 0, f = 1;    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}    return x * f;}int D;struct KD_tree {int d[2], mn[2], mx[2], val, l, r;long long sum;bool operator == (const KD_tree &b) const {return d[0] == b.d[0] && d[1] == b.d[1];}bool operator < (const KD_tree &b) const {return d[D] < b.d[D];} }t[maxn], a[maxn], now;int sz, root, m = 10000;void update(int p) {t[p].sum = t[ls].sum + t[rs].sum + t[p].val;for(int i = 0;i <= 1; ++i) {t[p].mn[i] = t[p].mx[i] = t[p].d[i];if(ls) {t[p].mn[i] = min(t[p].mn[i], t[ls].mn[i]);t[p].mx[i] = max(t[p].mx[i], t[ls].mx[i]);}if(rs) {t[p].mn[i] = min(t[p].mn[i], t[rs].mn[i]);t[p].mx[i] = max(t[p].mx[i], t[rs].mx[i]);}}}void KD_add(int &p, int d) {if(!p) {p = ++sz;t[p] = now;return;}if(t[p] == now) {t[p].val += now.val; t[p].sum += now.val; return;}D = d;if(now < t[p]) KD_add(ls, d ^ 1);else KD_add(rs, d ^ 1);update(p);}void KD_rebuild(int &p, int L, int R, int d) {p = L + R >> 1; D = d;nth_element(a + L, a + p, a + R + 1);t[p] = a[p];if(L < p) KD_rebuild(ls, L, p - 1, d ^ 1); else ls = 0;if(R > p) KD_rebuild(rs, p + 1, R, d ^ 1); else rs = 0;update(p);}int x1, y1, x2, y2; long long ans;bool eval_in(int X1, int Y1, int X2, int Y2) {return x1 <= X1 && X2 <= x2 && y1 <= Y1 && Y2 <= y2;}bool eval_out(int X1, int Y1, int X2, int Y2) {return x1 > X2 || x2 < X1 || y1 > Y2 || y2 < Y1;}void KD_query(int p) {if(!p) return;if(eval_in(t[p].mn[0], t[p].mn[1], t[p].mx[0], t[p].mx[1])) ans += t[p].sum;else if(!eval_out(t[p].mn[0], t[p].mn[1], t[p].mx[0], t[p].mx[1])) {if(eval_in(t[p].d[0], t[p].d[1], t[p].d[0], t[p].d[1])) ans += t[p].val;KD_query(ls); KD_query(rs);}}int main(){read(); ans = 0;while(1) {int opt = read();if(opt == 3) break;if(opt == 1) {now.d[0] = now.mn[0] = now.mx[0] = read() ^ ans;now.d[1] = now.mn[1] = now.mx[1] = read() ^ ans;now.val = now.sum = read() ^ ans;KD_add(root, 0);if(sz == m) {for(int i = 1;i <= sz; ++i) a[i] = t[i];KD_rebuild(root, 1, sz, 0);m += 10000;}}if(opt == 2) {x1 = read() ^ ans; y1 = read() ^ ans; x2 = read() ^ ans; y2 = read() ^ ans;ans = 0; KD_query(root);printf("%lld\n", ans);}}return 0;}



原创粉丝点击