제출 #852387

#제출 시각아이디문제언어결과실행 시간메모리
852387jerzyk자리 배치 (IOI18_seats)C++17
100 / 100
2161 ms120704 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include "seats.h" #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 1<<20; int n, val = 4, m, nm; int drz[2 * N], il[2 * N], laz[2 * N]; vector<int> tab[N]; pair<int, int> wh[N]; void Change(int v, int p, int k, int pz, int kz, int x) { if(p > kz || k < pz) return; if(p >= pz && k <= kz) { drz[v] += x; laz[v] += x; return; } int m = (p + k) / 2; Change(v * 2, p, m, pz, kz, x); Change(v * 2 + 1, m + 1, k, pz, kz, x); drz[v] = min(drz[v * 2], drz[v * 2 + 1]); il[v] = 0; if(drz[v] == drz[v * 2]) il[v] += il[v * 2]; if(drz[v] == drz[v * 2 + 1]) il[v] += il[v * 2 + 1]; drz[v] += laz[v]; } inline void CH(int a, int b, int x) {if(a <= b) Change(1, 0, N - 1, a, b, x);} inline void HN(vector<vector<int>> v, int x) { if(v[0][0] > v[1][1]) swap(v[0][0], v[1][1]); if(v[0][1] > v[1][0]) swap(v[0][1], v[1][0]); if(v[0][1] < v[0][0]) {swap(v[0][0], v[0][1]); swap(v[1][0], v[1][1]);} //cout << "\n" << v[0][0] << " " << v[0][1] << "\n"; //cout << v[1][0] << " " << v[1][1] << "\n"; CH(v[0][0], min(v[0][1], v[1][1]) - 1, x); CH(min(v[1][1], v[1][0]), max(v[1][1], v[1][0]) - 1, x * 5); } inline void HLP(int i, int j, int d) { vector<int> x(2, 0); vector<vector<int>> v(2, x); v[0][0] = tab[i][j]; v[0][1] = tab[i][j + 1]; v[1][0] = tab[i + 1][j]; v[1][1] = tab[i + 1][j + 1]; HN(v, d); } void Query(int a, int b) { ++a; ++b; pair<int, int> x = wh[a], y = wh[b]; vector<pair<int, int>> pos; for(int i = x.st - 1; i <= x.st; ++i) for(int j = x.nd - 1; j <= x.nd; ++j) pos.pb(make_pair(i, j)); for(int i = y.st - 1; i <= y.st; ++i) for(int j = y.nd - 1; j <= y.nd; ++j) pos.pb(make_pair(i, j)); sort(pos.begin(), pos.end()); for(int i = 0; i < (int)pos.size(); ++i) if(i == 0 || pos[i] != pos[i - 1]) HLP(pos[i].st, pos[i].nd, -1); swap(tab[x.st][x.nd], tab[y.st][y.nd]); wh[a] = y; wh[b] = x; for(int i = 0; i < (int)pos.size(); ++i) if(i == 0 || pos[i] != pos[i - 1]) HLP(pos[i].st, pos[i].nd, 1); } int Ans() { int ans = 0; if(drz[1] == val) ans += il[1]; return ans; } void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) { n = H; m = W; nm = n * m; vector<int> p1(m + 5, 0), p2(m + 5, nm + 10); p1[0] = nm + 10; p1[m + 1] = nm + 10; tab[0] = p2; tab[n + 1] = p2; for(int i = 1; i <= n; ++i) tab[i] = p1; for(int i = N; i < 2 * N; ++i) il[i] = 1; drz[N] = N; for(int i = N + nm + 1; i < 2 * N; ++i) drz[i] = N; for(int v = N - 1; v > 0; --v) { drz[v] = min(drz[v * 2], drz[v * 2 + 1]); if(drz[v] == drz[v * 2]) il[v] += il[v * 2]; if(drz[v] == drz[v * 2 + 1]) il[v] += il[v * 2 + 1]; } for(int i = 1; i <= nm; ++i) { tab[R[i - 1] + 1][C[i - 1] + 1] = i; wh[i] = make_pair(R[i - 1] + 1, C[i - 1] + 1); } vector<int> x(2, 0); vector<vector<int>> v(2, x); for(int i = 0; i <= n; ++i) { for(int j = 0; j <= m; ++j) { v[0][0] = tab[i][j]; v[0][1] = tab[i][j + 1]; v[1][0] = tab[i + 1][j]; v[1][1] = tab[i + 1][j + 1]; HN(v, 1); } } //cout << "XD " << Ans() << "\n"; } int swap_seats(int a, int b) { Query(a, b); 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...