bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066
https://www.lydsy.com/JudgeOnline/problem.php?id=2683
高仿:https://www.cnblogs.com/Narh/p/9605505.html
注意细节...
AC 300 ~
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=2e5+5;
double const alpha=0.75;
int n,cnt,sta[xn],top,rt,dm,c[xn][2],qx1,qx2,qy1,qy2;
int R,fa,son,num;
ll ans;
struct N{
int x[2],y[2],p[2],siz;
ll sum,ys;
}t[xn],a[xn];
int rd()
{
int ret=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return f?ret:-ret;
}
ll Abs(ll x){return x>0?x:-x;}
ll Min(ll x,ll y){return x<y?x:y;}
ll Max(ll x,ll y){return x<y?y:x;}
bool cmp(N x,N y){return x.p[dm]<y.p[dm];}
int node(){if(top)return sta[top--]; else return ++cnt;}
void turn(int x,N v)
{
for(int i=0;i<2;i++)t[x].x[i]=t[x].y[i]=t[x].p[i]=v.p[i];
t[x].ys=t[x].sum=v.ys; t[x].siz=1; c[x][0]=0; c[x][1]=0;//ys //c //v.ys!!!
}
void pushup(int x)
{
int ls=c[x][0],rs=c[x][1];
for(int i=0;i<2;i++)
{
if(ls)t[x].x[i]=Min(t[x].x[i],t[ls].x[i]),t[x].y[i]=Max(t[x].y[i],t[ls].y[i]);
if(rs)t[x].x[i]=Min(t[x].x[i],t[rs].x[i]),t[x].y[i]=Max(t[x].y[i],t[rs].y[i]);
}
t[x].sum=t[x].ys+(ls?t[ls].sum:0)+(rs?t[rs].sum:0);
t[x].siz=1+(ls?t[ls].siz:0)+(rs?t[rs].siz:0);
}
bool ck(int x)
{
int ls=c[x][0],rs=c[x][1];
return (ls&&t[ls].siz>t[x].siz*alpha)||(rs&&t[rs].siz>t[x].siz*alpha);
}
void ins(int &x,N v,int nw)
{
if(!x){x=node(); turn(x,v); return;}
if(v.p[nw]<=t[x].p[nw])ins(c[x][0],v,nw^1);
else ins(c[x][1],v,nw^1);
pushup(x);
if(ck(x))R=x,dm=nw;
if(R==c[x][0])fa=x,son=0; if(R==c[x][1])fa=x,son=1;
}
bool can(int x)
{return t[x].p[0]>=qx1&&t[x].p[0]<=qx2&&t[x].p[1]>=qy1&&t[x].p[1]<=qy2;}
bool in(int x)
{return t[x].x[0]>=qx1&&t[x].y[0]<=qx2&&t[x].x[1]>=qy1&&t[x].y[1]<=qy2;}
bool out(int x)
{return t[x].x[0]>qx2||t[x].y[0]<qx1||t[x].x[1]>qy2||t[x].y[1]<qy1;}
ll query(int x)
{
if(can(x))ans+=t[x].ys;//ys
int ls=c[x][0],rs=c[x][1];
if(ls)
{
if(in(ls))ans+=t[ls].sum;
else if(!out(ls))query(ls);
}
if(rs)
{
if(in(rs))ans+=t[rs].sum;
else if(!out(rs))query(rs);
}
}
void kill(int x)
{
sta[++top]=x; a[++num]=t[x];
if(c[x][0])kill(c[x][0]);
if(c[x][1])kill(c[x][1]);
}
void build(int &x,int l,int r,int nw)
{
x=node(); dm=nw; int mid=((l+r)>>1);
nth_element(a+l,a+mid,a+r+1,cmp);
turn(x,a[mid]);
if(mid>l)build(c[x][0],l,mid-1,nw^1);
if(mid<r)build(c[x][1],mid+1,r,nw^1);
pushup(x);
}
void rbuild(int x)
{
num=0; kill(x); int mid=((1+num)>>1);
nth_element(a+1,a+mid,a+num+1,cmp);
int p=node(); turn(p,a[mid]);
if(fa)c[fa][son]=p; else rt=p;//
if(mid>1)build(c[p][0],1,mid-1,dm^1);
if(mid<num)build(c[p][1],mid+1,num,dm^1);
pushup(p);
}
int main()
{
n=rd(); N tmp;
while((n=rd())!=3)
{
if(n==1)
{
//tmp.p[0]=(rd()^ans); tmp.p[1]=(rd()^ans); tmp.ys=(rd()^ans);
tmp.p[0]=rd(); tmp.p[1]=rd(); tmp.ys=rd();
fa=0; ins(rt,tmp,0);//fa
}
else
{
//qx1=(rd()^ans); qy1=(rd()^ans);
//qx2=(rd()^ans); qy2=(rd()^ans);
qx1=rd(); qy1=rd(); qx2=rd(); qy2=rd();
ans=0; query(rt); printf("%lld\n",ans);
}
if(R){if(R==rt)fa=0; rbuild(R); R=0;}
}
return 0;
}
bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)的更多相关文章
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...
- [bzoj4066/2683]简单题_KD-Tree
简单题 bzoj-4066 题目大意:n*n的棋盘,开始为均为0,支持:单点加权值,查询矩阵权值和,强制在线. 注释:$1\le n\le 5\cdot 10^5$,$1\le m \le 2\cdo ...
- P5057 [CQOI2006]简单题(线段树)
果然简单题,5分钟紫题++ 代码 #include <cstdio> #include <algorithm> #include <cstring> using n ...
- BZOJ 2683: 简单题(CDQ分治 + 树状数组)
BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...
- BZOJ 2683: 简单题 [CDQ分治]
同上题 那你为什么又发一个? 因为我用另一种写法又写了一遍... 不用排序,$CDQ$分治的时候归并排序 快了1000ms... #include <iostream> #include ...
- BZOJ 4066 简单题(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...
- BZOJ 2683: 简单题(CDQ 分治)
题面 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: ...
随机推荐
- LLVM编译器
LLVM 1. 说说 LLVM(Low Level Virtual Machine)到底是什么吧 先说编译器:编译器是把程序员的代码翻译成机器可以理解的语言的工具: 再谈 LLVM:一个模块化和可重用 ...
- 低秩近似 low-rank approximation
- MySQL5.7.26 忘记Root密码小计
以前直接修改mysql.user就ok了,现在不行了,正好虚拟机MySQL的root密码忘记了,就简单记录下:(本方法不适合互联网线上项目,除非你不在意这段时间的损失) PS:以UbuntuServe ...
- 特性,批次特性建立的BAPI函數
[转http://taijizhang.blog.163.com/blog/static/176071381201442225514453/] SAP特性,物料特性,批次特性建立的BAPI函數 类的T ...
- eclipse InvocationTargetException 错误解决
1.今天做一个推送的,用到了几个jar包,直接以User Library的形式加进 在单元测试中,测试的很好,没有任何问题, 但是在action中测试,一测试就崩. 跟踪以后出现InvocationT ...
- python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...
- python基础21 ------python基础之socket编程
一.C/S架构和B/S架构的简介 略 二.osi七层模型 略 三.socket层 1.如图所示: socket层是存在于应用层和传输层直接抽象出来的一层. 2.socket层是什么? Socket是应 ...
- path.join()和path.resolve()区别
一.区别 1.path.join() 方法使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起,然后规范化生成的路径. 2.path.resolve() 方法将路径或路径片段的序列解析 ...
- hihocoder第七周 完全背包模板题
时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说之前的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的时刻了! 等等,这段故事为 ...
- EntityFramework 学习 一 Explicit Loading with DBContext
即使延迟加载不能使用,也可以通过明确的调用来延迟加载相关实体 使用DBEntryEntity来完成 using (var context = new SchoolDBEntities()) { //D ...