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 ...
随机推荐
- openCV2马拉松第18圈——坐标变换
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g 收入囊中 仿射变换 坐标映射 利用坐标映射做一些效果,例如以下 watermark/ ...
- gulp(基础篇)
今天在写项目的时候用到了gulp构建工具,虽然一年前就有用过,但是一直只存在于我的“有道云笔记”里,今天又一次用到,固然是巩固一下,这里来记录一下吧:这里我主要想要记录的就是初学者在第一次使用gulp ...
- CentOS7配置opencv for python && eclipse c/c++[更新]
更改前的安装过程有些问题,主要是ffmpeg-devel的安装部分,这里重新说一下 两种安装方法: 第一种,直接: # yum install numpy opencv* 这种方法安装了之后,能够在p ...
- gulp css html image js 合并压缩
安装node.js npm 以及安装gulp等方法我就不在这里赘述了. 接下里我主要介绍的是Gulpfile文件里面的配置该如何书写. var gulp = require('gulp');//引 ...
- ubuntu手机识别
1.sudo gedit /etc/udev/rules.d/51-android.rules 2.将以下内容拷贝保存 SUBSYSTEM=="usb", ATTR{idVendo ...
- codeforces Looksery Cup 2015 H Degenerate Matrix
The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...
- CSDN第一期总结之三:Thread的问题(转)
C#是一门支持多线程的语言,因此线程的使用也是比较常见的.由于线程的知识在Win32编程的时候已经说得过多,所以在.Net中很少介绍这部分(可能.Net不觉得这部分是它所特有的). 那么线程相关的问题 ...
- asp.net mvc4 之Webapi之应用客户端访问服务器端
一.说明 客户端项目类型设计为:winform(winform窗体项目类型) 服务器端项目类型设计为:asp.net mvc4 webapi 在这里分为项目运行和调试两种情况讨论: 运行: 这种情况 ...
- libcurl理解和使用
1 libcurl是一个很好的客户端库 2 CURLOPT_URL 就是普通的url. 3 CURLOPT_HTTPHEADER 3.1 http get 4 CURLOPT_WRITEFUNCTIO ...
- 什么是aop?-------转
什么是AOP? http://www.cnblogs.com/zhugenqiang/archive/2008/07/27/1252761.html#commentform(转) AOP(Aspec ...