Source: Ural 1439. battle with you-know-who
Start with a series of numbers 1, 2, 3,... l K, output the number k, d k, and delete the number K.
Ideas: treap tree insert and delete count each time Binary Search number of k Number of mid query treap number of less than or equal to mid Number of y so mid should be the number of mid-y large continue with K binary
#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;const int maxm = 100010;int ch[maxm][2], r[maxm], val[maxm], sum[maxm], num[maxm], cnt, root;void Node(int &rt, int x){ rt = ++cnt; ch[rt][0] = ch[rt][1] = 0; r[rt] = rand(); val[rt] = x; if(cnt > 1) {sum[rt] = 1; num[rt] = 1; } else { sum[rt] = 0; num[rt] = 0; }} void maintain(int rt){ sum[rt] = sum[ch[rt][0]]+sum[ch[rt][1]]+num[rt];}void init(){ch[0][0] = ch[0][1] = 0;r[0] = (1LL<<31)-1;val[0] = 0;sum[0] = 0;cnt = 0;root = 0;Node(root, 2000000001);}void rotate(int &rt, int d){ int k = ch[rt][d^1]; ch[rt][d^1] = ch[k][d]; ch[k][d] = rt; maintain(rt); maintain(k); rt = k;}void insert(int &rt, int x){ if(!rt){ Node(rt, x); return; } else{ if(x == val[rt]) num[rt]++; else { int d = x < val[rt] ? 0 : 1; insert(ch[rt][d], x); if(r[ch[rt][d]] < r[rt]) rotate(rt, d^1); } } maintain(rt);}/*void remove(int &rt, int x){ if(val[rt] == x){ val[rt]--; if(!val[rt]){ if(!ch[rt][0] && !ch[rt][1]) { rt = 0; return; } else{ int d = r[ch[rt][0]] > r[ch[rt][1]] ? 1 : 0; rotate(rt, d); remove(ch[rt][d], x); } else{ } } } else remove(ch[rt][x>val[rt]], x); maintain(rt);}*/int kth(int rt, int k){ if(rt == 0) return 0;if(val[rt] <= k)return sum[ch[rt][0]]+num[rt]+kth(ch[rt][1], k);return kth(ch[rt][0], k);}int main(){int n, m; while(scanf("%d %d", &n, &m) != EOF) { init(); while(m--) { char s[10]; int x; scanf("%s %d", s, &x); int l = 1, r = n, ans; //printf("****%d\n", kth(root, 10)); while(l < r) { int mid = (l + r) >> 1; int y = kth(root, mid); if(x > mid-y) { l = mid+1; ans = mid+1; } else { r = mid; }//printf("***%d\n", y); } if(s[0] == 'L') {printf("%d\n", l); } else { //int y = kth(root, x);insert(root, l); } } } return 0;}
Ural 1439. battle with you-know-who treap tree