题目大意:

http://www.lydsy.com/JudgeOnline/problem.php?id=4066

题解

我们把每次的修改操作都当作二维平面上多了一个权值点

对于每组询问可以看做求一个矩形区域内的点权和

所以我们用k-D Tree直接搜就好了

#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 160010;
const int lim_siz = 2000;
int split[maxn],now;
int dem = 2;
struct Node{
int pos[2],val;
int minn[2],maxx[2],sum;
Node *ch[2];
void update(){
for(int d=0;d<dem;++d) minn[d] = min(pos[d],min(ch[0]->minn[d],ch[1]->minn[d]));
for(int d=0;d<dem;++d) maxx[d] = max(pos[d],max(ch[0]->maxx[d],ch[1]->maxx[d]));
sum = val + ch[0]->sum + ch[1]->sum;
} }*null,*root,*op;
Node T[maxn];
inline bool cmp(const Node &a,const Node &b){
return a.pos[split[now]] < b.pos[split[now]];
}
inline void init(){
null = &T[0];
null->ch[0] = null->ch[1] = null;
null->val = 0;
for(int d=0;d<dem;++d){
null->pos[d] = 0;
null->minn[d] = 0x3f3f3f3f;
null->maxx[d] = -0x3f3f3f3f;
}root = null;
}
Node* build(int l,int r,int s){
if(l > r) return null;
int mid = (l+r)>> 1;
split[now = mid] = s % dem;
nth_element(T+l,T+mid,T+r+1,cmp);
Node *p = &T[mid];
p->ch[0] = build(l,mid-1,s+1);
p->ch[1] = build(mid+1,r,s+1);
p->update();return p;
}
int sav[maxn][2],sav_cnt,cnt;
int val[maxn],cmd,X1,Y1,X2,Y2,x;
int query(Node *p){
if(p == null) return 0;
if(p->minn[0] >= X1 && p->minn[1] >= Y1
&& p->maxx[0] <= X2 && p->maxx[1] <= Y2)
return p->sum;
else if(p->maxx[1] < Y1 || p->maxx[0] < X1
|| p->minn[0] > X2 || p->minn[1] > Y2)
return 0;
if( p->pos[0] >= X1 && p->pos[1] >= Y1
&& p->pos[0] <= X2 && p->pos[1] <= Y2)
return p->val + query(p->ch[0]) + query(p->ch[1]);
return query(p->ch[0]) + query(p->ch[1]);
}
int main(){
init();
int n;read(n);//read(n);
int lastans = 0;
while(1){
read(cmd);
if(cmd == 3) break;
if(cmd == 1){
read(X1);read(Y1);read(x);
X1 ^= lastans;Y1 ^= lastans;x ^= lastans;
sav[++sav_cnt][0] = X1;
sav[sav_cnt][1] = Y1;
val[sav_cnt] = x;
if(sav_cnt == lim_siz){
for(int i=1;i<=sav_cnt;++i){
T[cnt+i].minn[0] = T[cnt+i].maxx[0] = T[cnt+i].pos[0] = sav[i][0];
T[cnt+i].minn[1] = T[cnt+i].maxx[1] = T[cnt+i].pos[1] = sav[i][1];
T[cnt+i].val = val[i];
}root = build(1,cnt+=sav_cnt,1);
sav_cnt = 0;
}
}else{
read(X1);read(Y1);read(X2);read(Y2);
X1 ^= lastans;Y1 ^= lastans;
X2 ^= lastans;Y2 ^= lastans;
int ans = 0;
for(int i=1;i<=sav_cnt;++i){
if(sav[i][0] >= X1 && sav[i][1] >= Y1
&& sav[i][0] <= X2 && sav[i][1] <= Y2)
ans += val[i];
}
ans += query(root);
printf("%d\n",lastans = ans);
}
}
getchar();getchar();
return 0;
}

bzoj 4066: 简单题 K-D树的更多相关文章

  1. BZOJ 4066 简单题(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...

  2. bzoj 4066: 简单题 kd-tree

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

  3. BZOJ 4066 简单题 ——KD-Tree套替罪羊树

    [题目分析] 直接x,y二维轮番划分,暴力即可. 套上替罪羊,打碎重构,对于时间复杂度有了保证. 写起来好麻烦,重构的技巧很棒! [代码] #include <cstdio> #inclu ...

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

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

  5. BZOJ 2683 简单题 cdq分治+树状数组

    题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...

  6. bzoj 4066: 简单题

    #include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #d ...

  7. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  8. BZOJ 3687: 简单题 bitset

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 小呆开始研究集合论了,他 ...

  9. P5057 [CQOI2006]简单题(线段树)

    果然简单题,5分钟紫题++ 代码 #include <cstdio> #include <algorithm> #include <cstring> using n ...

随机推荐

  1. 基于mysql本身的主从复制

    mysql的主从复制在我理解而言就是一个主数据库进行增删改操作的时候会自动将数据写入与之关联的从数据库中.这个从数据库可以是一个也可以是多个.(刚开始理解的时候觉得是同一个数据库服务下的不同的data ...

  2. springboot 项目中控制台打印日志以及每天生成日志文件

    1.控制台打印sql语句 只要在application.properties 中加入<configuration  scan="true" scanPeriod=" ...

  3. G1垃圾收集或者Java中垃圾收集的名词收集

    HotSpot WTF,热壶?我他奶奶的还热火呢,Heat,you know? 总之HotSpot是一种遵循java虚拟机规范的一种实现啦,当时并不是Sun公司搞出来的,而是另外一家公司,后来被Sun ...

  4. Hive报错:Failed with exception Unable to rename

    之前也安装过hive,操作过无数,也没发现什么错误,今天因为之前安装的hadoop不能用了,不知道为什么,老是提示node 0,所以重新安装了hadoop和hive.安装完测试hive创建表也没发现什 ...

  5. 如何利用JQuery获取iframe内联框架对象?

    parent.$("#iframeID").get(0).contentWindow; 父.$("选择器").get(0).contentWindow; get ...

  6. 计算机器内存数量+引入和显示ARDS成员

    [1]README 1.1) 本代码在于读取内存中多个 内存段的地址范围描述符结构体(ARDS),有多少个内存段可以用: 1.2) source code and images in the blog ...

  7. C#中的let字句应用示例

    一.应用场景 在查询表达式中,存储子表达式的结果有时很有用,这样可以在随后的子句中使用. 可以使用 let 关键字完成这一工作,该关键字可以创建一个新的范围变量,并且用您提供的表达式的结果初始化该变量 ...

  8. java log4j 日志文件

    开发中经常会用到log日志文件,根据业务需要可能不产生很大日志文件给维护和[排错带来了麻烦.所以我们希望能够每天或每个月产生一个日志文件,这样文件不至于过大. 或者根据日志文件大小来判断,超过规定大小 ...

  9. GS(道具,帮会)定时存储

    //最近数据库存储做了重大改变,数据库内部的回头再说,先看看GS这边的 .现在感觉数据库的状态将请求包放入命令队列中,以前是全部放进去,这样让其他的数据库操作不会随着数据库定时器而变慢,GS线程去驱动 ...

  10. 【BZOJ4383】[POI2015]Pustynia 线段树优化建图

    [BZOJ4383][POI2015]Pustynia Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r ...