简单题(K-D Tree)
简单题不简单……
我们把单点加操作改成插入一个权值为增加量的点,将问题转化成询问一个矩阵中所有点的和,用 \(K-D\ Tree\) 维护,时间复杂度 \(O(n\sqrt{n})\)
\(Code\ Below:\)
// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
const int maxn=500000+10;
const double alpha=0.75;
int n,D,rt,cnt,tot,rub[maxn],top;
struct node{
int d[2],val;
}a[maxn];
inline bool operator < (const node &a,const node &b){
return a.d[D]<b.d[D];
}
struct KD_Tree{
int d[2],Max[2],Min[2],ch[2],val,siz,sum;
inline void init(){
d[0]=d[1]=Max[0]=Max[1]=Min[0]=Min[1]=ch[0]=ch[1]=val=siz=sum=0;
}
inline void get(node a){
Max[0]=Min[0]=d[0]=a.d[0];
Max[1]=Min[1]=d[1]=a.d[1];
siz=1;val=sum=a.val;
}
}t[maxn];
inline int read(){
register int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return (f==1)?x:-x;
}
inline int newnode(){
int x=top?rub[top--]:++cnt;
t[x].init();return x;
}
inline void update(int x,int y){
t[x].Max[0]=max(t[x].Max[0],t[y].Max[0]);
t[x].Max[1]=max(t[x].Max[1],t[y].Max[1]);
t[x].Min[0]=min(t[x].Min[0],t[y].Min[0]);
t[x].Min[1]=min(t[x].Min[1],t[y].Min[1]);
}
inline void pushup(int x){
t[x].siz=t[t[x].ch[0]].siz+t[t[x].ch[1]].siz+1;
t[x].sum=t[t[x].ch[0]].sum+t[t[x].ch[1]].sum+t[x].val;
if(t[x].ch[0]) update(x,t[x].ch[0]);
if(t[x].ch[1]) update(x,t[x].ch[1]);
}
int build(int l,int r,int now){
int mid=(l+r)>>1,x=newnode();D=now;
nth_element(a+l,a+mid,a+r+1);
t[x].get(a[mid]);
if(l<mid) t[x].ch[0]=build(l,mid-1,now^1);
if(mid<r) t[x].ch[1]=build(mid+1,r,now^1);
pushup(x);return x;
}
void del(int x){
if(t[x].ch[0]) del(t[x].ch[0]);
a[++tot].d[0]=t[x].d[0];
a[tot].d[1]=t[x].d[1];
a[tot].val=t[x].val;
rub[++top]=x;
if(t[x].ch[1]) del(t[x].ch[1]);
}
void check(int &x,int now){
if(1.0*max(t[t[x].ch[0]].siz,t[t[x].ch[1]].siz)>1.0*alpha*t[x].siz)
tot=0,del(x),x=build(1,tot,now);
}
void insert(int &x,node c,int now){
if(!x){
x=newnode();
t[x].get(c);
return ;
}
if(c.d[now]<=t[x].d[now]) insert(t[x].ch[0],c,now^1);
else insert(t[x].ch[1],c,now^1);
pushup(x);check(x,now);
}
int query(int x,int x1,int x2,int y1,int y2){
if(t[x].Max[0]<x1||t[x].Min[0]>x2||t[x].Max[1]<y1||t[x].Min[1]>y2) return 0;
if(t[x].Max[0]<=x2&&t[x].Min[0]>=x1&&t[x].Max[1]<=y2&&t[x].Min[1]>=y1) return t[x].sum;
int ans=0;
if(t[x].d[0]<=x2&&t[x].d[0]>=x1&&t[x].d[1]<=y2&&t[x].d[1]>=y1) ans=t[x].val;
if(t[x].ch[0]) ans+=query(t[x].ch[0],x1,x2,y1,y2);
if(t[x].ch[1]) ans+=query(t[x].ch[1],x1,x2,y1,y2);
return ans;
}
int main()
{
n=read();
int op,x,y,z,x1,x2,y1,y2,lastans=0;
while(1){
op=read();
if(op==1){
x=read()^lastans,y=read()^lastans,z=read()^lastans;
insert(rt,(node){x,y,z},0);
}
if(op==2){
x1=read()^lastans,y1=read()^lastans,x2=read()^lastans,y2=read()^lastans;
printf("%d\n",lastans=query(rt,x1,x2,y1,y2));
}
if(op==3) break;
}
return 0;
}
简单题(K-D Tree)的更多相关文章
- Luogu P4148 简单题(K-D Tree)
题面 题解 因为强制在线,所以我们不能$cdq$分治,所以考虑用$KDT$,$KDT$维护一个矩阵,然后询问的时候如果当前矩形在询问区间内,直接记贡献,否则判断当前点是否在矩阵内,然后左右分别递归下去 ...
- BZOJ4066:简单题(K-D Tree)
Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...
- acm.njupt 1001-1026 简单题
点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
- 又一道简单题&&Ladygod(两道思维水题)
Ladygod Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- (hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)
题目: 平方和与立方和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- springMVC学习 六 跳转方式
SpringMVC的controller中的方法执行完之后,默认的跳转方式是请求转发 如果想要修改跳转方式,可以设置返回值字符串内容(1) 添加 redirect:资源路径 重定向 "red ...
- android-如何获得当前正在运行的activity的相关信息
http://blog.csdn.net/centralperk/article/details/7269326 ActivityManager manager = (ActivityManager) ...
- hdu 6208(后缀自动机、或者AC自动机
题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...
- Python中逗号的妙用
闲着没事打算用Python刷一遍pat,输出过程中遇到了一个这样的问题: 题目1002题目要求 在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格, 但是P ...
- 2018.10.26 NOIP模拟 性感手枪(搜索)
传送门 vis[x][y]vis[x][y]vis[x][y]记录这个点是否在之前被搜过,且被搜过的坐标是什么. 然后搜索的时候记录一个循环的下标和不循环的下标就行了. 代码
- 2018.06.27The Windy's(费用流)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6003 Accepted: 2484 Descripti ...
- 牛客训练六:海啸(二维树状数组+vector函数的使用)
题目链接:传送门 思路: 二维树状数组, vector(first,last)函数中assign函数相当于将first中的函数清空,然后将last中的值赋值给first. 参考文章:传送门 #incl ...
- oracle数据库的迁移(从一台服务器到另一个台服务器,从oracle 10g到oracle 11g)
这个过程呢,还是蛮艰难的.... 一.最初我使用的是Navicat中的数据传输来迁移的,虽说整个数据库的迁移没有成功,但传输指定的对象时还是传输成功了.所以还是记录一下吧. 1.前提连接好数据库.在指 ...
- UVa 11427 Expect the Expected (数学期望 + 概率DP)
题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...
- 乌龙之Ignoring query to other database问题
问题现象: [root@zxdb05 ~]# mysql -root -pEnter password: Welcome to the MySQL monitor. Commands end wit ...