|
| 1 | +/* |
| 2 | +CEOI 2014 Cake |
| 3 | +- Let's assume WLOG that b < a in each query |
| 4 | +- Also, let d[0] = d[N + 1] = INF |
| 5 | +- The answer for a query is simply |
| 6 | + min(c : max(d[a : c]) >= max(d[b : a])) - b |
| 7 | +- We can use a segtree to store range minimums and walk down it to get c |
| 8 | +- To handle an update efficiently, decrement the "rank" of the e - 1 tastiest |
| 9 | + pieces and set the |
| 10 | + - The "ranks" may become negative, but we only care about relative order |
| 11 | +- We can use a std::set to store the order of the cakes |
| 12 | +- Complexity: O(max(e) * Q log N) |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <bits/stdc++.h> |
| 16 | +typedef long long ll; |
| 17 | +using namespace std; |
| 18 | + |
| 19 | +int d[250001], segtree[2][1000001]; |
| 20 | + |
| 21 | +void update(int *seg, int pos, int val, int node, int l, int r) { |
| 22 | + if (l == r) seg[node] = val; |
| 23 | + else { |
| 24 | + int mid = (l + r) / 2; |
| 25 | + if (pos > mid) update(seg, pos, val, node * 2 + 1, mid + 1, r); |
| 26 | + else update(seg, pos, val, node * 2, l, mid); |
| 27 | + seg[node] = max(seg[node * 2], seg[node * 2 + 1]); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +int range_max(int *seg, int a, int b, int node, int l, int r) { |
| 32 | + if (l > b || r < a) return 0; |
| 33 | + if (l >= a && r <= b) return seg[node]; |
| 34 | + int mid = (l + r) / 2; |
| 35 | + return max(range_max(seg, a, b, node * 2, l, mid), range_max(seg, a, b, node * 2 + 1, mid + 1, r)); |
| 36 | +} |
| 37 | + |
| 38 | +int walk(int *seg, int threshold, int node, int l, int r) { |
| 39 | + if (threshold > seg[node]) return r; |
| 40 | + if (l == r) return l - (seg[node] > threshold); |
| 41 | + int mid = (l + r) / 2; |
| 42 | + if (seg[node * 2] >= threshold) return walk(seg, threshold, node * 2, l, mid); |
| 43 | + return walk(seg, threshold, node * 2 + 1, mid + 1, r); |
| 44 | +} |
| 45 | + |
| 46 | +int main() { |
| 47 | + cin.tie(0)->sync_with_stdio(0); |
| 48 | + int n, a; |
| 49 | + cin >> n >> a; |
| 50 | + vector<int> top_ten; |
| 51 | + for (int i = 1; i <= n; i++) { |
| 52 | + cin >> d[i]; |
| 53 | + top_ten.push_back(i); |
| 54 | + } |
| 55 | + sort(top_ten.begin(), top_ten.end(), [](int A, int B) { return d[A] > d[B]; }); |
| 56 | + while (top_ten.size() > 10) top_ten.pop_back(); |
| 57 | + |
| 58 | + for (int i = a + 1; i <= n; i++) update(segtree[0], i - a, d[i], 1, 1, n - a); |
| 59 | + for (int i = a - 1; i; i--) update(segtree[1], a - i, d[i], 1, 1, a - 1); |
| 60 | + |
| 61 | + int q; |
| 62 | + cin >> q; |
| 63 | + while (q--) { |
| 64 | + char c; |
| 65 | + cin >> c; |
| 66 | + if (c == 'F') { |
| 67 | + int b; |
| 68 | + cin >> b; |
| 69 | + if (b == a) cout << "0\n"; |
| 70 | + else if (b > a) { |
| 71 | + int mx = range_max(segtree[0], 1, b - a, 1, 1, n - a); |
| 72 | + cout << walk(segtree[1], mx, 1, 1, a - 1) + b - a << '\n'; |
| 73 | + } else { |
| 74 | + int mx = range_max(segtree[1], 1, a - b, 1, 1, a - 1); |
| 75 | + cout << walk(segtree[0], mx, 1, 1, n - a) + a - b << '\n'; |
| 76 | + } |
| 77 | + } else { |
| 78 | + int i, e; |
| 79 | + cin >> i >> e; |
| 80 | + |
| 81 | + auto it = find(top_ten.begin(), top_ten.end(), i); |
| 82 | + if (it != top_ten.end()) top_ten.erase(it); |
| 83 | + |
| 84 | + for (int i = 0; i < e - 1; i++) { |
| 85 | + d[top_ten[i]]++; |
| 86 | + if (top_ten[i] > a) |
| 87 | + update(segtree[0], top_ten[i] - a, d[top_ten[i]], 1, 1, n - a); |
| 88 | + else if (top_ten[i] < a) |
| 89 | + update(segtree[1], a - top_ten[i], d[top_ten[i]], 1, 1, a - 1); |
| 90 | + } |
| 91 | + |
| 92 | + d[i] = d[top_ten[e - 1]] + 1; |
| 93 | + if (i > a) update(segtree[0], i - a, d[i], 1, 1, n - a); |
| 94 | + else if (i < a) update(segtree[1], a - i, d[i], 1, 1, a - 1); |
| 95 | + |
| 96 | + top_ten.insert(top_ten.begin() + e - 1, i); |
| 97 | + if (top_ten.size() > 10) top_ten.pop_back(); |
| 98 | + } |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
0 commit comments