Splay 全局 Kth
参考代码:
#include <bits/stdc++.h> constexpr auto Inf = 0x3F3F3F3F; typedef long long LL; using namespace std; namespace IO { inline LL read() { LL o = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c > '/' && c < ':') { o = o * 10 + c - '0'; c = getchar(); } return o * f; } inline char recd() { char o; while ((o = getchar()) != 'Q' && o != 'D'); return o; } } using namespace IO; const int SIZE = 1E5 + 7; struct Node { int son[2], Fa, sze, w, c; } Node[SIZE << 4]; int Ind, Root; inline void pushup(int now) { Node[now].sze = Node[Node[now].son[1]].sze + Node[Node[now].son[0]].sze + Node[now].c; } inline int which(int now) { return now == Node[Node[now].Fa].son[1]; } void Rot(int now) { int F = Node[now].Fa, FF = Node[F].Fa, w = which(now); Node[Node[now].son[!w]].Fa = F, Node[F].son[w] = Node[now].son[!w]; Node[now].Fa = FF, Node[FF].son[which(F)] = now; Node[now].son[!w] = F, Node[F].Fa = now; pushup(F), pushup(now); } void Splay(int now, int pos) { for (int F = Node[now].Fa; F != pos; Rot(now), F = Node[now].Fa) if (Node[F].Fa != pos) which(now) ^ which(F) ? Rot(now) : Rot(F); if (!pos) Root = now; } int Ins(int& now, int F, int w) { if (!now) { now = ++Ind, Node[now].Fa = F, Node[now].sze = Node[now].c = 1, Node[now].w = w; return now; } if (Node[now].w == w) { Node[now].c++, pushup(now); return now; } int pos = Ins(Node[now].son[Node[now].w < w], now, w); pushup(now); return pos; } void Ins(int w) { int pos = Ins(Root, 0, w); Splay(pos, 0); } int Kth(int now, int K) { if (K >= Node[Node[now].son[0]].sze + 1 && K <= Node[Node[now].son[0]].sze + Node[now].c) return now; if (K <= Node[Node[now].son[0]].sze) return Kth(Node[now].son[0], K); return Kth(Node[now].son[1], K - Node[Node[now].son[0]].sze - Node[now].c); } int main() { int N = read(), Tmp, K = 1; Ins(Inf), Ins(-Inf); while (N--) { Tmp = read(); if (~Tmp) Ins(Tmp); else printf("%d\n", Node[Kth(Root, K++ + 1)].w); } }
0.0分
1 人评分
A+B for Input-Output Practice (VII) (C++代码)浏览:643 |
哥德巴赫曾猜测 (C语言代码)浏览:1148 |
妹子杀手的故事 (C语言代码)浏览:1297 |
wu-淘淘的名单 (C++代码)浏览:1532 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:1483 |
关于float,double变量的几点说明浏览:1926 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:497 |
1231题解(注意理解“输入多个测试实例”)浏览:830 |
A+B for Input-Output Practice (III) (C语言代码)浏览:455 |
C二级辅导-计负均正 (C语言代码)浏览:664 |