洛谷 P4148 简单题 KD-Tree 模板题
Code:
//洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
void setIO(string a){ freopen((a+".in").c_str(),"r",stdin);} #define maxn 500007 int root,d,tot;
struct Data
{
int ch[2],sum,w,minv[2],maxv[2],p[2];
}node[maxn];
bool cmp(Data i,Data j)
{
return i.p[d] == j.p[d]? i.p[d^1] < j.p[d^1]: i.p[d]<j.p[d];
}
bool isout(int k,int x1,int y1,int x2,int y2)
{
if(node[k].maxv[0]<x1||node[k].minv[0]>x2||node[k].maxv[1]<y1||node[k].minv[1]>y2) return 1;
return 0;
}
bool isin(int k,int x1,int y1,int x2,int y2)
{
if(node[k].maxv[0]<=x2 && node[k].minv[0]>=x1 && node[k].maxv[1]<=y2 && node[k].minv[1]>=y1) return 1;
return 0;
}
void pushup(int x,int o)
{
node[x].minv[0]=min(node[x].minv[0],node[o].minv[0]);
node[x].maxv[0]=max(node[x].maxv[0],node[o].maxv[0]);
node[x].minv[1]=min(node[x].minv[1],node[o].minv[1]);
node[x].maxv[1]=max(node[x].maxv[1],node[o].maxv[1]);
node[x].sum+=node[o].sum;
}
int build(int l,int r,int o)
{
int mid=(l+r)>>1;
d=o, nth_element(node + l, node + mid, node + r + 1, cmp);
node[mid].minv[0] = node[mid].maxv[0] = node[mid].p[0];
node[mid].minv[1] = node[mid].maxv[1] = node[mid].p[1];
node[mid].sum = node[mid].w;
node[mid].ch[0]=node[mid].ch[1]=0;
if(l < mid) node[mid].ch[0] = build(l, mid-1, o^1), pushup(mid, node[mid].ch[0]);
if(r > mid) node[mid].ch[1] = build(mid + 1, r , o^1), pushup(mid, node[mid].ch[1]);
return mid;
}
int query(int k,int x1,int y1,int x2,int y2)
{
if(!k||isout(k,x1,y1,x2,y2)) return 0;
if(isin(k,x1,y1,x2,y2)) return node[k].sum;
int ans=0;
if(x1<=node[k].p[0]&&x2>=node[k].p[0]&&y1<=node[k].p[1]&&y2>=node[k].p[1]) ans+=node[k].w;
ans+=query(node[k].ch[0],x1,y1,x2,y2)+query(node[k].ch[1],x1,y1,x2,y2);
return ans;
}
void insert(int x)
{
int *t = &root;
d = 0;
while(*t) pushup(*t , x) , t = &node[*t].ch[ node[x].p[d] > node[*t].p[d] ] , d ^= 1;
*t = x;
}
int main()
{
//setIO("input");
int n,lastans=0,opt;
scanf("%d",&n);
while(1)
{
scanf("%d",&opt);
if(opt == 1)
{
int x,y,a;
scanf("%d%d%d",&x,&y,&a),x^=lastans,y^=lastans,a^=lastans;
++tot;
node[tot].p[0]=x,node[tot].p[1]=y;
node[tot].maxv[0]=node[tot].minv[0]=x;
node[tot].maxv[1]=node[tot].minv[1]=y;
node[tot].w=node[tot].sum=a;
insert(tot);
if(tot%10000==0) root=build(1,tot,0);
}
if(opt == 2)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2),x1^=lastans,y1^=lastans,x2^=lastans,y2^=lastans;
lastans=query(root,x1,y1,x2,y2);
printf("%d\n",lastans);
}
if(opt == 3) break;
}
return 0;
}
洛谷 P4148 简单题 KD-Tree 模板题的更多相关文章
- 洛谷 P4148 简单题 解题报告
P4148 简单题 题意 维护单点加与矩形求和,强制在线 说明 \(n\le 500000,m\le 200000\),\(4000ms / 20MB\) kd-tree 复杂度我不懂 是一颗平衡树, ...
- 洛谷OJ U552 守墓人 线段树模板题
题目描述 Description 在一个荒凉的墓地上 有一个令人尊敬的守墓人, 他看守的墓地从来 没有被盗过, 所以人们很放心的把自己的先人的墓 安顿在他那 守墓人能看好这片墓地是必然而不是偶然... ...
- 洛谷 P3367 并查集 【模板题】
题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1 ...
- BZOJ 2648 / 2716 K-D Tree 模板题
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂
洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...
- k-d tree模板练习
1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...
- 洛谷试炼场-简单数学问题-P1088 火星人
洛谷试炼场-简单数学问题 A--P1088 火星人 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法 ...
- 洛谷试炼场-简单数学问题-P1403 [AHOI2005]-因数
洛谷试炼场-简单数学问题 P1403 [AHOI2005]约数研究 Description 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机"Samuel I ...
- DP【洛谷P1704】 寻找最优美做题曲线
[洛谷P1704] 寻找最优美做题曲线 题目背景 nodgd是一个喜欢写程序的同学,前不久(好像还是有点久了)洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情, ...
随机推荐
- 132.try throw catch介绍
#include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...
- COGS 2479 奇怪的姿势卡♂过去 (bitset+折半)
思路: 此题显然是CDQ套CDQ套树套树 (然而我懒) 想用一种奇怪的姿势卡过去 就出现了以下解法 5w*5w/8的bitset hiahiahia 但是空间会爆怎么办啊- 折半~ 变成5w*2.5w ...
- WPF XAML
xmlns 在xml中专门用于声明名字控件, xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 是 ...
- ing在写作中到底怎么用
改关静留的作业中我还得改英文摘要.于是有个地方用了ing.顺便查了一下. http://www.yygrammar.com/Article/201408/3677.html -ing分词用作状语时有九 ...
- 【原创】JMS发布者订阅者【异步接收消息】
发布订阅模式和PTP方式不同之处为后者依赖于一个Topic话题: package com.thunisoft.jms.mine.topic; import java.util.HashMap; imp ...
- ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- 一个php处理图片裁剪,压缩,水印的小代码
插件地址:https://github.com/cigua/imagefilter
- BZOJ 2555 SubString(LCT+后缀树)
喜闻乐见的LCT+SAM 此题要求动态插入,直接上后缀树.然后询问其实就是求一个节点的子树后缀结束节点的个数. 因为建立后缀树需要插入和删除,就直接上LCT.每次加入一个点,把它到根的路径加一 (现在 ...
- POJ 2311 Cutting Game(SG函数)
题目描述 意思就是说两个人轮流剪纸片,直到有一个人剪出1*1的方格就算这个人赢了.然后给出纸片的长和宽,求先手会赢还是会输 (1<=n,m<=200) 题解 看了一眼,这不是裸的SG吗 啪 ...
- windows下命令行复制
在CMD命令提示符窗口中点击鼠标右键,选择“标记”选项,然后按住鼠标左键不动,拖动鼠标标记想要复制的内容.标记完成以后请按键盘上的“回车”键