4605: 崂山白花蛇草水
Time Limit: 80 Sec Memory Limit: 512 MBSubmit: 527 Solved: 153
[ Submit][ Status][ Discuss]
Description
Input
Output
Sample Input
1 1 1 1
1 2 2 3
1 4 1 2
1 3 4 4
2 1 1 4 1 3
2 2 2 3 5 4
2 2 1 4 4 2
Sample Output
NAIVE!ORZzyz.
3
HINT
Source
KD-Tree是个啥? 好像不会啊... 听说就是按维度来二分? 于是YY了一下, 没想到就还YY出来了KD-Tree嘿嘿。 建树的时候每次按一个维度来划分, 选中间那个点作为当前节点, 然后分开搞lson, rson... 树高显然log. 插入怎么维护树高... gg. 于是还是可耻的打开了学习笔记.
原来是用替罪羊暴力重建来, 妙啊妙啊, 不过秉承着自己平衡树只写数组的原则就放弃指针的写法了(线段树还是秉承丁神的指针... 所以这份代码又会有指针又会有数组...). 而且找中位数不用sort直接调用一个nth_element? 期望O(n)? 并不知道怎么做到的...
然后这道题就很水啦, 要找第k大外层套一个值域线段树即可, 内部套一个2d-tree维护矩阵即可. 第一次写学了Claris神犇的模板... 还是很好写的. 成功跟随神犇的脚步走上了第一页的光辉之路. 不过码量还是不小啊... 码力还是欠缺.
Ps:话说为什么找最早的替罪羊重建要跑60s, 找到第一个替罪羊重建就是10s? 记得论文说过有两种写法的啊(难不成记错了)... 说不定是我写丑了. 如果神犇有独特的见解希望能留言指导一下蒟蒻.
#include<bits/stdc++.h>
#define smax(x, y) x = max(x, y)
#define smin(x, y) x = min(x, y)
using namespace std;
const int inf = 1e9;
const int maxm = 3e5;
const double A = 0.75;
const int maxn = 1e6 + 7e5;
int deep, n, ans, cmpd, cnt, X1, X2, Y1, Y2, K, cur, type;
int tmp[maxm], re[maxm];
struct Seg_Node {
int KDrt;
Seg_Node *ls, *rs;
}pool[maxn], *root, *tail = pool;
struct KDT_Node {
int ls, rs, siz;
int d[2], Min[2], Max[2];
}t[maxn];
inline bool cmp(int x, int y) {
return t[x].d[cmpd] < t[y].d[cmpd];
}
inline const int read() {
register int x = 0;
register char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x;
}
inline bool balance(int x) {
return (double)t[t[x].ls].siz < A * t[x].siz && (double)t[t[x].rs].siz < A * t[x].siz;
}
inline void update(int x) {
t[x].siz = t[t[x].ls].siz + t[t[x].rs].siz + 1;
if (t[x].ls) {
smax(t[x].Max[0], t[t[x].ls].Max[0]);
smin(t[x].Min[0], t[t[x].ls].Min[0]);
smax(t[x].Max[1], t[t[x].ls].Max[1]);
smin(t[x].Min[1], t[t[x].ls].Min[1]);
}
if (t[x].rs) {
smax(t[x].Max[0], t[t[x].rs].Max[0]);
smin(t[x].Min[0], t[t[x].rs].Min[0]);
smax(t[x].Max[1], t[t[x].rs].Max[1]);
smin(t[x].Min[1], t[t[x].rs].Min[1]);
}
}
void dfs(int x) {
if (x)
re[++ cnt] = x,
dfs(t[x].ls), dfs(t[x].rs);
}
int build(int lf, int rg, int D) {
int mid = (lf + rg) >> 1;
cmpd = D;
nth_element(re + lf + 1, re + mid + 1, re + rg + 1, cmp);
int x = re[mid];
t[x].Max[0] = t[x].Min[0] = t[x].d[0];
t[x].Max[1] = t[x].Min[1] = t[x].d[1];
if (lf ^ mid) t[x].ls = build(lf, mid - 1, !D);
else t[x].ls = 0;
if (rg ^ mid) t[x].rs = build(mid + 1, rg, !D);
else t[x].rs = 0;
update(x);
return x;
}
inline void insert(int &rt, int now) {
if (!rt) {
rt = now;
return;
}
for (int D = deep = 0, x = rt; ; D ^= 1) {
tmp[++ deep] = x;
t[x].siz ++;
smax(t[x].Max[0], t[now].Max[0]);
smin(t[x].Min[0], t[now].Min[0]);
smax(t[x].Max[1], t[now].Max[1]);
smin(t[x].Min[1], t[now].Min[1]);
if (t[now].d[D] < t[x].d[D]) {
if (!t[x].ls) {
t[x].ls = now;
break;
} else x = t[x].ls;
} else {
if (!t[x].rs) {
t[x].rs = now;
break;
} else x = t[x].rs;
}
}
tmp[++ deep] = now;
while (balance(now)) now = tmp[-- deep];
if (!now) return;
cnt = 0;
if (now == rt) {
dfs(rt);
rt = build(1, cnt, 0);
} else {
dfs(now);
int f = tmp[-- deep];
int k = build(1, cnt, deep & 1);
(t[f].ls == now) ? t[f].ls = k : t[f].rs = k;
}
}
inline void ask(int x) {
if (!x || t[x].Max[0] < X1 || t[x].Min[0] > X2 || t[x].Max[1] < Y1 || t[x].Min[1] > Y2 || ans >= K) return;
if (t[x].Min[0] >= X1 && t[x].Max[0] <= X2&& t[x].Min[1] >= Y1 && t[x].Max[1] <= Y2) {
ans += t[x].siz; return;
}
if (t[x].d[0] >= X1 && t[x].d[0] <= X2 && t[x].d[1] >= Y1 && t[x].d[1] <= Y2) ans ++;
ask(t[x].ls), ask(t[x].rs);
}
inline void add() {
Seg_Node *bt = root;
int lf = 1, rg = inf, flag = 1;
while(true) {
if (flag) {
cur ++;
t[cur].siz = 1;
t[cur].Max[0] = t[cur].Min[0] = t[cur].d[0] = X1;
t[cur].Max[1] = t[cur].Min[1] = t[cur].d[1] = Y1;
insert(bt -> KDrt, cur);
}
if (lf == rg) return;
int mid = (lf + rg) >> 1;
if (K <= mid) {
if (bt -> ls == NULL) bt -> ls = ++ tail;
bt = bt -> ls, rg = mid, flag = 0;
} else {
if (bt -> rs == NULL) bt -> rs = ++ tail;
bt = bt -> rs, lf = mid + 1, flag = 1;
}
}
}
inline void seg_query() {
ans = 0, ask(root -> KDrt);
if (ans < K) {
ans = 0;
puts("NAIVE!ORZzyz.");
return;
}
Seg_Node* bt = root;
int lf = 1, rg = inf;
while(lf < rg) {
int mid = (lf + rg) >> 1;
ans = 0;
if (bt -> rs != NULL) ask(bt -> rs -> KDrt);
if (ans >= K) bt = bt -> rs, lf = mid + 1;
else bt = bt -> ls, rg = mid, K -= ans;
}
printf("%d\n", ans = lf);
}
int main() {
root = ++ tail;
n = read(), n = read();
for (register int i = 1; i <= n; ++ i) {
type = read();
X1 = read(), Y1 = read();
X1 ^= ans, Y1 ^= ans;
if (type & 1) {
K = read(), K ^= ans;
add();
} else {
X2 = read(), Y2 = read(), K = read();
X2 ^= ans, Y2 ^= ans, K ^= ans;
seg_query();
}
}
}