浅谈\(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. 纯代码编写qt登录界面(转)

    1. 新建Qt Widgets Application,项目名称为login1,在类信息页面保持类名和基类为MainWindow和QMainWindow不变,取消选择创建界面选项,如下图所示.     ...

  2. 教你在windows10环境下如何安装minepy并成功运行!

    在学习使用sklearn做单机特征工程这篇文章时,发现在计算互信息时from minepy import MINE代码运行出错ModuleNotFoundError: No module named ...

  3. vmxnet3 丢包处理

    https://vswitchzero.com/2017/09/26/vmxnet3-rx-ring-buffer-exhaustion-and-packet-loss/

  4. Threalocal的使用及其原理

    虽然现在可以说很多程序员会用ThreadLocal,但是我相信大多数程序员还不知道ThreadLocal,而使用ThreadLocal的程序员大多只是知道其然而不知其所以然,因此,使用ThreadLo ...

  5. Centos6.5使用yum安装软件的时候 Another app is currently holding the yum lock; waiting for it to exit...

    Loaded plugins: fastestmirror, refresh-packagekit, security Existing . Another app is currently hold ...

  6. spring mvc 对象型参数的传递(遇到坑了)

    直接来个列子: 这里设置了,contenType="application/json" 这里post 接收的参数对象. 但是问题来了: <html> <head& ...

  7. 使用 grep 查找所有包含指定文本的文件

    目标:本文提供一些关于如何搜索出指定目录或整个文件系统中那些包含指定单词或字符串的文件. 难度:容易 约定: # - 需要使用 root 权限来执行指定命令,可以直接使用 root 用户来执行也可以使 ...

  8. Visual Studio 当前不会命中断点的问题 编辑pdf文件

    Visual Studio 当前不会命中断点的问题 PDB文件概述

  9. linux vim vi编辑时撤销输入操作

    linux vim vi编辑时撤销输入操作 1,esc退出输入状态 2,u 撤销上次操作 3,ctrl+r 恢复撤销

  10. hibernate报错org.hibernate.HibernateException: No CurrentSessionContext configured!

    org.hibernate.HibernateException: No CurrentSessionContext configured! at org.hibernate.internal.Ses ...