Submission #144288

#TimeUsernameProblemLanguageResultExecution timeMemory
144288MinnakhmetovSeats (IOI18_seats)C++14
17 / 100
4094 ms75796 KiB
#include "seats.h" #include<bits/stdc++.h> using namespace std; const int N = 1e6 + 5, INF = 1e9; int n, m; vector<int> x, y; int lx[N], rx[N], ly[N], ry[N], num[N]; bool rect[N], tmp[N]; int ans = 0; int swap_seats_slow(int a, int b) { swap(x[a], x[b]); swap(y[a], y[b]); for (int i = a + 1; i <= b + 1; i++) { lx[i] = min(lx[i - 1], x[i - 1]); rx[i] = max(rx[i - 1], x[i - 1]); ly[i] = min(ly[i - 1], y[i - 1]); ry[i] = max(ry[i - 1], y[i - 1]); if (rect[i]) ans--; if ((rx[i] - lx[i] + 1) * (ry[i] - ly[i] + 1) == i) { rect[i] = 1; ans++; } else { rect[i] = 0; } } return ans; } struct Node { int mn, ct, up; } t[N * 4]; Node operator + (Node a, Node b) { if (a.mn == b.mn) { a.ct += b.ct; return a; } if (a.mn < b.mn) return a; return b; } void push(int v, int L, int R) { if (t[v].up) { if (L != R) { t[v * 2].up += t[v].up; t[v * 2 + 1].up += t[v].up; } t[v].mn += t[v].up; t[v].up = 0; } } void upd(int l, int r, int x, int v, int L, int R) { push(v, L, R); if (l > r) return; if (l == L && r == R) { t[v].up += x; push(v, L, R); } else { int m = (L + R) >> 1; upd(l, min(m, r), x, v * 2, L, m); upd(max(m + 1, l), r, x, v * 2 + 1, m + 1, R); t[v] = t[v * 2] + t[v * 2 + 1]; } } void change(int a, int d) { if (y[a] > 0) upd(max(a, num[y[a] - 1]), m - 1, d, 1, 0, m - 1); if (y[a] < m - 1) upd(max(a, num[y[a] + 1]), m - 1, d, 1, 0, m - 1); } int swap_seats_line(int a, int b) { change(a, 1); change(b, 1); if (abs(y[a] - y[b]) == 1) { upd(max(num[a], num[b]), m - 1, -1, 1, 0, m - 1); } swap(num[y[a]], num[y[b]]); swap(y[a], y[b]); change(a, -1); change(b, -1); if (abs(y[a] - y[b]) == 1) { upd(max(num[a], num[b]), m - 1, 1, 1, 0, m - 1); } push(1, 0, m - 1); return t[1].mn == 1 ? t[1].ct : 0; } void init_slow() { lx[0] = INF; rx[0] = -1; ly[0] = INF; ry[0] = -1; for (int i = 1; i <= n * m; i++) { lx[i] = min(lx[i - 1], x[i - 1]); rx[i] = max(rx[i - 1], x[i - 1]); ly[i] = min(ly[i - 1], y[i - 1]); ry[i] = max(ry[i - 1], y[i - 1]); if ((rx[i] - lx[i] + 1) * (ry[i] - ly[i] + 1) == i) { rect[i] = 1; ans++; } } } void init_line() { int cur = 0; fill(t, t + m * 4, (Node){0, 1, 0}); for (int i = 0; i < m; i++) { num[y[i]] = i; cur++; tmp[y[i]] = 1; if (y[i] > 0 && tmp[y[i] - 1]) cur--; if (y[i] + 1 < m && tmp[y[i] + 1]) cur--; upd(i, i, cur, 1, 0, m - 1); } } void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) { n = H; m = W; x = R; y = C; if (n > 1) init_slow(); else init_line(); } int swap_seats(int a, int b) { if (a > b) swap(a, b); if (n > 1) { return swap_seats_slow(a, b); } return swap_seats_line(a, b); }
#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...