P6224 [BJWC2014] 数据
Description
维护一个平面直角坐标系,初始有 个点。
有 次操作,每次操作分为三种类型。
- 在点集里添加一个点;
- 给出一个点,查询它到点集里所有点的曼哈顿距离的最小值;
- 给出一个点,查询它到点集里所有点的曼哈顿距离的最大值。
Solution
记一次操作为 ,其中 表示点的坐标, 表示操作类型, 代表操作时间。
特别地,初始的 个点也看作 次 的操作 0。
那么对于一个操作 ,答案就是 。
同理,对于一个操作 ,答案为 。
,可以暴力分讨拆掉绝对值,多出来两个偏序条件。
因此,每一个查询的答案就是四个三维偏序的值取最优。
根据分讨跑四遍三维偏序即可。
可以利用对称操作减少码量。
分析得,时间复杂度 ,空间复杂度 。
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
const int N = 1e5 + 5, M = 2e5 + 5, V = 4e5 + 5, INF = 1e18;
int n, m;
struct op {
int x, y, typ, id;
} a[M],b[M],c[M];
int cnt, cq;
int ans[N];
int val[V], tot;
void ins(int x){val[++tot] = x;}
void dc(int & x){x = lower_bound(val + 1, val + tot + 1, x) - val;}
struct BIT {
int t[V];
BIT(){rep(i, 1, V - 1) t[i] =-INF;}
void upd(int p, int x){while (p <= tot) t[p] = max(t[p], x), p += p &-p;}
void clr(int p){while (p <= tot) t[p] =-INF, p += p &-p;}
int qy(int p){int res =-INF; while (p) res = max(res, t[p]), p &= p - 1; return res;}
} t1,t2;
void cdq(int l, int r){
if (l >= r) return;
int mid = (l + r) >> 1;
cdq(l, mid);
cdq(mid + 1, r);
int j = l;
rep(i, mid + 1, r){
while (j <= mid && b[j].x <= b[i].x){
if (!b[j].id){
t1.upd(b[j].y, b[j].x + val[b[j].y]);
t2.upd(b[j].y,-b[j].x - val[b[j].y]);
}
++j;
}
if (b[i].id){
if (b[i].typ == 1) ans[b[i].id] = min(ans[b[i].id], b[i].x + val[b[i].y] - t1.qy(b[i].y));
else ans[b[i].id] = max(ans[b[i].id], b[i].x + val[b[i].y] + t2.qy(b[i].y));
}
}
rep(i, l, j - 1) if (!b[i].id) t1.clr(b[i].y), t2.clr(b[i].y);
int p = j = l;
rep(i, mid + 1, r){
while (j <= mid && b[j].x <= b[i].x) c[p++] = b[j++];
c[p++] = b[i];
}
while (j <= mid) c[p++] = b[j++];
rep(i, l, r) b[i] = c[i];
}
void solve(int x){
rep(i, 1, cnt) b[i] = {x & 1 ? a[i].x :-a[i].x, x & 2 ? a[i].y : (tot + 1 - a[i].y), a[i].typ, a[i].id};
cdq(1, cnt);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
rep(i, 1, n){
int x, y;
cin >> x >> y;
a[++cnt] = {x, y, 0, 0};
ins(y), ins(-y);
}
cin >> m;
rep(i, 1, m){
int op, x, y;
cin >> op >> x >> y;
a[++cnt] = {x, y, op, op ? ++cq : 0};
if (op == 1) ans[cq] = INF;
else if (op == 2) ans[cq] =-INF;
ins(y), ins(-y);
}
sort(val + 1, val + tot + 1);
tot = unique(val + 1, val + tot + 1) - (val + 1);
rep(i, 1, cnt) dc(a[i].y);
rep(i, 0, 3) solve(i);
rep(i, 1, cq) cout << ans[i] << '\n';
return 0;
}