Submission #849186

#TimeUsernameProblemLanguageResultExecution timeMemory
849186hngwlogNew Home (APIO18_new_home)C++14
12 / 100
1542 ms96704 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define _size(x) (int)x.size() #define BIT(i, x) ((x >> i) & 1) #define MASK(n) ((1 << n) - 1) #define REP(i, n) for (int i = 0, _n = (n); i < _n; i++) #define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++) #define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--) #define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i) #define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i) #define FORALL(i, a) for (auto i: a) #define fastio ios_base::sync_with_stdio(0); cin.tie(0); struct storeNode { int x, t, a, b; }; struct queryNode { int l, y, id; }; int n, k, q; vector<storeNode> store; vector<queryNode> qu; namespace subtask12 { const int inf = 1e9; void main() { vector<pair<int, int>> g; FOR(i, 1, n) g.push_back({store[i].a, i}), g.push_back({store[i].b, - i}); REP(i, q) g.push_back({qu[i].y, 0}); sort(g.begin(), g.end(), [&] (const pair<int, int> a, pair<int, int> b) { return a.fi != b.fi ? a.fi < b.fi : a.se > b.se; }); sort(qu.begin(), qu.end(), [&] (const queryNode a, queryNode b) { return a.y < b.y; }); vector<int> ans(q); vector<multiset<int>> mts(k + 1); int i = 0, z = 0; while (i < _size(g)) { int j = i; while (j < _size(g) && g[j].fi == g[i].fi && g[j].se > 0) { int id = g[j].se; mts[store[id].t].insert(store[id].x); j++; } while (j < _size(g) && g[j].fi == g[i].fi && g[j].se == 0) j++; while (z < q && qu[z].y == g[i].fi) { int res = - 1; FOR(id, 1, k) { if (mts[id].empty()) { res = - 1; break; } auto it = mts[id].lower_bound(qu[z].l); int _res = inf; if (it != mts[id].end()) _res = min(_res, *it - qu[z].l); if (it != mts[id].begin()) { --it; _res = min(_res, qu[z].l - *it); } res = max(res, _res); } ans[qu[z].id] = res; z++; } while (j < _size(g) && g[j].fi == g[i].fi) { int id = - g[j].se; mts[store[id].t].erase(mts[store[id].t].find(store[id].x)); j++; } i = j; } REP(i, q) cout << ans[i] << '\n'; } } bool checkSub34() { FOR(i, 1, n) if (store[i].a != 1) return false; return true; } namespace subtask34 { const int inf = 1e9; struct segNode { int value, lazy = - inf; segNode operator + (segNode o) const { return {max(value, o.value), - inf}; } }; struct segTree { vector<segNode> ST; void init(int n) { ST.resize(4 * n + 4); } void down(int id, int l, int r) { int t = ST[id].lazy; if (t == - inf) return ; ST[id].value = max(ST[id].value, t); if (l != r) { ST[id * 2].lazy = max(ST[id * 2].lazy, t); ST[id * 2 + 1].lazy = max(ST[id * 2 + 1].lazy, t); } ST[id].lazy = - inf; } void update(int id, int l, int r, int u, int v, int val) { down(id, l, r); if (r < u || v < l) return ; if (u <= l && r <= v) { ST[id].value = max(ST[id].value, val); if (l != r) { ST[id * 2].lazy = max(ST[id * 2].lazy, val); ST[id * 2 + 1].lazy = max(ST[id * 2 + 1].lazy, val); } return ; } int mid = (l + r) / 2; update(id * 2, l, mid, u, v, val); update(id * 2 + 1, mid + 1, r, u, v, val); ST[id] = ST[id * 2] + ST[id * 2 + 1]; } int get(int id, int l, int r, int u, int v) { down(id, l, r); if (r < u || v < l) return - inf; if (u <= l && r <= v) return ST[id].value; int mid = (l + r) / 2; return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v)); } } st[2]; void main() { vector<int> vtx; FOR(i, 1, n) vtx.push_back(store[i].x); REP(i, q) vtx.push_back(qu[i].l); sort(vtx.begin(), vtx.end()); vtx.resize(unique(vtx.begin(), vtx.end()) - vtx.begin()); vector<multiset<int>> mts(k + 1); FOR(i, 1, n) mts[store[i].t].insert(lower_bound(vtx.begin(), vtx.end(), store[i].x) - vtx.begin()); st[0].init(n), st[1].init(n); FOR(i, 1, k) { if (mts[i].empty()) continue; st[0].update(1, 0, _size(vtx) - 1, 0, *mts[i].begin(), vtx[*mts[i].begin()]); for (auto it = mts[i].begin(); it != mts[i].end(); it++) { auto nit = it; nit++; if (nit == mts[i].end()) break; int id = *it, nid = *nit; int pos = lower_bound(vtx.begin(), vtx.end(), (vtx[id] + vtx[nid] + 1) / 2) - vtx.begin(); st[0].update(1, 0, _size(vtx) - 1, pos, nid, vtx[nid]); if (pos > id) st[1].update(1, 0, _size(vtx) - 1, id, pos - 1, - vtx[id]); } auto it = mts[i].end(); it--; st[1].update(1, 0, _size(vtx) - 1, *it, _size(vtx) - 1, - vtx[*it]); } vector<pair<int, int>> g; FOR(i, 1, n) g.push_back({store[i].b, - i}); REP(i, q) g.push_back({qu[i].y, i}); sort(g.begin(), g.end(), [&] (const pair<int, int> a, pair<int, int> b) { return a.fi != b.fi ? a.fi < b.fi : a.se > b.se; }); int ok = 0; vector<int> cnt(k + 1), ans(q); FOR(i, 1, n) cnt[store[i].t]++; FOR(i, 1, k) ok += (!cnt[i]); FORALL(e, g) { if (e.se > 0) { if (ok) ans[e.se] = - 1; else { int pos = lower_bound(vtx.begin(), vtx.end(), qu[e.se].l) - vtx.begin(); ans[e.se] = max(st[0].get(1, 0, _size(vtx) - 1, pos, pos) - vtx[pos], vtx[pos] + st[1].get(1, 0, _size(vtx) - 1, pos, pos)); } } else break; } REP(i, q) cout << ans[i] << '\n'; } } int main() { fastio; cin >> n >> k >> q; store.resize(n + 1); FOR(i, 1, n) { int x, t, a, b; cin >> x >> t >> a >> b; store[i] = {x, t, a, b}; } qu.resize(q); REP(i, q) { cin >> qu[i].l >> qu[i].y; qu[i].id = i; } if (max(n, q) <= 60000 && k <= 400) subtask12::main(); else if (checkSub34()) subtask34::main(); return 0; }
#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...