Submission #560537

#TimeUsernameProblemLanguageResultExecution timeMemory
560537hoanghq2004New Home (APIO18_new_home)C++14
57 / 100
5097 ms685444 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 3e5 + 10; const int M = 3e8 + 10; int n, k, q, m; multiset <int> s[N]; int ans[N]; int cnt; int dta[N * 20]; void fake_add(int t, int x) { int L = *(--s[t].upper_bound(x)); int R = *s[t].upper_bound(x); dta[m++] = (L + R) / 2; dta[m++] = (L + x) / 2; dta[m++] = (x + R) / 2; s[t].insert(x); } void fake_rev(int t, int x) { s[t].erase(s[t].find(x)); int L = *(--s[t].upper_bound(x)); int R = *s[t].upper_bound(x); dta[m++] = (L + R) / 2; dta[m++] = (L + x) / 2; dta[m++] = (x + R) / 2; } map <pair <int, int>, vector <int> > id[N]; int ti; vector <tuple <int, int, int, int> > work[N * 4]; void push(int id, int L, int R, int u, int v, int x, int y, int a, int b) { if (u > R || L > v || u > v) return; if (u <= L && R <= v) { work[id].push_back({x, y, a, b}); return; } int mid = L + R >> 1; push(id * 2, L, mid, u, v, x, y, a, b); push(id * 2 + 1, mid + 1, R, u, v, x, y, a, b); } inline void add(int t, int X) { cnt -= (s[t].size() == 2); int L = *(--s[t].upper_bound(X)); int R = *s[t].upper_bound(X); int l = lower_bound(dta, dta + m, L) - dta; int r = lower_bound(dta, dta + m, R) - dta; int lr = lower_bound(dta, dta + m, (L + R) / 2) - dta; int i = id[t][{L, R}].back(); id[t][{L, R}].pop_back(); push(1, 1, q, i, ti - 1, l, lr, 1, - L); push(1, 1, q, i, ti - 1, lr + 1, r, -1, R); id[t][{L, X}].push_back(ti); id[t][{X, R}].push_back(ti); s[t].insert(X); } inline void rev(int t, int X) { s[t].erase(s[t].find(X)); int L = *(--s[t].upper_bound(X)); int R = *s[t].upper_bound(X); int l = lower_bound(dta, dta + m, L) - dta; int r = lower_bound(dta, dta + m, R) - dta; int x = lower_bound(dta, dta + m, X) - dta; int lx = lower_bound(dta, dta + m, (L + X) / 2) - dta; int rx = lower_bound(dta, dta + m, (X + R) / 2) - dta; id[t][{L, R}].push_back(ti); int i = id[t][{L, X}].back(); id[t][{L, X}].pop_back(); push(1, 1, q, i, ti - 1, l, lx, 1, - L); push(1, 1, q, i, ti - 1, lx + 1, x, - 1, X); i = id[t][{X, R}].back(); id[t][{X, R}].pop_back(); push(1, 1, q, i, ti - 1, x, rx, 1, - X); push(1, 1, q, i, ti - 1, rx + 1, r, - 1, R); cnt += (s[t].size() == 2); } struct SegmentTree { int st[N * 30]; int m, ti; tuple <int, int, int> old[N * 20]; SegmentTree() { memset(st, - 60, sizeof(st)); } inline void add(int id, int L, int R, int u, int v, int val) { if (u > R || L > v) return; if (u <= L && R <= v) { if (st[id] < val) { old[m++] = {ti, id, st[id]}; st[id] = max(st[id], val); } return; } int mid = L + R >> 1; add(id * 2, L, mid, u, v, val); add(id * 2 + 1, mid + 1, R, u, v, val); } inline void Set() { ++ti; } inline void rollback() { while (m > 0 && get<0>(old[m - 1]) == ti) { st[get<1>(old[m - 1])] = get<2>(old[m - 1]); --m; } --ti; } inline int calc(int id, int L, int R, int i) { int ans = st[id]; if (L == R) return ans; int mid = L + R >> 1; if (i <= mid) return max(calc(id * 2, L, mid, i), ans); else return max(calc(id * 2 + 1, mid + 1, R, i), ans); } } lft, rgt; int ask[N], wh[N]; inline void solve(int id, int L, int R) { lft.Set(), rgt.Set(); for (auto [u, v, a, b]: work[id]) { if (a < 0) lft.add(1, 0, m - 1, u, v, b); else rgt.add(1, 0, m - 1, u, v, b); } if (L == R) { if (wh[L]) { ans[wh[L]] = max(- dta[ask[L]] + lft.calc(1, 0, m - 1, ask[L]), dta[ask[L]] + rgt.calc(1, 0, m - 1, ask[L])); } } else { int mid = L + R >> 1; solve(id * 2, L, mid); solve(id * 2 + 1, mid + 1, R); } lft.rollback(), rgt.rollback(); } int main() { // freopen("test.inp", "r", stdin); // freopen("test.out", "w", stdout); ios :: sync_with_stdio(0); cin.tie(0); vector <tuple <int, int, int, int> > events; cin >> n >> k >> q; for (int i = 0; i < n; ++i) { int x, t, a, b; cin >> x >> t >> a >> b; x += 1e8 + 5; dta[m++] = x; events.push_back({a, -1, x, t}); events.push_back({b + 1, 1, x, t}); } sort(events.begin(), events.end()); for (int i = 1; i <= k; ++i) { s[i].insert(0), s[i].insert(M); int L = 0, R = M; dta[m++] = L, dta[m++] = R, dta[m++] = (L + R) / 2; } for (auto [_, cmd, x, t]: events) { if (cmd == -1) fake_add(t, x); else fake_rev(t, x); } cnt = k; vector <tuple <int, int, int> > work; for (int i = 1; i <= q; ++i) { int x, y; cin >> x >> y; x += 1e8 + 5; work.push_back({y, x, i}); dta[m++] = x; } sort(work.begin(), work.end()); sort(dta, dta + m); m = unique(dta, dta + m) - dta; for (int i = 1; i <= k; ++i) { int L = 0, R = M; id[i][{L, R}].push_back(1); } int i = 0; for (auto [y, x, id]: work) { ++ti; while (i < events.size() && get<0>(events[i]) <= y) { auto [_, cmd, x, t] = events[i]; if (cmd == -1) add(t, x); else rev(t, x); ++i; } x = lower_bound(dta, dta + m, x) - dta; ask[ti] = x; wh[ti] = id; if (cnt) ans[id] = -1, ask[ti] = 0, wh[ti] = 0; } ++ti; while (i < events.size()) { auto [_, cmd, x, t] = events[i]; if (cmd == -1) add(t, x); else rev(t, x); ++i; } for (int i = 1; i <= k; ++i) { int L = 0, R = M; int l = 0, r = m - 1; int m = lower_bound(dta, dta + m, (L + R) / 2) - dta; int _i = id[i][{L, R}].back(); id[i][{L, R}].pop_back(); push(1, 1, q, _i, ti - 1, l, m, 1, - L); push(1, 1, q, _i, ti - 1, m + 1, r, -1, R); } solve(1, 1, q); for (int i = 1; i <= q; ++i) cout << ans[i] << '\n'; }

Compilation message (stderr)

new_home.cpp: In function 'void push(int, int, int, int, int, int, int, int, int)':
new_home.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   51 |     int mid = L + R >> 1;
      |               ~~^~~
new_home.cpp: In member function 'void SegmentTree::add(int, int, int, int, int, int)':
new_home.cpp:113:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  113 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In member function 'int SegmentTree::calc(int, int, int, int)':
new_home.cpp:130:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  130 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In function 'void solve(int, int, int)':
new_home.cpp:140:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  140 |     for (auto [u, v, a, b]: work[id]) {
      |               ^
new_home.cpp:149:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  149 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:176:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  176 |     for (auto [_, cmd, x, t]: events) {
      |               ^
new_home.cpp:198:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  198 |     for (auto [y, x, id]: work) {
      |               ^
new_home.cpp:200:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  200 |         while (i < events.size() && get<0>(events[i]) <= y) {
      |                ~~^~~~~~~~~~~~~~~
new_home.cpp:201:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  201 |             auto [_, cmd, x, t] = events[i];
      |                  ^
new_home.cpp:212:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  212 |     while (i < events.size()) {
      |            ~~^~~~~~~~~~~~~~~
new_home.cpp:213:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  213 |         auto [_, cmd, x, t] = events[i];
      |              ^
new_home.cpp:221:40: warning: 'm' may be used uninitialized in this function [-Wmaybe-uninitialized]
  221 |         int m = lower_bound(dta, dta + m, (L + R) / 2) - dta;
      |                                        ^
#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...