【Foreign】Rectangle [KD-tree]

时间:2022-01-18 01:16:01

Rectangle

Time Limit: 50 Sec  Memory Limit: 512 MB

Description

  【Foreign】Rectangle [KD-tree]

Input

  【Foreign】Rectangle [KD-tree]

Output

  【Foreign】Rectangle [KD-tree]

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

  【Foreign】Rectangle [KD-tree]

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]的更多相关文章

  1. 【数据结构】B-Tree&comma; B&plus;Tree&comma; B&ast;树介绍 转

    [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...

  2. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  3. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  4. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  5. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  6. &lpar;RE&rpar; luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. 【LeetCode】199&period; Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  9. 【数据结构】B-Tree&comma; B&plus;Tree&comma; B&ast;树介绍

    [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...

随机推荐

  1. &lbrack;转&rsqb;字符编码笔记:ASCII,Unicode和UTF-8

    转自:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 作者: 阮一峰 日期: 2007年10月28日 今天中午, ...

  2. 什么是 kNN 算法?

    学习 machine learning 的最低要求是什么?  我发觉要求可以很低,甚至初中程度已经可以.  首先要学习一点 Python 编程,譬如这两本小孩子用的书:[1][2]便可.   数学方面 ...

  3. Unity UGUI——遮罩效果(Mask)

    Show Mask Graphic

  4. contact form

    use the existing service: http://www.foxyform.com/

  5. CKeditor使用js验证不得为空

    if (CKEDITOR.instances.TextArea1.getData() == '') { alert('警告:详细内容不得为空!'); CKEDITOR.instances.TextAr ...

  6. C&num;去掉JSON字符串中的最后一个数字

    这个问题总结起来就是去掉字符串中的最后一个"," 字符串:string s = "1,2,3,4,5," 目标:删除最后一个 "," 方法: ...

  7. JS中的continue,break,return的区别

    关于continue.break.return的用法区别早在大一C语言学习中研究过,这里单独拿出来,总结一下. 还是来点实在的吧,上代码 <!DOCTYPE html PUBLIC " ...

  8. yum安装时遇到的问题

    在yum 安装出现下面的提示的时候,需要安装GPG key warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID e85 ...

  9. UDP中使用bind和connect的作用

    1:UDP中可以使用connect系统调用 2:UDP中connect操作与TCP中connect操作有着本质区别. TCP中调用connect会引起三次握手,client与server建立连结.UD ...

  10. Lintcode214-Max of Array-Naive

    Given an array with couple of float numbers. Return the max value of them. Example Example 1: Input: ...