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

  1. 【数据结构】B-Tree, B+Tree, B*树介绍 转

    [数据结构]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. (RE) 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. Binary Tree Right Side View 解题报告(Python)

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

  9. 【数据结构】B-Tree, B+Tree, B*树介绍

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

随机推荐

  1. LeetCode题解:(139) Word Break

    题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...

  2. 第九周(11.11-11.17)----Beta版本发布140字评论

    1.新蜂组: 俄罗斯方块:项目有良好的用户界面,在原版的基础上可以对用户分数进行排序,增加了显示下一个方块的功能.是个很有趣的小游戏. 2.天天向上组: 连连看:完成了基本功能,增加了消除时和鼠标点击 ...

  3. jmeter body 中文显示为乱码解决

    这种情况在jmeter3.0的版本中才会产生,注意:这不是乱码,而是由于3.0中优化body data后,使用默认的字体(Consolas)不支持汉字的显示.这样的情况可以这样调整:进入jmeter. ...

  4. Windows 配置Reids集群 Redis Cluster

    一 .所需软件:Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb 二 .安装配置redis  redis下载地 ...

  5. 用SQL查询方式显示GROUP BY中的TOP解决方法[转]

    用SQL查询方式显示GROUP BY中的TOP怎样用一个SQL语句来显示 分组后每个组的前几位 比如把一个学校所有学生的成绩按班级分组,再显示每个班级前五名的信息. 班级     学生   成绩 一班 ...

  6. 每日一问(常用的集合接口和类有哪些【二】)—最常用的集合ArrayList类

    本人在曾经的数年编程生涯中,使用的最多的就是ArrayList类了,原因也非常简单.ArrayList类可以是最直接符合集合这一概念的类了,当然这种说法只是我的个人之见.ArrayList可以说是一个 ...

  7. 文件同步工具 lsyncd2.1.6 安装使用问题

    项目有文件实时同步备份的需求,做了一下调查,比较好的解决方法是使用lsyncd工具.这里主要记录一下遇到的问题及解决方法. lsyncd 的相关介绍和对比可见: lsyncd实时同步搭建指南——取代r ...

  8. 百度地图经纬度批量查找功能XGeocoding使用手册

    <XGeocoding使用手册> 1.下载XGeocoding V2 http://www.gpsspg.com/xgeocoding/download/ 2.解压XGeocoding_v ...

  9. 注册系统所有的dll文件.bat

    @echo off echo 运行后,能重新注册系统所有的dll文件, echo 能解决内存读写错误的问题 pause echo on for %%1 in (%windir%/system32/*. ...

  10. 中行P1签名及验签

    分享中国银行快捷.NET P1签名和验签方法代码中ReturnValue为自定义类型请无视 #region 验证签名 /// <summary> /// 验证签名 /// </sum ...