Submission #1100269

#TimeUsernameProblemLanguageResultExecution timeMemory
1100269vladiliusNew Home (APIO18_new_home)C++17
57 / 100
5106 ms797152 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define pb push_back #define ff first #define ss second #define arr3 array<int, 3> const int inf = 1e8; const int N = 2e6; const int NN = 4e7; struct IT{ struct node{ int m, l, r, s; node(){ l = r = m = 0; } }; node all[NN]; multiset<int> st[N]; int rt, cc, cc1; void init(){ all[1] = node(); rt = cc = 1; cc1 = -1; } int nw(int tl, int tr){ all[++cc] = node(); if (tl == tr){ st[++cc1] = {}; all[cc].s = cc1; } return cc; } void add(int v, int tl, int tr, int& p, int& x){ if (tl == tr){ st[all[v].s].insert(x); all[v].m = max(all[v].m, x); return; } int tm = (tl + tr) / 2; if (p <= tm){ if (!all[v].l) all[v].l = nw(tl, tm); add(all[v].l, tl, tm, p, x); } else { if (!all[v].r) all[v].r = nw(tm + 1, tr); add(all[v].r, tm + 1, tr, p, x); } all[v].m = 0; if (all[v].l) all[v].m = max(all[v].m, all[all[v].l].m); if (all[v].r) all[v].m = max(all[v].m, all[all[v].r].m); } void add(int l, int r){ add(rt, 1, inf, l, r); } void rem(int v, int tl, int tr, int& p, int& x){ if (tl == tr){ st[all[v].s].erase(st[all[v].s].find(x)); all[v].m = (st[all[v].s].empty()) ? 0 : *prev(st[all[v].s].end()); return; } int tm = (tl + tr) / 2; if (p <= tm){ rem(all[v].l, tl, tm, p, x); } else { rem(all[v].r, tm + 1, tr, p, x); } all[v].m = 0; if (all[v].l) all[v].m = max(all[v].m, all[all[v].l].m); if (all[v].r) all[v].m = max(all[v].m, all[all[v].r].m); } void rem(int l, int r){ rem(rt, 1, inf, l, r); } int get(int v, int tl, int tr, int l, int r){ if (l > tr || r < tl) return 0; if (l <= tl && tr <= r) return all[v].m; int tm = (tl + tr) / 2, out = 0; if (all[v].l) out = max(out, get(all[v].l, tl, tm, l, r)); if (all[v].r) out = max(out, get(all[v].r, tm + 1, tr, l, r)); return out; } bool get(int l, int r){ return get(rt, 1, inf, 1, l) >= r; } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, q; cin>>n>>k>>q; vector<int> x(n + 1), t(n + 1), a(n + 1), b(n + 1); for (int i = 1; i <= n; i++){ cin>>x[i]>>t[i]>>a[i]>>b[i]; } vector<int> xq(q + 1), yq(q + 1); for (int i = 1; i <= q; i++){ cin>>xq[i]>>yq[i]; } vector<arr3> mp; for (int i = 1; i <= n; i++){ mp.pb({a[i], 1, i}); if (b[i] < inf) mp.pb({b[i] + 1, 2, i}); } for (int i = 1; i <= q; i++){ mp.pb({yq[i], i + 2, xq[i]}); } auto cmp = [&](arr3 x, arr3 y){ if (x[0] == y[0]){ return x[1] < y[1]; } return x[0] < y[0]; }; sort(mp.begin(), mp.end(), cmp); vector<int> out(q + 1), cnt(k + 1); multiset<int> st[k + 1]; int df = 0, l, r; IT T; T.init(); for (auto [X, tt, i]: mp){ if (tt == 1){ df += !cnt[t[i]]; cnt[t[i]]++; if (cnt[t[i]] == 1){ if (1 < x[i]) T.add(1, x[i] - 1); if (x[i] < inf) T.add(x[i] + 1, inf); st[t[i]].insert(x[i]); continue; } if (st[t[i]].find(x[i]) != st[t[i]].end()){ st[t[i]].insert(x[i]); continue; } auto it = st[t[i]].upper_bound(x[i]); if (it == st[t[i]].end()) r = inf; else r = (*it) - 1; if (it == st[t[i]].begin()) l = 1; else l = *prev(it) + 1; if (l <= r) T.rem(l, r); if (l < x[i]) T.add(l, x[i] - 1); if (x[i] < r) T.add(x[i] + 1, r); st[t[i]].insert(x[i]); } else if (tt == 2){ cnt[t[i]]--; df -= !cnt[t[i]]; if (st[t[i]].count(x[i]) > 1){ st[t[i]].erase(st[t[i]].find(x[i])); continue; } auto it = st[t[i]].find(x[i]); if (it == st[t[i]].begin()) l = 1; else l = *prev(it) + 1; if (it == prev(st[t[i]].end())) r = inf; else r = *next(it) - 1; if (l < x[i]) T.rem(l, x[i] - 1); if (x[i] < r) T.rem(x[i] + 1, r); if (cnt[t[i]]) T.add(l, r); st[t[i]].erase(it); } else { int ii = tt - 2; if (df < k){ out[ii] = -1; continue; } auto check = [&](int d){ return T.get(max(1, i - d), min(inf, i + d)); }; int l = 0, r = inf; while (l + 1 < r){ int m = (l + r) / 2; if (check(m)){ l = m + 1; } else { r = m; } } if (check(l)) l = r; out[ii] = l; } } for (int i = 1; i <= q; i++){ cout<<out[i]<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...