【bzoj4066】简单题 KD-tree
题目描述
|
命令 |
参数限制 |
内容 |
|
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 |
无 |
终止程序 |
输入
输出
样例输入
4
1 2 3 3
2 1 1 3 3
1 1 1 1
2 1 1 0 7
3
样例输出
3
5
题解
KD-tree
题目强制在线,所以不能离线分治;平面上问题,可以用KD-tree来解决。
具体来说大部分都是板子,只有查询时有点变化,类似于 线段树/平衡树 的区间查询,不断缩小范围。
然而这样裸上亲测会TLE,究其原因是KD-tree的退化。
所以每次加入一定数目的点后,我们需要将KD-tree重构,类似于朝鲜树(实际替罪羊树式重构更为高效,这里懒了)。
这样能够使得树高不会特别离谱,然后就可以过了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 500010
using namespace std;
struct data
{
int p[2] , maxn[2] , minn[2] , c[2] , w , sum;
}a[N];
int d , root;
bool cmp(data a , data b)
{
return a.p[d] == b.p[d] ? a.p[d ^ 1] < b.p[d ^ 1] : a.p[d] < b.p[d];
}
void pushup(int k , int s)
{
a[k].maxn[0] = max(a[k].maxn[0] , a[s].maxn[0]);
a[k].minn[0] = min(a[k].minn[0] , a[s].minn[0]);
a[k].maxn[1] = max(a[k].maxn[1] , a[s].maxn[1]);
a[k].minn[1] = min(a[k].minn[1] , a[s].minn[1]);
a[k].sum += a[s].sum;
}
int build(int l , int r , int now)
{
int mid = (l + r) >> 1;
d = now , nth_element(a + l , a + mid , a + r + 1 , cmp);
a[mid].maxn[0] = a[mid].minn[0] = a[mid].p[0];
a[mid].maxn[1] = a[mid].minn[1] = a[mid].p[1];
a[mid].sum = a[mid].w;
a[mid].c[0] = a[mid].c[1] = 0;
if(l < mid) a[mid].c[0] = build(l , mid - 1 , now ^ 1) , pushup(mid , a[mid].c[0]);
if(r > mid) a[mid].c[1] = build(mid + 1 , r , now ^ 1) , pushup(mid , a[mid].c[1]);
return mid;
}
void ins(int x)
{
int *t = &root;
d = 0;
while(*t) pushup(*t , x) , t = &a[*t].c[a[x].p[d] > a[*t].p[d]] , d ^= 1;
*t = x;
}
int query(int k , int x1 , int y1 , int x2 , int y2)
{
if(!k || a[k].maxn[0] < x1 || a[k].maxn[1] < y1 || a[k].minn[0] > x2 || a[k].minn[1] > y2) return 0;
if(a[k].maxn[0] <= x2 && a[k].maxn[1] <= y2 && a[k].minn[0] >= x1 && a[k].minn[1] >= y1) return a[k].sum;
int ans = 0;
if(a[k].p[0] >= x1 && a[k].p[0] <= x2 && a[k].p[1] >= y1 && a[k].p[1] <= y2) ans += a[k].w;
ans += query(a[k].c[0] , x1 , y1 , x2 , y2) + query(a[k].c[1] , x1 , y1 , x2 , y2);
return ans;
}
int main()
{
int opt , x1 , y1 , x2 , y2 , last = 0 , tot = 0;
scanf("%*d");
while(scanf("%d" , &opt) != EOF && opt != 3)
{
if(opt == 1)
{
tot ++ , scanf("%d%d%d" , &a[tot].p[0] , &a[tot].p[1] , &a[tot].w);
a[tot].p[0] ^= last , a[tot].p[1] ^= last , a[tot].w ^= last , a[tot].sum = a[tot].w;
a[tot].maxn[0] = a[tot].minn[0] = a[tot].p[0];
a[tot].maxn[1] = a[tot].minn[1] = a[tot].p[1];
ins(tot);
if(tot % 10000 == 0) root = build(1 , tot , 0);
}
else scanf("%d%d%d%d" , &x1 , &y1 , &x2 , &y2) , x1 ^= last , y1 ^= last , x2 ^= last , y2 ^= last , printf("%d\n" , last = query(root , x1 , y1 , x2 , y2));
}
return 0;
}
【bzoj4066】简单题 KD-tree的更多相关文章
- 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次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
- bzoj4066: 简单题 K-Dtree
bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- bzoj 4066: 简单题 K-D树
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...
- BZOJ4066 简单题(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 【kd-tree】bzoj4066 简单题
同p1176. #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ...
- bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 https://www.lydsy.com/JudgeOnline/problem.p ...
- 初涉k-d tree
听说k-d tree是一个骗分的好东西?(但是复杂度差评??? 还听说绍一的kdt常数特别小? KDT是什么 KDT的全称是k-degree tree,顾名思义,这是一种处理多维空间的数据结构. 例如 ...
随机推荐
- appium---adb通过wifi连接手机
前几天接到领导的安排,想要测试下apk的耗电量,可以通过手机adb命令进行监控手机电量的变化:但是这样如果通过USB连接手机的话,USB就会自动给手机进行充电,无法达到我们想要的结果,于是想到了通过w ...
- [solr 管理界面] - 索引数据删除
删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...
- 解决mysql8小时无连接自动断掉机制
windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 MySQL是一个小型关系型数据库管理系统,由于MySQL ...
- latex目录标题常用宏包说明与示例
http://blog.sina.com.cn/s/blog_5e16f1770100gyxn.html
- 微信小程序js学习心得体会
微信小程序js学习心得体会 页面控制的bindtap和catchtap 用法,区别 <button id='123' data-userDate='100' bindtap='tabMessag ...
- 二十五、MySQL 索引
MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...
- form中 单选框 input[type="radio"] 分组
在form中有时候需要给单选框分组,这种情况下 可以通过给单选框设置相同的name来进行分组: <html> <head> <title> </title&g ...
- python笔记-list列表的方法
#!usr/bin/python # -*- coding: utf-8 -*- # 存储5个人的年龄,求他们的平均年龄 age1 = 18 age2 = 15 age3 = 38 age4 = 20 ...
- L1-039 古风排版 (20 分)
L1-039 古风排版 (20 分) 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<),是每一列的字符数.第 ...
- 版本控制之GitHub — — 第一步的理解
GitHub是时下最流行的版本控制的一门“技术”,此之前svn(subversion)也是同样的作用. 至于版本控制:Git是分布式的,而svn是中心式的(或者叫集中式的)版本控制系统,这是两者之间理 ...