제출 #1067106

#제출 시각아이디문제언어결과실행 시간메모리
1067106TheQuantiX자리 배치 (IOI18_seats)C++17
37 / 100
4078 ms102484 KiB
#include "seats.h" #include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") using namespace std; using ll = int; constexpr ll INF = 1000000000LL; ll n, m, q, k, x, y, a, b, c; pair<ll, ll> v[1000001]; pair<ll, ll> h[1000001]; pair<ll, ll> w[1000001]; ll ans = 0; struct segtree { ll n; array< pair<ll, ll>, 4000001 > mintr; array< pair<ll, ll>, 4000001 > maxtr; void build(ll x, ll l, ll r) { // cout << x << ' ' << l << ' ' << r << endl; if (l == r) { mintr[x] = {v[l].first, v[l].second}; maxtr[x] = {v[l].first, v[l].second}; // cout << x << ' ' << mintr[x] << ' ' << maxtr[x] << '\n'; return; } ll mid = (r + l) / 2; build(x * 2 + 1, l, mid); build(x * 2 + 2, mid + 1, r); mintr[x] = {min(mintr[x * 2 + 1].first, mintr[x * 2 + 2].first), min(mintr[x * 2 + 1].second, mintr[x * 2 + 2].second)}; maxtr[x] = {max(maxtr[x * 2 + 1].first, maxtr[x * 2 + 2].first), max(maxtr[x * 2 + 1].second, maxtr[x * 2 + 2].second)}; // cout << x << ' ' << mintr[x] << ' ' << maxtr[x] << '\n'; } segtree() : n(0) {} void resize(ll N) { n = N; build(0, 0, n - 1); } void upd(ll x, ll l, ll r, ll idx, pair<ll, ll> val) { if (l == r) { mintr[x] = {val.first, val.second}; maxtr[x] = {val.first, val.second}; return; } ll mid = (r + l) / 2; if (idx <= mid) { upd(x * 2 + 1, l, mid, idx, val); } else { upd(x * 2 + 2, mid + 1, r, idx, val); } mintr[x] = {min(mintr[x * 2 + 1].first, mintr[x * 2 + 2].first), min(mintr[x * 2 + 1].second, mintr[x * 2 + 2].second)}; maxtr[x] = {max(maxtr[x * 2 + 1].first, maxtr[x * 2 + 2].first), max(maxtr[x * 2 + 1].second, maxtr[x * 2 + 2].second)}; } pair< pair<ll, ll>, pair<ll, ll> > get(ll x, ll l, ll r, ll L, ll R) { if (r < l) { return {{-INF, -INF}, {INF, INF}}; } if (r < L) { return {{-INF, -INF}, {INF, INF}}; } if (R < l) { return {{-INF, -INF}, {INF, INF}}; } if (L <= l && r <= R) { // cout << x << ' ' << l << ' ' << r << ' ' << L << ' ' << R << '\n'; // cout << maxtr[x] << '\n'; return {maxtr[x], mintr[x]}; } ll mid = (r + l) / 2; auto p1 = get(x * 2 + 1, l, mid, L, R), p2 = get(x * 2 + 2, mid + 1, r, L, R); return {{max(p1.first.first, p2.first.first), max(p1.first.second, p2.first.second)}, {min(p1.second.first, p2.second.first), min(p1.second.second, p2.second.second)}}; } }; segtree sg; void give_initial_chart(int H, int W, vector<int> R, vector<int> C) { n = H; m = W; for (int i = 0; i < n * m; i++) { v[i] = {R[i], C[i]}; } if (n <= 1000 && m <= 1000) { sg.resize(n * m); } else { for (int i = 0; i <= n * m; i++) { h[i] = {INF, -INF}; w[i] = {INF, -INF}; } for (int i = 1; i <= n * m; i++) { h[i].first = min(h[i - 1].first, v[i - 1].first); h[i].second = max(h[i - 1].second, v[i - 1].first); w[i].first = min(w[i - 1].first, v[i - 1].second); w[i].second = max(w[i - 1].second, v[i - 1].second); } for (int i = 1; i <= n * m; i++) { if ((h[i].second - h[i].first + 1) * (w[i].second - w[i].first + 1) == i) { ans++; } } } } int swap_seats(int a, int b) { if (n <= 1000 && m <= 1000) { swap(v[a], v[b]); sg.upd(0, 0, n * m - 1, a, v[a]); sg.upd(0, 0, n * m - 1, b, v[b]); // pair<ll, ll> mn1 = sg.getmn(0, 0, n * m - 1, 0, 1); // pair<ll, ll> mx1 = sg.getmx(0, 0, n * m - 1, 0, 1); // cout << mn1.first << ' ' << mx1.first << '\n'; // cout << mn1.second << ' ' << mx1.second << endl; int ans = 1; pair<ll, ll> mn = {v[0].first, v[0].second}; pair<ll, ll> mx = {v[0].first, v[0].second}; while ((mx.first - mn.first + 1) * (mx.second - mn.second + 1) != n * m) { ll idx = (mx.first - mn.first + 1) * (mx.second - mn.second + 1); // cout << idx << endl; pair< pair<ll, ll>, pair<ll, ll> > p = {v[idx], v[idx]}; mx = {max(mx.first, p.first.first), max(mx.second, p.first.second)}; mn = {min(mn.first, p.second.first), min(mn.second, p.second.second)}; while ((mx.first - mn.first + 1) * (mx.second - mn.second + 1) != idx + 1) { auto p = sg.get(0, 0, n * m - 1, idx + 1, (mx.first - mn.first + 1) * (mx.second - mn.second + 1) - 1); idx = (mx.first - mn.first + 1) * (mx.second - mn.second + 1) - 1; mx = {max(mx.first, p.first.first), max(mx.second, p.first.second)}; mn = {min(mn.first, p.second.first), min(mn.second, p.second.second)}; } ans++; } return ans; } else { if (a > b) { swap(a, b); } for (int i = a + 1; i <= b + 1; i++) { // cout << i << ' ' << (h[i].second - h[i].first + 1) << ' ' << (w[i].second - w[i].first + 1) << '\n'; if ((h[i].second - h[i].first + 1) * (w[i].second - w[i].first + 1) == i) { ans--; } } swap(v[a], v[b]); for (int i = a + 1; i <= b + 1; i++) { h[i].first = min(h[i - 1].first, v[i - 1].first); h[i].second = max(h[i - 1].second, v[i - 1].first); w[i].first = min(w[i - 1].first, v[i - 1].second); w[i].second = max(w[i - 1].second, v[i - 1].second); // cout << i << ' ' << (h[i].second - h[i].first + 1) << ' ' << (w[i].second - w[i].first + 1) << '\n'; if ((h[i].second - h[i].first + 1) * (w[i].second - w[i].first + 1) == i) { ans++; } } return ans; } }
#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...