Submission #394006

#TimeUsernameProblemLanguageResultExecution timeMemory
394006phathnvSeats (IOI18_seats)C++11
0 / 100
478 ms146236 KiB
#include <bits/stdc++.h> #include "seats.h" using namespace std; struct Node{ int minVal, cntMin; Node(int x = 0, int cnt = 1){ minVal = x; cntMin = cnt; } void update(int x){ minVal += x; } Node operator + (const Node &oth){ if (minVal < oth.minVal) return *this; if (minVal > oth.minVal) return oth; return Node(minVal, cntMin + oth.cntMin); } }; struct SegmentTree{ int n; vector<int> lazy; vector<Node> d; void build(int id, int l, int r){ lazy[id] = 0; if (l == r){ d[id] = Node(0, 1); return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); d[id] = d[id << 1] + d[id << 1 | 1]; } void init(int _n){ n = _n; d.assign(n * 4 + 1, Node(0, 1)); lazy.assign(n * 4 + 1, 0); } void doLazy(int id, int l, int r){ d[id].update(lazy[id]); if (l < r){ lazy[id << 1] += lazy[id]; lazy[id << 1 | 1] += lazy[id]; } lazy[id] = 0; } void update(int id, int l, int r, int u, int v, int val){ doLazy(id, l, r); if (v < l || r < u) return; if (u <= l && r <= v){ lazy[id] += val; doLazy(id, l, r); return; } int mid = (l + r) >> 1; update(id << 1, l, mid, u, v, val); update(id << 1 | 1, mid + 1, r, u, v, val); d[id] = d[id << 1] + d[id << 1 | 1]; } void update(int pos, int val){ update(1, 0, n - 1, pos, n - 1, val); } int getCnt(){ doLazy(1, 0, n - 1); assert(d[1].minVal == 2); return d[1].cntMin; } } st; int h, w; vector<vector<int>> chair; vector<int> x, y; bool inSide(int x, int y){ return (0 <= x && x < h && 0 <= y && y < w); } void UpdateSquare(int x, int y, int type){ vector<int> vals; for(int dx = 0; dx < 2; dx++) for(int dy = 0; dy < 2; dy++) if (inSide(x + dx, y + dy)) vals.push_back(chair[x + dx][y + dy]); sort(vals.begin(), vals.end()); for(int x : vals){ st.update(x, type); type *= -1; } } void give_initial_chart(int h, int w, vector<int> r, vector<int> c) { chair.assign(h, vector<int>(w, 0)); x = r; y = c; for(int i = 0; i < w * h; i++) chair[r[i]][c[i]] = i; st.init(h * w); for(int i = -1; i < h; i++) for(int j = -1; j < w; j++) UpdateSquare(i, j, 1); } int swap_seats(int a, int b) { for(int dx = -1; dx < 1; dx++) for(int dy = -1; dy < 1; dy++){ UpdateSquare(x[a] + dx, y[a] + dy, -1); UpdateSquare(x[b] + dx, y[b] + dy, -1); } swap(chair[x[a]][y[a]], chair[x[b]][y[b]]); swap(x[a], x[b]); swap(y[a], y[b]); for(int dx = -1; dx < 1; dx++) for(int dy = -1; dy < 1; dy++){ UpdateSquare(x[a] + dx, y[a] + dy, +1); UpdateSquare(x[b] + dx, y[b] + dy, +1); } return st.getCnt(); }
#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...