bzoj 4066: 简单题 K-D树
题目大意:
题解
我们把每次的修改操作都当作二维平面上多了一个权值点
对于每组询问可以看做求一个矩形区域内的点权和
所以我们用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树的更多相关文章
- BZOJ 4066 简单题(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...
- bzoj 4066: 简单题 kd-tree
4066: 简单题 Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 234 Solved: 82[Submit][Status][Discuss] De ...
- BZOJ 4066 简单题 ——KD-Tree套替罪羊树
[题目分析] 直接x,y二维轮番划分,暴力即可. 套上替罪羊,打碎重构,对于时间复杂度有了保证. 写起来好麻烦,重构的技巧很棒! [代码] #include <cstdio> #inclu ...
- bzoj 4066 简单题——KDtree(带重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- bzoj 4066: 简单题
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #d ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- BZOJ 3687: 简单题 bitset
3687: 简单题 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 小呆开始研究集合论了,他 ...
- P5057 [CQOI2006]简单题(线段树)
果然简单题,5分钟紫题++ 代码 #include <cstdio> #include <algorithm> #include <cstring> using n ...
随机推荐
- An easy to use android color picker library
https://github.com/xdtianyu/ColorPicker
- Java结束线程的三种方法
线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁,不能再次start,只能重新建立新的线程对象,但有时run()方法是永远不会结束的.例如在程序中使用线程进行So ...
- 【PHP开发】用curl向https发请求时的35号错误
放了个假发现以前写的程序的模拟登陆不管用了,中间输出,发现curl向https发请求时没有返回数据,输出错误信息,得到: curl_errno($ch) -----> 35 curl_error ...
- WPF的ListView控件自定义布局用法实例
正文: 如何布局是在App.xaml中定义源码如下 <Application x:Class="CWebsSynAssistant.App" xmlns="ht ...
- SQLite集成与用法
本文转载至 http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/fra ...
- EasyPlayer RTSP Windows播放器D3D,GDI的几种渲染方式的选择区别
EasyPlayer-RTSP windows播放器支持D3D和GDI两种渲染方式,其中D3D支持格式如下: DISPLAY_FORMAT_YV12 DISPLAY_FORMAT_YUY2 DISPL ...
- JSON格式之GSON解析
JSON格式之GSON解析 最近在做websocket相关,项目需要JSON解析.相较之下感觉google的GSON解析不错. JAVA后台 Gson提供了fromJson()方法来实现从Json相关 ...
- 【WinForm】创建自定义控件(转)
转自:http://www.cnblogs.com/bomo/archive/2012/12/09/2810559.html 虽然VS为我们提供了很多控件可以使用,但有时候这些控件仍然不能满足我们的要 ...
- 九度OJ 1020:最小长方形 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6019 解决:2849 题目描述: 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在 ...
- 查看SqlServer安装的log文件
SqlServer安装时产生的log被保存在这个目录下: "%programfiles%\Microsoft SQL Server\[SQL_VERSION]\Setup Bootstrap ...