【Foreign】Rectangle [KD-tree]
Rectangle
Time Limit: 50 Sec Memory Limit: 512 MB
Description

Input

Output

Sample Input
0
4
2 0
2 1
1 1
1 2
4
0 0 2 2
1 1 2 2
1 0 2 1
0 0 1 1
Sample Output
2 3
2 2
2 2
1 1
HINT

Solution
显然,如果我们求出了 last[i] 表示 在某个相同横/纵坐标下,前一个纵/横坐标的取值,那问题就转化为了三维偏序,且要求在线。
限制显然形如:L1 <= xi <= R1, L2 <= yi <= R2, last_i <= L3。
由于BearChild太菜了,它的 bitset 内存不够开。直接上个 KD-tree 卡卡常即可。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long s64; const int ONE = ;
const int Max = ; int get()
{
int res = , Q = ; char c;
while( (c = getchar()) < || c > )
if(c == '-') Q = -;
if(Q) res = c - ;
while( (c = getchar()) >= && c <= )
res = res * + c - ;
return res * Q;
}
int T, n; struct node {int x, y;} fir[ONE];
bool cmp_x(const node &a, const node &b) {return a.x < b.x || (a.x == b.x && a.y < b.y);}
bool cmp_y(const node &a, const node &b) {return a.y < b.y || (a.y == b.y && a.x < b.x);} struct power {int c[];};
bool cmp_0(const power &a, const power &b) {return a.c[] < b.c[];}
bool cmp_1(const power &a, const power &b) {return a.c[] < b.c[];}
bool cmp_2(const power &a, const power &b) {return a.c[] < b.c[];} int Ans;
struct ID
{
struct point {int lc, rc, size, l[], r[];} p[ONE];
power a[ONE]; int kd_num;
void Update(point &x)
{
if(x.lc) x.size += p[x.lc].size;
if(x.rc) x.size += p[x.rc].size;
point y;
if(x.lc) y = p[x.lc],
x.l[] = min(x.l[], y.l[]), x.r[] = max(x.r[], y.r[]),
x.l[] = min(x.l[], y.l[]), x.r[] = max(x.r[], y.r[]),
x.l[] = min(x.l[], y.l[]), x.r[] = max(x.r[], y.r[]);
if(x.rc) y = p[x.rc],
x.l[] = min(x.l[], y.l[]), x.r[] = max(x.r[], y.r[]),
x.l[] = min(x.l[], y.l[]), x.r[] = max(x.r[], y.r[]),
x.l[] = min(x.l[], y.l[]), x.r[] = max(x.r[], y.r[]);
} s64 calc(int l, int r, int id)
{
s64 x = , xx = ;
for(int i = l; i <= r; i++)
x += a[i].c[id], xx += (s64)a[i].c[id] * a[i].c[id];
return (r - l + ) * xx - x * x;
} int root;
int Build(int l, int r)
{
int mid = l + r >> ;
point &x = p[mid]; s64 w0 = calc(l, r, ), w1 = calc(l, r, ), w2 = calc(l, r, );
s64 w = max(w0, max(w1, w2)); if(w == w0) nth_element(a + l, a + mid, a + r + , cmp_0);
else
if(w == w1) nth_element(a + l, a + mid, a + r + , cmp_1);
else
if(w == w2) nth_element(a + l, a + mid, a + r + , cmp_2); if(l < mid) x.lc = Build(l, mid - );
if(mid < r) x.rc = Build(mid + , r); x.size = ;
x.l[] = x.r[] = a[mid].c[];
x.l[] = x.r[] = a[mid].c[];
x.l[] = x.r[] = a[mid].c[];
Update(x);
return mid;
} bool insect(const point &a, const point &b)
{
if(a.r[] < b.l[] || b.r[] < a.l[]) return ;
if(a.r[] < b.l[] || b.r[] < a.l[]) return ;
if(a.r[] < b.l[] || b.r[] < a.l[]) return ;
return ;
} bool contain(const point &a, const point &b)
{
if(!(a.l[] <= b.l[] && b.r[] <= a.r[])) return ;
if(!(a.l[] <= b.l[] && b.r[] <= a.r[])) return ;
if(!(a.l[] <= b.l[] && b.r[] <= a.r[])) return ;
return ;
} bool contain(const point &a, const power &b)
{
if(!(a.l[] <= b.c[] && b.c[] <= a.r[])) return ;
if(!(a.l[] <= b.c[] && b.c[] <= a.r[])) return ;
if(!(a.l[] <= b.c[] && b.c[] <= a.r[])) return ;
return ;
} void Query(int i, const point &x)
{
point now = p[i];
if(!insect(x, now)) return;
if(contain(x, now)) {Ans += now.size; return;}
if(contain(x, a[i])) Ans++;
if(now.lc) Query(now.lc, x);
if(now.rc) Query(now.rc, x);
}
};
ID A, B; void Make_1()//|
{
sort(fir + , fir + n + , cmp_y);
static int last[ONE];
for(int i = ; i <= Max; i++) last[i] = -;
for(int i = ; i <= n; i++)
{
A.a[i].c[] = fir[i].x;
A.a[i].c[] = fir[i].y;
A.a[i].c[] = last[fir[i].x];
last[fir[i].x] = fir[i].y;
}
A.root = A.Build(, n);
} void Make_2()
{
sort(fir + , fir + n + , cmp_x);
static int last[ONE];
for(int i = ; i <= Max; i++) last[i] = -;
for(int i = ; i <= n; i++)
{
B.a[i].c[] = fir[i].x;
B.a[i].c[] = fir[i].y;
B.a[i].c[] = last[fir[i].y];
last[fir[i].y] = fir[i].x;
}
B.root = B.Build(, n);
} int main()
{
T = get(), n = get();
for(int i = ; i <= n; i++)
fir[i].x = get(), fir[i].y = get();
Make_1(), Make_2();
int Q = get(), lax = , lay = ;
while(Q--)
{
int x_1 = get(), y_1 = get(), x_2 = get(), y_2 = get();
x_1 = x_1 + (lax + lay) * T, y_1 = y_1 + (lax + lay) * T;
x_2 = x_2 + (lax + lay) * T, y_2 = y_2 + (lax + lay) * T;
ID::point x; Ans = x.size = x.lc = x.rc = ;
x.l[] = x_1, x.r[] = x_2, x.l[] = y_1, x.r[] = y_2;
x.l[] = -, x.r[] = y_1 - ;
A.Query(A.root, x), lax = Ans; Ans = x.size = x.lc = x.rc = ;
x.l[] = x_1, x.r[] = x_2, x.l[] = y_1, x.r[] = y_2;
x.l[] = -, x.r[] = x_1 - ;
B.Query(B.root, x), lay = Ans; printf("%d %d\n", lax, lay);
}
}
【Foreign】Rectangle [KD-tree]的更多相关文章
- 【数据结构】B-Tree, B+Tree, B*树介绍 转
[数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【数据结构】B-Tree, B+Tree, B*树介绍
[摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...
随机推荐
- 【百度】大型网站的HTTPS实践(一)——HTTPS协议和原理
大型网站的HTTPS实践(一)——HTTPS协议和原理 原创 网络通信/物联网 作者:AIOps智能运维 时间:2018-11-09 15:07:39 349 0 前言 百度于2015年上线了全站 ...
- NULL,"",String.Empty三者在C#中的区别
(1)NULLnull 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值类 ...
- 关于Delphi内存表的使用说明
关于Delphi内存表的使用说明: 1.建立临时表 数据输入是开发数据库程序的必然环节.在Client/Server结构中,客户端可能要输入一批数据后,再向服务器的后台数据库提交,这就需要在本地(客 ...
- 自定义smokeping告警(邮件+短信)
前段时间接到公司IT同事需求,帮助其配置smokeping的告警功能,之前配置的姿势有些问题,告警有些问题,现在调试OK,在此将关键配置点简单记录下. 关键的配置项主要有: 定义告警规则并配置将告警信 ...
- 虚拟主机、ECS云服务器、VPS区别汇总
想做一个网站,但是在各种类型的服务器琳琅满目,现在总结一下市场上常见的几种服务器. 1.虚拟主机 虚拟主机就是利用虚拟化的技术,将一台服务器划分出一定大小的空间,每个空间都给予单独的 FTP 权限和 ...
- python中使用%与.format格式化文本
初学python,看来零零碎碎的格式化文本的方法,总结一下python中格式化文本的方法.使用不当的地欢迎指出谢谢. 1.首先看使用%格式化文本 常见的占位符: 常见的占位符有: %d 整数 %f 浮 ...
- winrar 授权破解过期解决
RAR registration data Federal Agency for Education 1000000 PC usage license UID=b621cca9a84bc5deffbf ...
- 【比赛】NOIP2017 列队
一直忘了发,现在赶快补 用权值线段树维护有人的位置,动态开点省空间 多加的人用个vector存下来就可以了 #include<bits/stdc++.h> #define ui unsig ...
- 【BZOJ5252】林克卡特树(动态规划,凸优化)
[BZOJ5252]林克卡特树(动态规划,凸优化) 题面 BZOJ(交不了) 洛谷 题解 这个东西显然是随着断开的越来越多,收益增长速度渐渐放慢. 所以可以凸优化. 考虑一个和\(k\)相关的\(dp ...
- 【THUSC2017】杜老师
题目描述 杜老师可是要打+∞年World Final的男人,虽然规则不允许,但是可以改啊! 但是今年WF跟THUSC的时间这么近,所以他造了一个idea就扔下不管了…… 给定L,R,求从L到R的这R− ...