P4148 简单题(KDTree)
KDTree
修改权值当做插入节点,不平衡就暴力重构,询问的时候判断当前节点代表的矩形是否在询问的矩形的,是的话返回答案,相离返回0,否则的话判断当前点是否在矩形内,然后继续递归下去
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=2e5+5;
struct point{int v[2],w;}pool[N];
struct node{int mn[2],mx[2],sum,l,r,sz;point tp;}tr[N];
int n,m,K,rt,top,cnt,st[N],ans;
inline bool operator <(const point &a,const point &b){return a.v[K]<b.v[K];}
inline int newnode(){return top?st[top--]:++cnt;}
void upd(int p){
int l=tr[p].l,r=tr[p].r;
fp(i,0,1){
tr[p].mn[i]=tr[p].mx[i]=tr[p].tp.v[i];
if(l)cmin(tr[p].mn[i],tr[l].mn[i]),cmax(tr[p].mx[i],tr[l].mx[i]);
if(r)cmin(tr[p].mn[i],tr[r].mn[i]),cmax(tr[p].mx[i],tr[r].mx[i]);
}tr[p].sum=tr[l].sum+tr[r].sum+tr[p].tp.w,tr[p].sz=tr[l].sz+tr[r].sz+1;
}
int build(int l,int r,int k){
if(l>r)return 0;int mid=(l+r)>>1,p=newnode();
K=k,nth_element(pool+l,pool+mid,pool+r+1),tr[p].tp=pool[mid];
tr[p].l=build(l,mid-1,k^1),tr[p].r=build(mid+1,r,k^1);
upd(p);return p;
}
void pia(int p,int sum){
if(tr[p].l)pia(tr[p].l,sum);
pool[sum+tr[tr[p].l].sz+1]=tr[p].tp,st[++top]=p;
if(tr[p].r)pia(tr[p].r,sum+tr[tr[p].l].sz+1);
}
void ck(int &p,int k){
if(tr[p].sz*0.75<tr[tr[p].l].sz||tr[p].sz*0.75<tr[tr[p].r].sz)
pia(p,0),p=build(1,tr[p].sz,k);
}
void ins(int &p,point res,int k){
if(!p)return (void)(p=newnode(),tr[p].l=tr[p].r=0,tr[p].tp=res,upd(p));
res.v[k]<=tr[p].tp.v[k]?ins(tr[p].l,res,k^1):ins(tr[p].r,res,k^1);
upd(p),ck(p,k);
}
inline bool in(int x1,int y1,int x2,int y2,int X1,int Y1,int X2,int Y2){
return x1<=X1&&y1<=Y1&&x2>=X2&&y2>=Y2;
}
inline bool out(int x1,int y1,int x2,int y2,int X1,int Y1,int X2,int Y2){
return x1>X2||x2<X1||y1>Y2||y2<Y1;
}
int query(int p,int x1,int y1,int x2,int y2){
if(!p)return 0;int res=0;
if(in(x1,y1,x2,y2,tr[p].mn[0],tr[p].mn[1],tr[p].mx[0],tr[p].mx[1]))return tr[p].sum;
if(out(x1,y1,x2,y2,tr[p].mn[0],tr[p].mn[1],tr[p].mx[0],tr[p].mx[1]))return 0;
if(in(x1,y1,x2,y2,tr[p].tp.v[0],tr[p].tp.v[1],tr[p].tp.v[0],tr[p].tp.v[1]))res+=tr[p].tp.w;
res+=query(tr[p].l,x1,y1,x2,y2)+query(tr[p].r,x1,y1,x2,y2);return res;
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();int op,x1,y1,x2,y2;
while(true){
op=read();if(op==3)break;
if(op==1)ins(rt,(point){read()^ans,read()^ans,read()^ans},0);
else{
x1=read()^ans,y1=read()^ans,x2=read()^ans,y2=read()^ans;
ans=query(rt,x1,y1,x2,y2),print(ans);
}
}return Ot(),0;
}
P4148 简单题(KDTree)的更多相关文章
- 洛谷 P4148 简单题 KD-Tree 模板题
Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...
- 洛谷 P4148 简单题 解题报告
P4148 简单题 题意 维护单点加与矩形求和,强制在线 说明 \(n\le 500000,m\le 200000\),\(4000ms / 20MB\) kd-tree 复杂度我不懂 是一颗平衡树, ...
- bzoj 4066: 简单题 kd-tree
4066: 简单题 Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 234 Solved: 82[Submit][Status][Discuss] De ...
- 【BZOJ4066】简单题 KDtree
[BZOJ4066]简单题 Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y& ...
- bzoj4066: 简单题 K-Dtree
bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...
- luogu P4148 简单题
传送门 这题真简单,直接把\(CDQ\)给ban掉了 其实数据范围比较小可以直接二维树状数组,我们看数据范围,发现点的个数比N还小,可以考虑用一些奇怪的数据结构 说的就是你,\(KD tree\) \ ...
- Luogu P4148 简单题(K-D Tree)
题面 题解 因为强制在线,所以我们不能$cdq$分治,所以考虑用$KDT$,$KDT$维护一个矩阵,然后询问的时候如果当前矩形在询问区间内,直接记贡献,否则判断当前点是否在矩阵内,然后左右分别递归下去 ...
- bzoj 4066 简单题——KDtree(带重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...
- 【bzoj4066】简单题 KD-tree
题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...
随机推荐
- Org-mode五分钟教程ZZZ
Table of Contents 1 源起 2 简介 2.1 获取 org-mode 2.2 安装 3 基础用法 3.1 创建一个新文件 3.2 简单的任务列表 3.3 使用标题组织一篇文章 3.4 ...
- Bag-of-words模型、TF-IDF模型
Bag-of-words model (BoW model) 最早出现在NLP和IR(information retrieval)领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words) ...
- [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync
In node.js, you can require fs, and then call fs.writeFile with the filename, and data to write to t ...
- Mac 使用smb协议连接FTPserver
在Mac中,能够通过smb协议作为client连接到server,比如一个FTPserver,然后获取上面的共享文件. 方法: 1.在Finder菜单中点击前往 -- 连接server. 也能够Com ...
- HDU 3080 The plan of city rebuild(prim和kruskal)
The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 3 TypeScript 语法特性
一.类型注解(Type annotations) TypeScript 通过类型注解提供静态类型以在编译时启动类型检查,简单来说,就是指定数据类型,它会在代码运行的时候,对传入的数据进行数据类型匹配检 ...
- 【iOS系列】-iOS开发常用库文件总结
这里是列举出得一部分,更多内容可参考https://github.com/darren90/Gather_iOS 码农周刊的总结 - 覆盖很广 调调的 - 很多开发相关内容都有体现 右滑返回的解决 - ...
- python爬虫爬取内容中,-xa0,-u3000的含义
python爬虫爬取内容中,-xa0,-u3000的含义 - CSDN博客 https://blog.csdn.net/aiwuzhi12/article/details/54866310
- 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法【转】
本文转载自:http://blog.csdn.net/jeesenzhang/article/details/40300127 今天发现电脑的系统时间不正确,因此将时钟进行了修改,回头编译Linux ...
- Iterator 使用
Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactor ...