bzoj 4066: 简单题 kd-tree
4066: 简单题
Time Limit: 50 Sec Memory Limit: 20 MB
Submit: 234 Solved: 82
[Submit][Status][Discuss]
Description
|
命令 |
参数限制 |
内容 |
|
1 x y A |
1<=x,y<=N,A是正整数 |
将格子x,y里的数字加上A |
|
2 x1 y1 x2 y2 |
1<=x1<= x2<=N 1<=y1<= y2<=N |
输出x1 y1 x2 y2这个矩形内的数字和 |
|
3 |
无 |
终止程序 |
Input
Output
Sample Input
1 2 3 3
2 1 1 3 3
1 1 1 1
2 1 1 0 7
3
Sample Output
5
HINT
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 300100
#define MAXT MAXN
#define INF 0x3f3f3f3f
struct kdt_node
{
int lc,rc;
int xmn,xmx;
int ymn,ymx;
int sum;
int x,y;
}kdt[MAXT];
int topt=;
inline void update(int now)
{
kdt[now].xmn=min(kdt[now].x,min(kdt[kdt[now].lc].xmn,kdt[kdt[now].rc].xmn));
kdt[now].xmx=max(kdt[now].x,max(kdt[kdt[now].lc].xmx,kdt[kdt[now].rc].xmx));
kdt[now].ymn=min(kdt[now].y,min(kdt[kdt[now].lc].ymn,kdt[kdt[now].rc].ymn));
kdt[now].ymx=max(kdt[now].y,max(kdt[kdt[now].lc].ymx,kdt[kdt[now].rc].ymx));
}
int x,y,v;
void Add_kdt(int &now,int d=)
{
if (!now)
{
now=++topt;
kdt[now].x=x;
kdt[now].y=y;
update(now);
}
kdt[now].sum+=v;
if (kdt[now].x==x && kdt[now].y==y)return ;
if (!d)
{
if (x<=kdt[now].x)
{
Add_kdt(kdt[now].lc,-d);
}else
{
Add_kdt(kdt[now].rc,-d);
}
}else
{
if (y<=kdt[now].y)
{
Add_kdt(kdt[now].lc,-d);
}else
{
Add_kdt(kdt[now].rc,-d);
}
}
update(now);
}
int x1,x2,y1,y2;
int Query_kdt(int now,int d)
{
if (!now)return ;
if (kdt[now].xmn>=x1 && kdt[now].xmx<=x2 && kdt[now].ymn>=y1 && kdt[now].ymx<=y2)
return kdt[now].sum;
int ret=;
if (kdt[now].x<=x2 && kdt[now].x>=x1 && kdt[now].y<=y2 && kdt[now].y>=y1)ret+=kdt[now].sum-kdt[kdt[now].lc].sum-kdt[kdt[now].rc].sum;
if (!d)
{
if (x1<=kdt[now].x)
ret+=Query_kdt(kdt[now].lc,-d);
if (x2>kdt[now].x)
ret+=Query_kdt(kdt[now].rc,-d);
}else
{
if (y1<=kdt[now].y)
ret+=Query_kdt(kdt[now].lc,-d);
if (y2>kdt[now].y)
ret+=Query_kdt(kdt[now].rc,-d);
}
return ret;
} int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n,m;
scanf("%d",&n);
int root=;
int opt=;
int lastans=;
kdt[].xmn=kdt[].ymn=INF;
kdt[].xmx=kdt[].ymx=-INF;
while (true)
{
scanf("%d",&opt);
if (opt==)
{
scanf("%d%d%d",&x,&y,&v);
x^=lastans;y^=lastans;v^=lastans;
Add_kdt(root,);
}else if (opt==)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1^=lastans;x2^=lastans;y1^=lastans;y2^=lastans;
printf("%d\n",lastans=Query_kdt(root,));
}else
{
break;
}
}
}
bzoj 4066: 简单题 kd-tree的更多相关文章
- bzoj 4066: 简单题 K-D树
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...
- BZOJ4066:简单题(K-D Tree)
Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...
- P4148 简单题 k-d tree
思路:\(k-d\ tree\) 提交:2次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...
- BZOJ 4066 简单题(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...
- bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 https://www.lydsy.com/JudgeOnline/problem.p ...
- bzoj 4066 简单题——KDtree(带重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...
- BZOJ 4066 简单题 ——KD-Tree套替罪羊树
[题目分析] 直接x,y二维轮番划分,暴力即可. 套上替罪羊,打碎重构,对于时间复杂度有了保证. 写起来好麻烦,重构的技巧很棒! [代码] #include <cstdio> #inclu ...
- 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] ...
随机推荐
- Day07 - Python 网络编程 Socket
1. Python 网络编程 Python 提供了两个级别访问网络服务: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口 ...
- eplice下载地址
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/kepler/R/eclips ...
- 转:oracle ebs po模块一揽子采购协议小结
转自:http://yedward.net/?id=193 oracle ebs po模块一揽子采购协议小结 本文总结oracle ebs采购订单(po)模块一揽子采购协议的相关知识,总结如下: 1. ...
- 趣拍proguard配置
# Add project specific ProGuard rules here.# By default, the flags in this file are appended to flag ...
- Virtual Studio C++ Version Macro - _MSC_VER
MSVC++ (Visual Studio ) MSVC++ (Visual Studio ) MSVC++ (Visual Studio ) MSVC++ (Visual Studio ) MSVC ...
- DLL中导出STL模板类的问题
接上一篇. 上一篇的dll在编译过程中一直有一个警告warning C4251: ‘CLASS_TEST::m_structs’ : class ‘std::vector<_Ty>’ ne ...
- 利用Highcharts制作web图表学习(二)
最近中海油的项目需要用到图表展示数据,最近还是一直边学习边开发,今天做了一个展示,炼化厂加热炉效率展示的柱状图,把代码贴出来,大家指点一下互相学习,我是通过数组给Highcharts绑定的值, ...
- C#删除微信自定义菜单
删除 string access_token = "你的token"; string posturl = "https://api.weixin.qq.com/cgi-b ...
- OC与Swift的区别一(文件结构)
1.文件后缀名 oc的文件后缀名为:头文件.h 主体文件.m swift文件后缀名为:.swift 2. 代码分隔符 oc中使用分号;作为代码分隔符 swift中无需使用代码分隔符,以行作为代码分隔 ...
- 国庆第六日(2014年10月6日11:51:15),node-webkit,理财产品
(1)node-webkit:一篇很好的入门文章.入门.系列. 在window下的打包和运行.大漠的一篇讲解文章 . (2)lighttable: 官网. (3)现在的理财产品,雨后春笋般冒出:宝点网 ...