BZOJ 4066 kd-tree 矩形询问求和
第一次遇见强制在线的题目 每个操作都和前面的ans有关 所以不能直接离线做
在这个问题中 kdtree更像一个线段树在一维单点修改区间询问的拓展一样
如果区间被询问区间完全包含 就不用继续递归
插入时如果该点已被修改 就不用建新点
由于kdtree是一个二叉搜索树 所以如果数据构造 是可以卡出一条链的 所以需要在插入一定点数之后开始重构这个kdtree 使深度维持在一个可控范围内
因为写错了in_it函数找了一天bug
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
#include<string>
using namespace std;
#define L long long
const int INF = 999999999 ;
const int maxn = 200050 ;
int n , root , cmp_d , m;
struct node {
int d[2] , Max[2] , Min[2] ;
int l , r ;
int sum ;
int z ;
}a[maxn];
int x, y , X;
int x1, x2, y11 ,y2 ;
bool cmp(node a , node b ){return ((a.d[cmp_d] < b.d[cmp_d]) || (a.d[cmp_d] == b.d[cmp_d] && a.d[!cmp_d] < b.d[!cmp_d])) ;}
void up(int p , int k) {
a[p].Max[0] = max(a[p].Max[0] , a[k].Max[0]) ;
a[p].Max[1] = max(a[p].Max[1] , a[k].Max[1]) ;
a[p].Min[0] = min(a[p].Min[0] , a[k].Min[0]) ;
a[p].Min[1] = min(a[p].Min[1] , a[k].Min[1]) ;
a[p].sum += a[k].sum ;
}
int build(int l , int r , int D){
int mid = (l+r) / 2 ;
cmp_d = D;
nth_element(a+1+l,a+1+mid,a+1+r,cmp) ;
a[mid].Max[0] = a[mid].Min[0] = a[mid].d[0] ;
a[mid].Max[1] = a[mid].Min[1] = a[mid].d[1] ;
a[mid].sum = a[mid].z ;
if(l != mid) a[mid].l = build(l,mid-1,D^1) ; else a[mid].l = 0;
if(r != mid) a[mid].r = build(mid+1,r,D^1) ; else a[mid].r = 0;
if(a[mid].l)up(mid,a[mid].l) ;
if(a[mid].r)up(mid,a[mid].r) ;
return mid ;
}
void inse(int x , int y , int X) {
int p = root ;
int D = 0 ;
while(true) {
if(x > a[p].Max[0]) a[p].Max[0] = x ;
if(x < a[p].Min[0]) a[p].Min[0] = x ;
if(y > a[p].Max[1]) a[p].Max[1] = y ;
if(y < a[p].Min[1]) a[p].Min[1] = y ;
a[p].sum += X ;
if(x == a[p].d[0] && y == a[p].d[1]) {
a[p].z += X ;
return ;
}
else {
if(D == 0) {
if(x <= a[p].d[0]) {
if(a[p].l) p = a[p].l ;
else {
n ++ ;
a[n].l = a[n].r = 0 ;
a[n].Max[0] = a[n].Min[0] = a[n].d[0] = x ;
a[n].Min[1] = a[n].Max[1] = a[n].d[1] = y ;
a[n].sum = a[n].z = X ;
a[p].l = n ;
return ;
}
}
else {
if(a[p].r) p = a[p].r ;
else {
n ++ ;
a[n].l = a[n].r = 0 ;
a[n].Max[0] = a[n].Min[0] = a[n].d[0] = x ;
a[n].Min[1] = a[n].Max[1] = a[n].d[1] = y ;
a[n].sum = a[n].z = X ;
a[p].r = n ;
return ;
}
}
}
else {
if(y <= a[p].d[1]) {
if(a[p].l) p = a[p].l ;
else {
n ++ ;
a[n].l = a[n].r = 0 ;
a[n].Max[0] = a[n].Min[0] = a[n].d[0] = x ;
a[n].Min[1] = a[n].Max[1] = a[n].d[1] = y ;
a[n].sum = a[n].z = X ;
a[p].l = n ;
return ;
}
}
else {
if(a[p].r) p = a[p].r ;
else {
n ++ ;
a[n].l = a[n].r = 0 ;
a[n].Max[0] = a[n].Min[0] = a[n].d[0] = x ;
a[n].Min[1] = a[n].Max[1] = a[n].d[1] = y ;
a[n].sum = a[n].z = X ;
a[p].r = n ;
return ;
}
}
}
}
D ^= 1 ;
}
}
bool in_it(int p , int x1,int y11,int x2 , int y2 ){
if(x1 <= a[p].Min[0] && x2 >= a[p].Max[0] && y11 <= a[p].Min[1] && y2 >= a[p].Max[1]) {
return true ;
}
return false ;
}
bool rea_out(int p , int x1 , int y11 , int x2 , int y2 ){
if(x2 < a[p].Min[0] || x1 > a[p].Max[0] || y2 < a[p].Min[1] || y11 > a[p].Max[1]) return true;
return false ;
}
int ans ;
void ask(int p) {
if(rea_out(p , x1 , y11 , x2 , y2)) return ;
if(in_it(p , x1 , y11 , x2 , y2)) {
ans += a[p].sum ;
return ;
}
if(a[p].d[0] >= x1 && a[p].d[0] <= x2 && a[p].d[1] >= y11 && a[p].d[1] <= y2) {
ans += a[p].z ;
}
if(a[p].l){
ask(a[p].l);
}
if(a[p].r){
ask(a[p].r);
}
}
int main(){
scanf("%d",&m);
int op;
int last=0;
n = 0;
while(~scanf("%d",&op)){
if(op == 3)break;
if(op == 1) {
scanf("%d%d%d",&x,&y,&X) ;
x ^= last;
y ^= last;
X ^= last;
if(n == 0) {
n ++ ;
a[n].d[0] = x ; a[n].d[1] = y ;
a[n].z = X ;
a[n].sum = X ;
root = build(1,n,0) ;
}
else {
inse(x,y,X) ;
}
if(n % 5000 == 0) {
root = build(1,n,0) ;
}
}
else {
scanf("%d%d%d%d",&x1,&y11,&x2,&y2) ;
x1 ^= last;
y11 ^= last;
x2 ^= last;
y2 ^= last;
ans = 0;
if(n>0)
ask(root);
else ans = 0 ;
last = ans ;
printf("%d\n",ans) ;
}
}
}
BZOJ 4066 kd-tree 矩形询问求和的更多相关文章
- BZOJ - 4066 KD树 范围计数 暴力重构
题意:单点更新,大矩阵(\(n*n,n≤10^5\))求和 二维的KD树能使最坏情况不高于\(O(N\sqrt{N})\) 核心在于query时判断当前子树维护的区间是否有交集/当前子节点是否在块中, ...
- bzoj 4066: 简单题 K-D树
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...
- BZOJ 3489: A simple rmq problem(K-D Tree)
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2579 Solved: 888[Submit][Status][Discuss] Descripti ...
- [BZOJ 3221][Codechef FEB13] Obserbing the tree树上询问
[BZOJ 3221]Obserbing the tree树上询问 题目 小N最近在做关于树的题.今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M ...
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )
树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...
- BZOJ 3053: The Closest M Points(K-D Tree)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1235 Solved: 418[Submit][Status][Discuss] Descripti ...
- BZOJ 4520: [Cqoi2016]K远点对(k-d tree)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1162 Solved: 618[Submit][Status][Discuss] Descripti ...
- BZOJ 1941: [Sdoi2010]Hide and Seek(k-d Tree)
Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 1712 Solved: 932[Submit][Status][Discuss] Descripti ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
随机推荐
- 基于Boost无锁队列实现的内存池
- 【BZOJ4917】Hash Killer IV 乱搞
[BZOJ4917]Hash Killer IV Description 有一天,tangjz造了一个Hash函数: unsigned int Hash(unsigned int v){ un ...
- 【BZOJ3158】千钧一发 最小割
[BZOJ3158]千钧一发 Description Input 第一行一个正整数N. 第二行共包括N个正整数,第 个正整数表示Ai. 第三行共包括N个正整数,第 个正整数表示Bi. Output 共 ...
- Redis持久化之rdb&aof
Redis有两种持久化的方式:快照(RDB文件)和追加式文件(AOF文件) RDB持久化方式是在一个特定的间隔保存某个时间点的一个数据快照. AOF(Append only file)持久化方式则会记 ...
- Linux下Solr的安装和配置
一.安装 1.需要的安装包:apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 2.解压tomcat并创建solr文件夹 [root@lo ...
- 巨蟒python全栈开发django9:一些知识点的汇总
回顾上周内容: 题目: 1.人民出版社出版过的所有书籍的名字以及作者的姓名(三种写法,笔记中有两种写法) 2.手机以2开头的作者出版过的所有书籍名称以及出版社名称(三种写法,笔记中有1种写法) 1.聚 ...
- _utf8_encode _utf8_decode base64_encode base64_decode
const Base64 = { // private property _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv ...
- spring多数据源事务配置
项目中遇到多数据源问题, 对于每个数据源需要单独完成事务控制, 这里记录下具体实现方法 在spring配置文件中 定义两个数据源 <!-- 数据源定义(spring-jndi) --> ...
- Linux中的服务管理
RPM包默认安装的服务 查看已安装的服务: chkconfig --list 默认安装位置: /etc/init.d 启动脚本 /etc/sysconfig 初始化环境配置文件 /etc 配置文件位 ...
- linux下增加swap分区
Swap交换分区概念 什么是Linux swap space呢?我们先来看看下面两段关于Linux swap space的英文介绍资料: Linux divides its physical RAM ...