浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4066

裸的强制在线插入点,像替罪羊树那样重建不平衡的子树即可。在回收要重建的子树的节点的时候记得把结点信息初始化掉。

时间复杂度:\(O(n\sqrt{n})\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const double alpha=0.75;
const int maxn=2e5+5,inf=2e9; bool need_rebuild;
int n,lstans,pps,x,y,A,x1,x2,y1,y2; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct kd_tree {
int id[maxn];
int tot,root,cnt; struct point {
int c[2],mn[2],mx[2];
int val,sum,siz,ls,rs; bool operator<(const point &a)const {
return c[pps]<a.c[pps];
}
}p[maxn],node[maxn]; void prepare() {
p[0].mn[0]=p[0].mn[1]=inf;
p[0].mx[0]=p[0].mx[1]=-inf;
} void update(int u) {
int ls=p[u].ls,rs=p[u].rs;
p[u].siz=p[ls].siz+1+p[rs].siz;
p[u].sum=p[ls].sum+p[u].val+p[rs].sum;
for(int i=0;i<2;i++) {
int mn=min(p[ls].mn[i],p[rs].mn[i]);
p[u].mn[i]=min(p[u].c[i],mn);
int mx=max(p[ls].mx[i],p[rs].mx[i]);
p[u].mx[i]=max(p[u].c[i],mx);
}
} void ins(int &u,int d) {
if(!u) {
u=++tot;need_rebuild=1;
p[u].mn[0]=p[u].mx[0]=p[u].c[0]=x;
p[u].mn[1]=p[u].mx[1]=p[u].c[1]=y;
p[u].val=p[u].sum=A,p[u].siz=1;return;
}
if(p[u].c[0]==x&&p[u].c[1]==y) {
p[u].val+=A,p[u].sum+=A;return;
}
int tmp=d?y:x;
if(tmp<p[u].c[d])ins(p[u].ls,d^1);
else ins(p[u].rs,d^1);
update(u);
} void recycle(int u) {
if(!u)return;
int ls=p[u].ls,rs=p[u].rs;
p[u].siz=1,p[u].ls=p[u].rs=p[u].sum=0;
id[++cnt]=u,node[cnt]=p[u];
recycle(ls),recycle(rs);
} int rebuild(int l,int r,int d) {
int mid=(l+r)>>1,u=id[mid];pps=d;
nth_element(node+l,node+mid,node+r+1);
p[u]=node[mid];
if(l<mid)p[u].ls=rebuild(l,mid-1,d^1);
if(r>mid)p[u].rs=rebuild(mid+1,r,d^1);
update(u);return u;
} void check(int &u,int d) {
int siz1=p[p[u].ls].siz,siz2=p[p[u].rs].siz;
if(max(siz1,siz2)>1.0*p[u].siz*alpha) {
recycle(u),u=rebuild(1,cnt,d),cnt=0;return;
}
if(p[u].c[0]==x&&p[u].c[1]==y)return;
int tmp=d?y:x;
if(tmp<p[u].c[d])check(p[u].ls,d^1);
else check(p[u].rs,d^1);
} void query(int u) {
if(!u)return;
if(x2<p[u].mn[0]||x1>p[u].mx[0])return;
if(y2<p[u].mn[1]||y1>p[u].mx[1])return;
bool bo1=(x1<=p[u].mn[0]&&p[u].mx[0]<=x2);
bool bo2=(y1<=p[u].mn[1]&&p[u].mx[1]<=y2);
if(bo1&&bo2) {lstans+=p[u].sum;return;}
bo1=(x1<=p[u].c[0]&&p[u].c[0]<=x2);
bo2=(y1<=p[u].c[1]&&p[u].c[1]<=y2);
if(bo1&&bo2)lstans+=p[u].val;
if(p[u].ls)query(p[u].ls);
if(p[u].rs)query(p[u].rs);
}
}T; int main() {
n=read();T.prepare();
while(1) {
int opt=read();
if(opt==1) {
x=read()^lstans,y=read()^lstans,A=read()^lstans;
T.ins(T.root,0);if(need_rebuild)T.check(T.root,0);
}
if(opt==2) {
x1=read()^lstans,y1=read()^lstans;
x2=read()^lstans,y2=read()^lstans;
lstans=0,T.query(T.root),printf("%d\n",lstans);
}
if(opt==3)break;
}
return 0;
}

BZOJ4066:简单题的更多相关文章

  1. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

  2. bzoj4066: 简单题 K-Dtree

    bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...

  3. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  4. BZOJ4066 简单题(KD-Tree)

    板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  5. 【kd-tree】bzoj4066 简单题

    同p1176. #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ...

  6. BZOJ4066:简单题(K-D Tree)

    Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:   命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...

  7. 【BZOJ4066】简单题(KD-Tree)

    [BZOJ4066]简单题(KD-Tree) 题面 BZOJ 题解 如果这题不卡空间,并且不强制在线的话 显然可以用\(CDQ\)分治做 但是它又卡空间又强制在线,于是我们欢快的来用\(KD-Tree ...

  8. 【BZOJ4066】简单题 KDtree

    [BZOJ4066]简单题 Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y& ...

  9. [bzoj4066/2683]简单题_KD-Tree

    简单题 bzoj-4066 题目大意:n*n的棋盘,开始为均为0,支持:单点加权值,查询矩阵权值和,强制在线. 注释:$1\le n\le 5\cdot 10^5$,$1\le m \le 2\cdo ...

  10. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

随机推荐

  1. 一些逼格略高的 js 片段

    // 一个接一个运行 // timeout 不能直接放在 for 里面,暂时不知道为什么 function functionOneByOne(fn, times, duration) { for(va ...

  2. 【P2015】二叉苹果树(树状DP)

    蒟蒻弱弱的开始做树形DP了,虽然做了这道题还是有很多不懂得地方. 这道题大意就是有一棵树,只保留其中q条边,求出剩余边的最大权值. 然后开始考虑怎么做(其实是看着题解出思路....),很容易可以想出D ...

  3. input ajax自动补全

    页面 <div class="manage-Car-add-info-sn"> <p style="width:25%; height:70%;floa ...

  4. 键盘高级操作技巧【TLCL】

    Ctrl-a     移动光标到行首. Ctrl-e     移动光标到行尾. Ctrl-f     光标前移一个字符:和右箭头作用一样. Ctrl-b     光标后移一个字符:和左箭头作用一样. ...

  5. MVC 绑定 下拉框数据

    HTML: <div class="form-group col-sm-12"> <div class="col-sm-4"> < ...

  6. javascript 时间日期处理相加相减

    var d = new Date("2008/04/15"); d.setMonth(d.getMonth() + 1 + 1);//加一个月,同理,可以加一天:getDate() ...

  7. Oralce查询后修改数据,弹窗报提示these query result are not updateable,include the ROWID to get updateable

    select t.*, (select a.ANNEXNAME from base_annex a where a.id = t.closeFile) closeFileName, (select a ...

  8. GreenDao 多表事务操作

    场景:Android APP多表操作事务管理 使用Android自带的sql操作类操作的时候需要手动处理事务,使用GreenDao的时候不用管了,啥都处理好了.但是,如果是多表操作的话,怎么统一管理事 ...

  9. Linux软件安装常用方法

    1.软件安装卸载,分几种情况: A:RPM包,这种软件包就像windows的EXE安装文件一样,各种文件已经编译好,并打了包,哪个文件该放到哪个文件夹,都指定好了,安装非常方便,在图形界面里你只需要双 ...

  10. 调用摄像头并将其显示在UGUI image上自适应屏幕大小

    参考链接:http://www.cnblogs.com/Erma-king/p/5869177.html 不过该博主是竖屏,我的是横屏 代码修改: using UnityEngine; using S ...