bzoj4066
KD-tree
强制在线就不能愉快的做这道题了。
我们用KD-tree维护平面上的点,这样建出来的树高大概是log,复杂度过得去,但是插入过多会使树深很深,这样就能卡死,那么我们每个10000次插入就重构一次。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + ;
int n, root, cnt, m = , d, last;
struct data {
int x, y, mx_x, mn_x, mx_y, mn_y, lc, rc, sum, val;
bool friend operator < (const data &a, const data &b) {
if(d == ) return a.x == b.x ? a.y < b.y : a.x < b.x;
if(d == ) return a.y == b.y ? a.x < b.x : a.y < b.y;
}
bool friend operator == (const data &a, const data &b) {
return a.x == b.x && a.y == b.y;
}
} a[N], b[N];
inline void read(int &x)
{
x = ;
int f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << ) + (x << ) + c - ''; c = getchar(); }
x *= f;
}
bool in(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2)
{
return X1 >= x1 && Y1 >= y1 && x2 >= X2 && y2 >= Y2;
}
bool out(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2)
{
return x1 > X2 || x2 < X1 || y1 > Y2 || y2 < Y1;
}
void update(int x)
{
a[x].mn_x = min(a[x].x, min(a[a[x].lc].mn_x, a[a[x].rc].mn_x));
a[x].mx_x = max(a[x].x, max(a[a[x].lc].mx_x, a[a[x].rc].mx_x));
a[x].mn_y = min(a[x].y, min(a[a[x].lc].mn_y, a[a[x].rc].mn_y));
a[x].mx_y = max(a[x].y, max(a[a[x].lc].mx_y, a[a[x].rc].mx_y));
a[x].sum = a[x].val + a[a[x].lc].sum + a[a[x].rc].sum;
}
int build(int l, int r, int D)
{
if(l > r) return ;
d = D;
int mid = (l + r) >> ;
nth_element(b + l, b + mid, b + r + );
a[mid] = b[mid];
a[mid].lc = build(l, mid - , D ^ );
a[mid].rc = build(mid + , r, D ^ );
update(mid);
return mid;
}
void insert(int &x, const data &tmp, int D)
{
d = D;
if(!x)
{
x = ++cnt;
a[x].x = a[x].mn_x = a[x].mx_x = tmp.x;
a[x].y = a[x].mn_y = a[x].mx_y = tmp.y;
a[x].sum = a[x].val = tmp.val;
return;
}
if(a[x] == tmp)
{
a[x].val += tmp.val;
a[x].sum += tmp.val;
return;
}
if(tmp < a[x]) insert(a[x].lc, tmp, D ^ );
else insert(a[x].rc, tmp, D ^ );
update(x);
}
int query(int x, int x1, int y1, int x2, int y2)
{
if(!x) return ;
int ret = ;
if(in(x1, y1, x2, y2, a[x].mn_x, a[x].mn_y, a[x].mx_x, a[x].mx_y)) return a[x].sum;
if(out(x1, y1, x2, y2, a[x].mn_x, a[x].mn_y, a[x].mx_x, a[x].mx_y)) return ;
if(in(x1, y1, x2, y2, a[x].x, a[x].y, a[x].x, a[x].y)) ret += a[x].val;
ret += query(a[x].lc, x1, y1, x2, y2) + query(a[x].rc, x1, y1, x2, y2);
return ret;
}
int main()
{
// freopen("bzoj_4066.in", "r", stdin);
// freopen("bzoj_4066.out", "w", stdout);
a[].mn_x = 1e9;
a[].mx_x = -1e9;
a[].mn_y = 1e9;
a[].mx_y = -1e9;
b[].mn_x = 1e9;
b[].mx_x = -1e9;
b[].mn_y = 1e9;
b[].mx_y = -1e9;
read(n);
while()
{
int opt, x1, y1, x2, y2;
data tmp;
read(opt);
if(opt == )
{
read(tmp.x);
read(tmp.y);
read(tmp.val);
tmp.x ^= last;
tmp.y ^= last;
tmp.val ^= last;
insert(root, tmp, );
if(cnt == m)
{
for(int i = ; i <= cnt; ++i) b[i] = a[i];
root = build(, cnt, );
m += ;
}
}
if(opt == )
{
read(x1);
read(y1);
read(x2);
read(y2);
x1 ^= last;
x2 ^= last;
y1 ^= last;
y2 ^= last;
printf("%d\n", last = query(root, x1, y1, x2, y2));
}
if(opt == ) break;
}
// fclose(stdin);
// fclose(stdout);
return ;
}
bzoj4066的更多相关文章
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
- BZOJ4066 简单题(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 【bzoj4066】 简单题
http://www.lydsy.com/JudgeOnline/problem.php?id=4066 (题目链接) 题意 维护一个矩阵,两个操作,给某一个元素加上A,求其中一个子矩阵的元素之和.强 ...
- 【BZOJ4066】简单题(KD-Tree)
[BZOJ4066]简单题(KD-Tree) 题面 BZOJ 题解 如果这题不卡空间,并且不强制在线的话 显然可以用\(CDQ\)分治做 但是它又卡空间又强制在线,于是我们欢快的来用\(KD-Tree ...
- 【kd-tree】bzoj4066 简单题
同p1176. #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ...
- 【BZOJ4066】简单题 KDtree
[BZOJ4066]简单题 Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y& ...
- KD-Tree复习笔记(BZOJ1941 & BZOJ2648 & BZOJ4066)
快一年了都没碰到什么必须用KDT的题目导致模板完全忘光了,重新复习了一下. K_Dimention_Tree是一种用来处理二维以上问题的数据结构(OI中一般都是二维),本质是二维启发式估价函数实现剪枝 ...
- 【转】BZOJ4066(kdtree)(占位)
https://www.cnblogs.com/OYzx/p/5506468.html BZOJ2863:(允许离线) 题目大意:给定一个n*n的矩形,以及若干个操作,操作有如下两种: 1.给矩形的( ...
- [bzoj4066/2683]简单题_KD-Tree
简单题 bzoj-4066 题目大意:n*n的棋盘,开始为均为0,支持:单点加权值,查询矩阵权值和,强制在线. 注释:$1\le n\le 5\cdot 10^5$,$1\le m \le 2\cdo ...
- bzoj4066: 简单题 K-Dtree
bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...
随机推荐
- vue2.0 + vux (六)NewsList 资讯页 及 NewsDetail 资讯详情页
设置代理,避免出现跨域问题 /*设置代理,避免出现跨域问题*/ proxyTable: { '/api':{ target: 'https://www.oschina.net/action/apiv2 ...
- python(36)- 测试题
1.8<<2等于? 32 “<<”位运算 264 132 64 32 16 8 4 2 1 原始位置 0 0 0 0 0 1 0 0 0 想左位移2位 0 0 0 1 0 0 ...
- initializer_list、初始化列表、列表初始化
什么是列表初始化 使用一个花括号来初始化变量,表现形式如下: std::vector<int>a{1,2,3,4,5}; 或者 std::vector<int>a = {1,2 ...
- 如何与强势的人相处zz
要和强势的人相处良好,须知道强势的人有两个很显著的特点:一.以自我观点为中心.二.怕别人否定自己.强势的主要作用也有两个:一.支配别人.二.掩盖自卑. 首先,要区分一下强势的人和特立独行的人,这两类人 ...
- npm 淘宝设置代理
直接安装cnpm导致无限索引,因此直接使用代理 方法一: 直接在当前用户文件夹下,npmrc 文件上直接设置代理:registry=https://registry.npm.taobao.org 方法 ...
- 李洪强iOS开发之带placeHolder的Textview
李洪强iOS开发之带placeHolder的Textview 01 - 创建工过程,定义全局属性,遵守textview的代理协议 02 - 添加一个textview和一个label 03 - 实现 ...
- ubuntun16.04不支持intel的最新网卡,升级到17.10后解决
新买的神舟战神电脑.装了ubuntu16.04版本.但是安装后无线网卡无法使用无线网卡型号:是intel的一款网卡02:00.0 Network controller [0280]: Intel Co ...
- 1.JavaScript:写入 HTML 输出
①JavaScript 是可插入HTML页面的编程代码 ②JavaScript插入HTML页面后,可有所有的现代浏览器执行 ※提示:您只能在 HTML 输出中使用 document.write.如果您 ...
- 04-树4 是否同一棵二叉搜索树(25 point(s)) 【Tree】
04-树4 是否同一棵二叉搜索树(25 point(s)) 给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到.例如分别按照序列{2, 1, 3}和 ...
- vue axios拦截器介绍
关于axios的拦截器是一个作用非常大,非常好用的东西.分为请求拦截器和响应拦截器两种.我一般把拦截器写在main.js里. 1. 请求拦截器 请求拦截器的作用是在请求发送前进行一些操作,例如在每个请 ...