浅谈\(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. HttpUtils工具类

    HttpUtils工具类 package net.hs.itn.teng.common.util; import java.io.IOException; import java.io.Unsuppo ...

  2. HTseq-count

    HTSeq:一个用于处理高通量数据(High-throughout sequencing)的python包.HTSeq包有很多功能类,熟悉python脚本的可以自行编写数据处理脚本.另外,HTSeq也 ...

  3. windows简单使用etcd

    一.下载安装选择版本 https://github.com/coreos/etcd/releases 二.解压 三.首先开启etcd 1.进入在etcd解压的目录中 2.etcd.exe 没有erro ...

  4. Go make 和 new的区别

    在Go语言中: make 被用来分配引用类型的内存: map, slice, channel new 被用来分配除了引用类型的所有其他类型的内存: int, string, array等 本文主要给大 ...

  5. MongoDB快速入门(七)- Save() 方法

    MongoDB Save() 方法 save() 方法取代,通过新文档到 save()方法 语法 mongodb 的 save()方法如下所示的基本语法: >db.COLLECTION_NAME ...

  6. C语言一个细节地方的说明【防止使用不当而出错】

    1.运行如下的代码: #include <stdio.h> #include <string.h> int main() { int a; a=1; int s[4]; mem ...

  7. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. STL中一些函数的应用

    1.nth_element():找到第几大的数.用法:nth_element(a,a+k,a+n),返回一个数组a中第k大的数,时间复杂度比较小,头文件#include <algorithm&g ...

  9. How to use Jenkins

    一.关键点 1.how to start the build server? do i need to start some app to do this? I don't believe so... ...

  10. js多个<ul>相应不同的点击事件

    $('ul').on("click","li#left",function(){ currentProvince = $(this).text().replac ...