Title Description
Given an integer sequence consisting of N elements, there are now two operations:
1 add a
At the end of the sequence, an integer a is added to form an integer sequence of length N + 1.
2 mid outputs the median of the current sequence
Median is the number that places a sequence in the middle after sorting it from small to large. (If the sequence length is even, it refers to the smaller of the two numbers in the middle)
Case 1:1 2 13 14 15 16 Median 13
Case 2:1 3 5 7 10 11 17 Median 7
Example 3:1 1 1 1 123 Median 1
Input and output format
Input format:
The initial sequence length of the first behavior is N. The second action is N integers, representing the sequence of integers, separated by spaces. The third action operand M is to perform M operations. Below are M lines, each of which is entered in the format described in the title.
Output format:
Output median for each mid operation
Input and Output Samples
Explain
For 30% of the data, 1 < N < 10,000, 0 < M < 1,000
For 100% data, 1 < N < 100,000, 0 < M < 10,000
The absolute value of an integer in a sequence does not exceed 1,000,000,000, and the number in the sequence may repeat.
Time limit of 1 second per test point
This question is not done casually.
Give me a mouthful of what I can think of.
1. $M < 10,000 dollars if vector s force insertion do not know whether A,
2. Use balance tree directly. I write splay. If I don't want to write code, I can use pb_ds to maintain the red-black tree in siz domain.
3. First offline, the weights are discretized, and then the weights are tree-checked.
4. Follow the practice of https://www.luogu.org/problemnew/show/P1801
#include<cstdio> using namespace std; const int MAXN = 1e6 + 10; #define ls(x) ch[x][0] #define rs(x) ch[x][1] #define root ch[0][1] int fa[MAXN], val[MAXN], rev[MAXN], siz[MAXN], ch[MAXN][2], tot = 0; bool ident(int x) { return ch[fa[x]][0] == x ? 0 : 1; } void connect(int x, int _fa, int opt) { fa[x] = _fa, ch[fa[x]][opt] = x; } void update(int x) { siz[x] = siz[ls(x)] + siz[rs(x)] + rev[x]; } void rotate(int x) { int Y = fa[x], R = fa[Y]; int Yson = ident(x), Rson = ident(Y); int B = ch[x][Yson ^ 1]; connect(B, Y, Yson); connect(x, R, Rson); connect(Y, x, Yson ^ 1); //tag update(Y); update(x); } void splay(int x, int to) { to = fa[to]; while(fa[x] != to) { int y = fa[x]; if(fa[y] == to) rotate(x); else if(ident(x) == ident(y)) rotate(y), rotate(x); else rotate(x), rotate(x); } } int NewNode(int _fa, int _val) { val[++tot] = _val; fa[tot] = _fa; siz[tot] = rev[tot] = 1; return tot; } void insert(int x) { if(!root) {root = NewNode(0, x); return ;} int now = root; while(now) { siz[now]++; if(val[now] == x) {rev[now]++; return ;} int nxt = val[now] < x; if(!ch[now][nxt]) {ch[now][nxt] = NewNode(now, x); splay(ch[now][nxt], root); return ;} now = ch[now][nxt]; } } int ARank(int x) { int now = root; while(now) { //if(siz[now] == x) return val[now]; int used = siz[now] - siz[rs(now)]; if(x > siz[ls(now)] && x <= used) return val[now]; if(used < x) x = x - used, now = ch[now][1]; else now = ch[now][0]; } } char opt[5]; int main() { #ifdef WIN32 freopen("a.in", "r", stdin); #endif int N; scanf("%d", &N); for(int i = 1; i <= N; i++) { int x; scanf("%d", &x); insert(x); } int Q; scanf("%d", &Q); while(Q--) { scanf("%s", opt + 1); if(opt[1] == 'a') { int x; scanf("%d", &x); insert(x); N++; } else printf("%d\n", ARank(N / 2 + (N & 1))); } return 0; }