BZOJ 4066 简单题 ——KD-Tree套替罪羊树
【题目分析】
直接x,y二维轮番划分,暴力即可。
套上替罪羊,打碎重构,对于时间复杂度有了保证。
写起来好麻烦,重构的技巧很棒!
【代码】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define lim 0.7
#define L t[o].c[0]
#define R t[o].c[1]
#define mid (l+r>>1)
#define F(i,j,k) for (int i=j;i<=k;++i)
struct node{
int d[2],c[2];
int mx[2],mn[2],sum,v,siz,D;
int& operator [] (int x){return d[x];}
}t[maxn],now;
int p[maxn];
int opt=0,D,rt=0,ans=0,tot=0,cnt;
inline bool cmp(int x,int y){return t[x][D]<t[y][D];}
void pushup(int k)
{
for (int i=0;i<2;++i)
{
t[k].mn[i]=min(t[t[k].c[0]].mn[i],min(t[t[k].c[1]].mn[i],t[k].mn[i]));
t[k].mx[i]=max(t[t[k].c[0]].mx[i],max(t[t[k].c[1]].mx[i],t[k].mx[i]));
}
t[k].sum=t[t[k].c[0]].sum+t[t[k].c[1]].sum+t[k].v;
t[k].siz=t[t[k].c[0]].siz+t[t[k].c[1]].siz+1;
}
inline int build(int l,int r,int dir){
D=dir;
nth_element(p+l,p+mid,p+r+1,cmp);
int o=p[mid];
t[o].D=dir;
F(i,0,1) t[o].mn[i]=t[o].mx[i]=t[o][i];
t[o].sum=t[o].v;
L=l<mid ? build(l,mid-1,dir^1) : 0;
R=mid<r ? build(mid+1,r,dir^1) : 0;
pushup(o);
return o;
}
inline void dfs(int o){
if (!o) return;
dfs(L);
p[++cnt]=o;
dfs(R);
}
inline void rebuild(int &o){
cnt=0;
dfs(o);
o=build(1,cnt,t[o].D);
}
void ins(int &o,int dir)
{
if (!o)
{
o=++tot;
t[o]=now;
for (int i=0;i<2;++i) t[o].mn[i]=t[o].mx[i]=t[o].d[i];
t[o].siz=1;
t[o].D=dir;
t[o].sum=t[o].v;
return ;
}
if (now.d[dir]<t[o].d[dir])
{
ins(t[o].c[0],dir^1);
pushup(o);
if ((double)t[t[o].c[0]].siz>(double)t[o].siz*lim) rebuild(o);
}
else
{
ins(t[o].c[1],dir^1);
pushup(o);
if ((double)t[t[o].c[1]].siz>(double)t[o].siz*lim) rebuild(o);
}
}
void print(int o){
if (!o) return;
printf("%d t[o].mn[0]=%d t[o].mn[1]=%d t[o].mx[0]=%d t[o].mx[1]=%d\n",o,t[o].mn[0],t[o].mn[1],t[o].mx[0],t[o].mx[1]);
print(L);
print(R);
}
int query(int o,int x1,int y1,int x2,int y2)
{
if (!o) return 0;
if (t[o].mn[0]>=x1 && t[o].mn[1]>=y1 && t[o].mx[0]<=x2 && t[o].mx[1]<=y2)
return t[o].sum;
else
{
int ret=0;
if (t[o].d[0]>=x1&&t[o].d[0]<=x2&&t[o].d[1]>=y1&&t[o].d[1]<=y2)
ret+=t[o].v;
if (t[t[o].c[0]].mn[0]>x2||t[t[o].c[0]].mx[0]<x1||t[t[o].c[0]].mn[1]>y2||t[t[o].c[0]].mx[1]<y1);
else ret+=query(t[o].c[0],x1,y1,x2,y2);
if (t[t[o].c[1]].mn[0]>x2||t[t[o].c[1]].mx[0]<x1||t[t[o].c[1]].mn[1]>y2||t[t[o].c[1]].mx[1]<y1);
else ret+=query(t[o].c[1],x1,y1,x2,y2);
return ret;
}
}
int main()
{
for (int i=0;i<2;++i) t[rt].mx[i]=-inf,t[rt].mn[i]=inf;
t[rt].siz=t[rt].sum=t[rt].v=0;
scanf("%*d");
while (scanf("%d",&opt)!=EOF&&opt!=3)
{
if (opt==1)
{
scanf("%d%d%d",&now.d[0],&now.d[1],&now.v);
now.d[0]^=ans; now.d[1]^=ans; now.v^=ans;
ins(rt,1);
}
else{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1^=ans;y1^=ans;x2^=ans;y2^=ans;
printf("%d\n",ans=query(rt,x1,y1,x2,y2));
}
}
}
BZOJ 4066 简单题 ——KD-Tree套替罪羊树的更多相关文章
- bzoj 4066: 简单题 K-D树
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...
- bzoj 4066: 简单题 kd-tree
4066: 简单题 Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 234 Solved: 82[Submit][Status][Discuss] De ...
- bzoj 4066 简单题——KDtree(带重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...
- BZOJ4066:简单题(K-D Tree)
Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...
- P4148 简单题 k-d tree
思路:\(k-d\ tree\) 提交:2次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...
- BZOJ 4066 简单题(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...
- bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 https://www.lydsy.com/JudgeOnline/problem.p ...
- bzoj 4066: 简单题
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #d ...
- 【CJOJ2433】陌上花开 树状数组套替罪羊树
[CJOJ2433]陌上花开 树状数组套替罪羊树 蛤?bzoj?没权限QAQ 蛤?CDQ?看了好久没看懂QwQ 好吧我会拿cdq再写一遍的 为啥我感觉这东西比cdq好写 只好拿树状数组套替罪羊树水水了 ...
随机推荐
- Xms Xmx PermSize MaxPermSize 区别
Eclipse崩溃,错误提示: MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) s ...
- AVAudioPlayer
AVAudioPlayer在AVFoundation框架下,所以我们要导入AVFoundation.framework. AVAudioPlayer类封装了播放单个声音的能力.播放器可以用NSURL或 ...
- final修饰符
final本身的含义是"最终的,不可变的",它可以修饰非抽象类,非抽象方法和变量.注意:构造方法不能使用final修饰,因为构造方法不能被继承,肯定是最终的. final修饰的类: ...
- 【河北省队互测】 gcd BZOJ 2818
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sa ...
- python之路十八
1.JS 正则 test - 判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf ...
- java问题排查总结
前些天发现:http://hellojava.info/这个站点,关于java问题排查分析总结线上故障总结其实是最有价值的,好的总结就是一个系统演进历史,是团队难得的积累沉淀. 花了不少时间看了下,顺 ...
- java大并发数据保存方案
做了几年.net,如今终于要做java了. 需求: 线下终端会定时上传gps位置到服务端,服务端收到数据保存到mysql数据库,当线下终端过多时,问题出现了,首当其冲的是数据库连接池经常会崩溃,单个t ...
- Builder模式在Java中的应用
在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...
- 纯 CSS 绘制图形(心形、六边形等)
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...
- PHP Ueditor 富文本编辑器
2016年12月11日 08:46:59 星期日 百度的简版富文本编辑器umeditor很久没更新了 全功能版本的配置项跟umeditor还是有区别的, 这里说下ueditor怎么对接到项目中去, 主 ...