【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 ...
随机推荐
- Scrum 项目 3.0
-------------------------------------3.0----------------------------------------------------- 一.项目工作 ...
- 11th 单元测试工具JUnit的学习
1.写好一个简易的四则运算的程序 UnitTest类文件: public class UnitTest { int a; int b; int answer;//正确答案 public int plu ...
- 树莓派安装、卸载docker
1.下载安装脚本(官方说明的是不能使用debian的系统的安装方式安装): curl -fsSL get.docker.com -o get-docker.sh 2.安装: sudo sh get-d ...
- BAT等公司必问的8道Java经典面试题,你都会了吗?
工作多年以及在面试中,我经常能体会到,有些面试者确实是认真努力工作,但坦白说表现出的能力水平却不足以通过面试,通常是两方面原因: 1.“知其然不知其所以然”.做了多年技术,开发了很多业务应用,但似乎并 ...
- LINQ to SQL和Entity Framework
LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术. 那么为什么会有LINQ这个东西的出现呢. 简单来说LINQ是为了满足不知道怎么操作数据库的程序员开 ...
- jQueryEasyUI的使用
easyUI是一个基于jQuery的前端框架,如果想要使用easyUI就需要先导入easyUI的一些js文件和样式文件(本地化的JS文件可以自己选择是否导入),导入方式如下: 其中必须首先导入jQue ...
- BZOJ3083 遥远的国度(树链剖分+线段树)
考虑暴力树剖.那么修改路径和查询子树最小值非常简单. 对于换根当然不能真的给他转一下,我们只记录当前根是哪个.对于查询,如果查询点不在当前根到原根的路径上,显然换根是对答案没有影响的:如果是当前根,答 ...
- Linux中的防火墙----iptables
防火墙,它是一种位于内部网络与外部网络之间的网络安全系统.一项信息安全的防护系统,依照特定的规则,允许或是限制传输的数据通过. 防火墙根据主要的功能可分为网络层防火墙.应用层防火墙.数据库防火墙. 网 ...
- kafka问题集(一):broker少于kafka节点数
问题集仅为个人实践,若有不准确的,欢迎交流! 一.现象: 集群有3台kafka服务器,而kafka 的9002界面上broker仅有2个:log.dirs配置路径为/data/kafka/data,而 ...
- 用C语言获取任意文件的长度(可能大于2GB)#define _FILE_OFFSET_BITS 64
打开文件后用 fseek() 函数把文件位置指针移动到文件的末尾,用 ftell() 获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.但是这样做也会受到下面的限制:ftell() 函数的返 ...