版本比较
[二分][ds][主席树]P2839 [国家集训队] middle
正文
增加 5 行删除 5 行
20 行未改动2122232425252627282922 行未改动5253545556565758596018 行未改动798081828383848586877 行未改动95969798999910010110210310410510610710810810911011120 行未改动于是便自然的想到了可持久化线段树了,具体怎么做建议自己想想。
```cpp#include<bits/stdc++.h>#define int long long #define int long long#define zsw(x) ((x + lst) % n + 1)using namespace std;
const int N = 1e5 + 10, INF = 0x3f3f3f3f;22 行未改动 void build(int& o, int l, int r){ hjt[++cnt].dat = {-1, -1, -1}; o = cnt; if(l == r) return; build(hjt[o].ls, l, mid); build(hjt[o].rs, mid + 1, r); pushup(o); pushup(o); } void add(int pre, int& o, int l, int r, int x){ hjt[++cnt] = hjt[pre]; o = cnt; if(l == r){hjt[o].dat = (data){1, 1, 1}; return;}18 行未改动 ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i], rk[i] = a[i]; sort(rk + 1, rk + n + 1); int Rksiz = unique(rk + 1, rk + n + 1) - (rk + 1); sort(rk + 1, rk + n + 1); int Rksiz = unique(rk + 1, rk + n + 1) - (rk + 1); for(int i = 1; i <= n; i++) a[i] = lower_bound(rk + 1, rk + Rksiz + 1, a[i]) - rk, pos[a[i]].push_back(i); build(root[Rksiz + 1], 1, n); for(int i = Rksiz; i > 0; i--){ root[i] = root[i + 1];7 行未改动 sort(q, q + 4); a = q[0], b = q[1], c = q[2], d = q[3]; //cout << a << " " << b << " " << c << " " << d << "\n"; int l = 1, r = Rksiz, ans = 0; while(l <= r){ int rank = querydat(root[mid], 1, n, b + 1, c - 1).sum + querydat(root[mid], 1, n, a, b).suf + querydat(root[mid], 1, n, c, d).pre; // cout << mid << " " << rank << "\n"; // cout << mid << " " << rank << "\n"; if(rank >= 0){ ans = mid; l = mid + 1; } else r = mid - 1; } cout << (lst = rk[ans]) << "\n"; }
return 0;}```