제출 #229977

#제출 시각아이디문제언어결과실행 시간메모리
229977fedoseevtimofey자리 배치 (IOI18_seats)C++14
37 / 100
4070 ms166652 KiB
#include "seats.h" #include <iostream> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <random> #include <iomanip> #include <functional> #include <cassert> using namespace std; typedef long long ll; const int N = 1e6 + 7; const int Inf = 1e9 + 7; struct SegmentTree { pair <int, int> t[4 * N]; int mod[4 * N]; void update(int v, int val) { t[v].first += val; mod[v] += val; } void push(int v) { if (mod[v]) { update(2 * v + 1, mod[v]); update(2 * v + 2, mod[v]); mod[v] = 0; } } void pull(int v) { pair <int, int> res; res.first = min(t[2 * v + 1].first, t[2 * v + 2].first); res.second = 0; if (res.first == t[2 * v + 1].first) res.second += t[2 * v + 1].second; if (res.first == t[2 * v + 2].first) res.second += t[2 * v + 2].second; t[v] = res; } void build(int l, int r, int v, vector <int> &a) { if (l == r) { t[v] = {a[l], 1}; mod[v] = 0; } else { int m = (l + r) >> 1; build(l, m, 2 * v + 1, a); build(m + 1, r, 2 * v + 2, a); pull(v); mod[v] = 0; } } void modify(int ql, int qr, int val, int l, int r, int v) { if (l == ql && r == qr) { update(v, val); return; } int m = (l + r) >> 1; push(v); if (qr <= m) { modify(ql, qr, val, l, m, 2 * v + 1); } else if (ql > m) { modify(ql, qr, val, m + 1, r, 2 * v + 2); } else { modify(ql, m, val, l, m, 2 * v + 1); modify(m + 1, qr, val, m + 1, r, 2 * v + 2); } pull(v); } int get(int pos, int l, int r, int v) { if (l == r) { return t[v].first; } else { push(v); int m = (l + r) >> 1; if (pos <= m) return get(pos, l, m, 2 * v + 1); else return get(pos, m + 1, r, 2 * v + 2); } } }; vector <vector <int>> a; vector <int> r, c; int n, m; SegmentTree tr, str; bool ok(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } vector <int> dx = {0, -1, 0, 1}; vector <int> dy = {-1, 0, 1, 0}; int tmm(int x, int y, int k) { int nx = x + dx[k]; int ny = y + dy[k]; if (ok(nx, ny)) return a[nx][ny]; return n * m; } void add(int x, int y, int s) { vector <int> who; for (int k = 0; k < 4; ++k) { int nx = x + dx[k]; int ny = y + dy[k]; if (ok(nx, ny)) { who.push_back(a[nx][ny]); } } sort(who.begin(), who.end()); { int l_good = 0; int r_good = a[x][y] - 1; if ((int)who.size() >= 2) { r_good = min(r_good, who[1] - 1); } if (l_good <= r_good) { tr.modify(l_good, r_good, -1 * s, 0, n * m - 1, 0); } l_good = a[x][y]; if ((int)who.size() >= 2) { l_good = max(l_good, who[1]); } else { l_good = Inf; } r_good = n * m - 1; if ((int)who.size() >= 3) { r_good = who[2] - 1; } if (l_good <= r_good) { tr.modify(l_good, r_good, 1 * s, 0, n * m - 1, 0); } l_good = a[x][y]; r_good = n * m - 1; if ((int)who.size() >= 2) { r_good = who[1] - 1; } if (l_good <= r_good) { tr.modify(l_good, r_good, 100 * s, 0, n * m - 1, 0); } } { int l_good = 0; int r_good = a[x][y] - 1; if ((int)who.size() >= 2) { r_good = min(r_good, who[1] - 1); } if (l_good <= r_good) { str.modify(l_good, r_good, -1 * s, 0, n * m - 1, 0); } int ml = n * m; for (int i = 0; i < 4; ++i) { int l = max(a[x][y], max(tmm(x, y, i), tmm(x, y, (i + 1) % 4))); ml = min(ml, l); } if (ml <= n * m - 1) str.modify(ml, n * m - 1, 100 * s, 0, n * m - 1, 0); l_good = a[x][y]; r_good = n * m - 1; if ((int)who.size() >= 1) { l_good = max(l_good, who[0]); } else { l_good = Inf; } if ((int)who.size() >= 2) { r_good = who[1] - 1; } if (l_good <= r_good) { str.modify(l_good, r_good, 1 * s, 0, n * m - 1, 0); } l_good = a[x][y]; r_good = n * m - 1; if ((int)who.size() >= 1) { r_good = who[0] - 1; } if (l_good <= r_good) { str.modify(l_good, r_good, 100 * s, 0, n * m - 1, 0); } } } void give_initial_chart(int N, int M, vector<int> R, vector<int> C) { n = N, m = M; r = R, c = C; a.resize(n, vector <int> (m)); for (int i = 0; i < n * m; ++i) { a[r[i]][c[i]] = i; } vector <int> tr_start(n * m); for (int i = 0; i < n * m; ++i) { tr_start[i] = n * m - (i + 1) - 4; } vector <int> str_start(n * m); for (int i = 0; i < n * m; ++i) { str_start[i] = n * m - (i + 1); if (i != 0) { str_start[i] -= min(2, i + 1); } else { str_start[i] -= 100; } } tr.build(0, n * m - 1, 0, tr_start); str.build(0, n * m - 1, 0, str_start); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { add(i, j, 1); } } } int swap_seats(int x, int y) { set <pair <int, int>> go; for (int k = 0; k < 4; ++k) { int nx = r[x] + dx[k]; int ny = c[x] + dy[k]; if (ok(nx, ny)) { go.insert({nx, ny}); } } go.insert({r[x], c[x]}); for (int k = 0; k < 4; ++k) { int nx = r[y] + dx[k]; int ny = c[y] + dy[k]; if (ok(nx, ny)) { go.insert({nx, ny}); } } go.insert({r[y], c[y]}); for (auto p : go) { add(p.first, p.second, -1); } swap(a[r[x]][c[x]], a[r[y]][c[y]]); swap(r[x], r[y]); swap(c[x], c[y]); for (auto p : go) { add(p.first, p.second, 1); } int ans = (tr.t[0].first == 0 ? tr.t[0].second : 0) + (str.t[0].first == 0 ? str.t[0].second : 0); return ans; } #ifdef LOCAL int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n, m, q; cin >> n >> m >> q; vector <int> R(n * m), C(n * m); for (int i = 0; i < n * m; ++i) { cin >> R[i] >> C[i]; } vector <int> aq(q), bq(q); for (int i = 0; i < q; ++i) { cin >> aq[i] >> bq[i]; } double st = clock(); give_initial_chart(n, m, R, C); for (int j = 0; j < q; ++j) { int ans = swap_seats(aq[j], bq[j]); //cout << ans << '\n'; } #ifdef LOCAL cout << (double)(clock() - st) / CLOCKS_PER_SEC << '\n'; #endif } #endif
#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...