传送门

KDTree

修改权值当做插入节点,不平衡就暴力重构,询问的时候判断当前节点代表的矩形是否在询问的矩形的,是的话返回答案,相离返回0,否则的话判断当前点是否在矩形内,然后继续递归下去

//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=2e5+5;
struct point{int v[2],w;}pool[N];
struct node{int mn[2],mx[2],sum,l,r,sz;point tp;}tr[N];
int n,m,K,rt,top,cnt,st[N],ans;
inline bool operator <(const point &a,const point &b){return a.v[K]<b.v[K];}
inline int newnode(){return top?st[top--]:++cnt;}
void upd(int p){
int l=tr[p].l,r=tr[p].r;
fp(i,0,1){
tr[p].mn[i]=tr[p].mx[i]=tr[p].tp.v[i];
if(l)cmin(tr[p].mn[i],tr[l].mn[i]),cmax(tr[p].mx[i],tr[l].mx[i]);
if(r)cmin(tr[p].mn[i],tr[r].mn[i]),cmax(tr[p].mx[i],tr[r].mx[i]);
}tr[p].sum=tr[l].sum+tr[r].sum+tr[p].tp.w,tr[p].sz=tr[l].sz+tr[r].sz+1;
}
int build(int l,int r,int k){
if(l>r)return 0;int mid=(l+r)>>1,p=newnode();
K=k,nth_element(pool+l,pool+mid,pool+r+1),tr[p].tp=pool[mid];
tr[p].l=build(l,mid-1,k^1),tr[p].r=build(mid+1,r,k^1);
upd(p);return p;
}
void pia(int p,int sum){
if(tr[p].l)pia(tr[p].l,sum);
pool[sum+tr[tr[p].l].sz+1]=tr[p].tp,st[++top]=p;
if(tr[p].r)pia(tr[p].r,sum+tr[tr[p].l].sz+1);
}
void ck(int &p,int k){
if(tr[p].sz*0.75<tr[tr[p].l].sz||tr[p].sz*0.75<tr[tr[p].r].sz)
pia(p,0),p=build(1,tr[p].sz,k);
}
void ins(int &p,point res,int k){
if(!p)return (void)(p=newnode(),tr[p].l=tr[p].r=0,tr[p].tp=res,upd(p));
res.v[k]<=tr[p].tp.v[k]?ins(tr[p].l,res,k^1):ins(tr[p].r,res,k^1);
upd(p),ck(p,k);
}
inline bool in(int x1,int y1,int x2,int y2,int X1,int Y1,int X2,int Y2){
return x1<=X1&&y1<=Y1&&x2>=X2&&y2>=Y2;
}
inline bool out(int x1,int y1,int x2,int y2,int X1,int Y1,int X2,int Y2){
return x1>X2||x2<X1||y1>Y2||y2<Y1;
}
int query(int p,int x1,int y1,int x2,int y2){
if(!p)return 0;int res=0;
if(in(x1,y1,x2,y2,tr[p].mn[0],tr[p].mn[1],tr[p].mx[0],tr[p].mx[1]))return tr[p].sum;
if(out(x1,y1,x2,y2,tr[p].mn[0],tr[p].mn[1],tr[p].mx[0],tr[p].mx[1]))return 0;
if(in(x1,y1,x2,y2,tr[p].tp.v[0],tr[p].tp.v[1],tr[p].tp.v[0],tr[p].tp.v[1]))res+=tr[p].tp.w;
res+=query(tr[p].l,x1,y1,x2,y2)+query(tr[p].r,x1,y1,x2,y2);return res;
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();int op,x1,y1,x2,y2;
while(true){
op=read();if(op==3)break;
if(op==1)ins(rt,(point){read()^ans,read()^ans,read()^ans},0);
else{
x1=read()^ans,y1=read()^ans,x2=read()^ans,y2=read()^ans;
ans=query(rt,x1,y1,x2,y2),print(ans);
}
}return Ot(),0;
}

P4148 简单题(KDTree)的更多相关文章

  1. 洛谷 P4148 简单题 KD-Tree 模板题

    Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...

  2. 洛谷 P4148 简单题 解题报告

    P4148 简单题 题意 维护单点加与矩形求和,强制在线 说明 \(n\le 500000,m\le 200000\),\(4000ms / 20MB\) kd-tree 复杂度我不懂 是一颗平衡树, ...

  3. bzoj 4066: 简单题 kd-tree

    4066: 简单题 Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 234  Solved: 82[Submit][Status][Discuss] De ...

  4. 【BZOJ4066】简单题 KDtree

    [BZOJ4066]简单题 Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y& ...

  5. bzoj4066: 简单题 K-Dtree

    bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...

  6. luogu P4148 简单题

    传送门 这题真简单,直接把\(CDQ\)给ban掉了 其实数据范围比较小可以直接二维树状数组,我们看数据范围,发现点的个数比N还小,可以考虑用一些奇怪的数据结构 说的就是你,\(KD tree\) \ ...

  7. Luogu P4148 简单题(K-D Tree)

    题面 题解 因为强制在线,所以我们不能$cdq$分治,所以考虑用$KDT$,$KDT$维护一个矩阵,然后询问的时候如果当前矩形在询问区间内,直接记贡献,否则判断当前点是否在矩阵内,然后左右分别递归下去 ...

  8. bzoj 4066 简单题——KDtree(带重构)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...

  9. 【bzoj4066】简单题 KD-tree

    题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...

随机推荐

  1. Es首页

    https://www.elastic.co/guide/en/elasticsearch/reference/index.html

  2. Codeforces 559A(计算几何)

    A. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. markdown八条基础语法

    1.空行 答:使用全角打出空格,之后再换行就可以打出空行了 2.标题 答:#表示标题,#表示一级标题,字号最大,一共有六级标题 3.列表 答:- 无序列表,1. 有序列表,注意和文本之间有空格 4.链 ...

  4. poj——2771 Guardian of Decency

    poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   ...

  5. 实现浏览器兼容的innerText

    今天学习到了FF不支持innerText,而IE.chrome.Safari.opera均支持innerText. 为了各个浏览器能兼容innerText,必须对js做一次封装. 为啥能实现兼容呢?原 ...

  6. Servlet的Service方法和doget 和 dopost方法的区别,常见的错误解析

    package com.sxt.in; import java.io.IOException; import javax.servlet.ServletException; import javax. ...

  7. VB程序逆向反汇编常见的函数(修改版)

    VB程序逆向常用的函数 1) 数据类型转换: a) __vbaI2Str    将一个字符串转为8 位(1个字节)的数值形式(范围在 0 至 255 之间) 或2 个字节的数值形式(范围在 -32,7 ...

  8. js:简单的拖动效果

    效果演示:https://jsfiddle.net/dwqs/b5ywws9f/embedded/result/ html: <div class="wrap"> &l ...

  9. Linux-百度云之AccleriderMini使用

    打开命令行,然后输入: sudo apt-get install mono-devel 最后安装完成时,运行: 示例:mono xx.exe 实例:mono accleridaMini.exe 最后成 ...

  10. 如何离线分析Kafka海量业务消息?1分钟快速为您支招

    场景介绍 说起Kafka,许多使用者对它是又爱又恨.Kafka是一种分布式的.基于发布/订阅的消息系统,其极致体验让人欲罢不能,但操心的运维.复杂的安全策略.可靠性易用性的缺失等,仍需要使用者付出诸多 ...