Submission #758514

#TimeUsernameProblemLanguageResultExecution timeMemory
758514drdilyorSeats (IOI18_seats)C++17
0 / 100
343 ms56240 KiB
#include<bits/stdc++.h> #include "seats.h" using namespace std; using ll = long long; const int inf = 1e9; int h, w; std::vector<int> r, c; vector<vector<int>> arr; struct Fenwick { int n, m; vector<vector<ll>> t; Fenwick() = default; Fenwick(int n, int m) : n(n), m(m), t(n, vector<ll>(m)) {} ll sum(int r, int c) { ll s = 0; for (int i = r; i >= 0; i = (i&(i+1))-1) for (int j = c; j >= 0; j = (j&(j+1))-1) s += t[i][j]; return s; } ll sum(int r1, int c1, int r2, int c2) { return sum(r2, c2) + sum(r1-1, c1-1) - sum(r1-1, c2) - sum(r2, c1-1); } void inc(int r, int c, int d) { for (; r < n; r |= r+1) for (int i = c; i < m; i |= i+1) t[r][i] += d; } }; Fenwick ft; int nextPower2(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return 1+n; } struct SegmentTree { using S = int; struct T { int sum=0; int min=0; int minc=0; }; const T id = {1, 1, 0}; inline T single(S v) { return {v, min(v, 0), 1 + (v==0)}; } static T merge(const T& l, const T& r) { if (l.min == l.sum + r.min) { return {.sum = l.sum + r.sum, .min = l.min, .minc = l.minc + r.minc - (l.min == 0)}; } else if (l.min < l.sum + r.min) { return {.sum = l.sum + r.sum, .min = l.min, .minc = l.minc}; } else { return {.sum = l.sum + r.sum, .min = l.sum + r.min, .minc = r.minc}; } } int n; vector<T> tree; SegmentTree() {} void init(int n) { this->n = n = nextPower2(n); tree.assign(n * 2, id); build(); } void build() { for (int i = n-1; i >= 1; i--) { tree[i] = merge(tree[i*2], tree[i*2 + 1]); } } void update(int i, S v) { tree[i+=n] = single(v); for (i /= 2; i >= 1; i/= 2) tree[i] = merge(tree[i*2], tree[i*2+1]); } int query() { return tree[1].minc; } }; SegmentTree pot; int add(int i) { if (i == 0) return 0; auto& brr = arr[0]; int j = c[i]; int cnt = 0; if (j == 0 || brr[j-1] > i) cnt++; else cnt--; if (j == w -1 || brr[j+1] > i) cnt++; else cnt--; return cnt; }; void test() { const int n = 10; SegmentTree st; st.init(n); vector<int> arr(n); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); st.update(0, 0); for (int i = 1, s = 0; i < n; i++) { int c; do { c = uniform_int_distribution<int>(-2, 2)(rng); } while(s + c < 0); arr[i] = c; st.update(i, c); s += c; } } void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) { test(); h = H; w = W; r = R; c = C; arr = vector(H, vector<int>(W, 0)); ft = Fenwick(H, W); for (int i = 0; i < H * W; i++) { arr[r[i]][c[i]] = i; ft.inc(r[i], c[i], i); } pot.init(w); for (int i = 0; i < w; i++) { pot.update(i, add(i)); } #ifdef ONPC for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cout << arr[i][j] << " \n"[j == W-1]; #endif } int swap_seats(int a, int b) { ft.inc(r[a], c[a], -a +b); ft.inc(r[b], c[b], -b +a); swap(arr[r[a]][c[a]], arr[r[b]][c[b]]); swap(r[a], r[b]); swap(c[a], c[b]); for (int i : {a-1, a, a+1, b-1, b, b+1}) { if (0 <= i && i <= w-1) pot.update(i, add(i)); } //for (int i = 0; i < w;i++) cout << pot.tree[pot.n + i].sum << ' ';cout<<endl; assert(h == 1); int ans = pot.query() - 1; 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...