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);
}
}