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 ...
随机推荐
- [HAOI2012] 容易题[母函数]
794. [HAOI2012] 容易题 ★★☆ 输入文件:easy.in 输出文件:easy.out 简单对比时间限制:1 s 内存限制:128 MB 秒 输入:easy.in 输出: ...
- n++ ++n
n = 100 + m++ n = 100 +m; ++m n = 100 + ++m; ++m; n = 100 +m ;
- 《从零开始学Swift》学习笔记(Day 52)——Cocoa错误处理模式
原创文章,欢迎转载.转载请注明:关东升的博客 Swift错误处理模式,在Swift 1.x和Swift 2.0是不同的两种模式. Swift 1.x代码错误处理模式采用Cocoa框架错误处理模式,到现 ...
- Log4j将不同Package的日志输出到不同的文件
转自:http://www.crazyant.net/1931.html 随着项目规模的越来越大,会不断的引入新的模块,不同的模块都会打印自己的日志,最后就造成日志根本没法查看,比如我自己的项目中,就 ...
- JmsTemplate 发送方式
---恢复内容开始--- 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 发现 系统运行后 创建 的并不是队列 ,而是Topic , 自己看下源码,发现在创 ...
- 关于Springboot中dubbo整合注意的误区(不对之处请指出)
这是我的客户端配置! 这是生产的配置, 首先注意一下 scan 我之前尝试这样的客户端配置 然后 果断客户端不能注册接口 @Reference(version="1.0") ...
- setlocale()函数测试当前语言的两个程序
http://www.cnblogs.com/cnyao/archive/2010/05/06/1729220.html setlocale()函数是用来配置地域信息的,原本以为这个也是windows ...
- Vue父组件调用子组件的方法
vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用,例如: 父组件: <template> <div @click ...
- DOM 属性操作
1 属性节点 2 attribute操作 3 value获取值操作 4 class的操作 5 指定CSS操作 1.属性节点 //获取文本节点的值 var divEle = document.getEl ...
- boost之操作系统相关
1.保存I/O流 下面这段代码cout会失效,原因是cout重定向之后失效. #include <iostream> #include <fstream> using name ...