Submission #671080

#TimeUsernameProblemLanguageResultExecution timeMemory
671080SirCovidThe19thIndex (COCI21_index)C++17
110 / 110
1266 ms58688 KiB
#include <bits/stdc++.h> using namespace std; template<class T, int sz> struct persistentSegTree{ static const int maxSz = sz * 31; T seg[maxSz]; int idx = 1, lc[maxSz], rc[maxSz]; void dup(int &i){ lc[idx] = lc[i]; rc[idx] = rc[i]; seg[idx] = seg[i]; i = idx; idx++; } void upd(int p, int &i, int l = 0, int r = 2e5 + 5){ dup(i); if (l == r){ seg[i] = 1; return; } int mid = (l + r) / 2; if (p <= mid) upd(p, lc[i], l, mid); else upd(p, rc[i], mid + 1, r); seg[i] = seg[lc[i]] + seg[rc[i]]; } T qry(int ql, int qr, int i, int l = 0, int r = 2e5 + 5){ if (qr < l or ql > r or !i) return 0; if (l >= ql and r <= qr) return seg[i]; int mid = (l + r) / 2; return qry(ql, qr, lc[i], l, mid) + qry(ql, qr, rc[i], mid + 1, r); } }; const int mx = 2e5 + 5; int n, q, root[mx]; vector<int> nums[mx]; persistentSegTree<int, mx> pseg; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++){ int p; cin >> p; nums[p].push_back(i); } for (int p = mx - 2; p; p--){ //p[i] <= 2e5 root[p] = root[p + 1]; for (int i : nums[p]) pseg.upd(i, root[p]); } while (q--){ int l, r; cin >> l >> r; int lo = 0, hi = mx; while (lo < hi){ int mid = (lo + hi + 1) / 2; //How many elems >= mid in [l...r]? (pseg.qry(l, r, root[mid]) >= mid) ? lo = mid : hi = mid - 1; } cout<<lo<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...