Submission #758494

#TimeUsernameProblemLanguageResultExecution timeMemory
758494drdilyorSeats (IOI18_seats)C++17
0 / 100
424 ms56180 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; struct SegmentTree { using S = int; struct T { int sum=0; int min=0; int minc=0; }; const T id = {}; inline T single(S v) { return {max(v, 0), v, 1}; } 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}; } 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 = r.min, .minc = r.minc}; } } int n; vector<T> tree; SegmentTree(int n) : n(n) { tree.resize(n * 2, id); build(); } SegmentTree(vector<S> arr) : n(arr.size()) { tree.resize(n * 2, id); for (int i = 0; i < n; i++) { tree[i + n] = single(arr[i]); } 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]); } T query(int l, int r) { T left = id, right = id; l += n; r += n; while (l <= r) { if (l % 2 == 1) left = merge(left, tree[l++]); if (r % 2 == 0) right = merge(right, tree[r--]); l /= 2; r /= 2; } return merge(left, right); } int find_first(function<bool(T&)> predicate, int last = 0) { int v = 1; while (v < n) { if (predicate(tree[v*2 + last])) v = v*2 + last; else if (predicate(tree[v*2 + !last])) v = v*2 + !last; else return -1; } return v; } }; vector<int> pot; void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) { 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); } #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]); assert(h == 1); int ans = 2, cnt = 0; auto& brr = arr[0]; auto add = [&](int i) -> void { int j = c[i]; if (j == 0 || brr[j-1] > i) cnt++; else cnt--; if (j < w -1 || brr[j+1] > i) cnt++; else cnt--; }; for (int i = 1; i < h*w-1; i++) { add(i); if (cnt == 0) ans++; } 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...