Submission #374796

#TimeUsernameProblemLanguageResultExecution timeMemory
374796valerikkBubble Sort 2 (JOI18_bubblesort2)C++17
100 / 100
2659 ms138792 KiB
/* doing virtual contest start: 10:00 finish: 14:00 */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; struct segtree { struct node { int mx, add; }; node merge(const node& l, const node& r) { return {max(l.mx, r.mx), 0}; } int n; vector<node> t; void apply(int v, int d) { t[v].mx += d; t[v].add += d; } void push(int v) { apply(2 * v, t[v].add); apply(2 * v + 1, t[v].add); t[v].add = 0; } void upd(int v) { t[v] = merge(t[2 * v], t[2 * v + 1]); } void add_seg(int v, int vl, int vr, int l, int r, int d) { if (vl >= r || vr <= l) return; if (vl >= l && vr <= r) { apply(v, d); return; } push(v); int vm = (vl + vr) / 2; add_seg(2 * v, vl, vm, l, r, d); add_seg(2 * v + 1, vm, vr, l, r, d); upd(v); } void add_seg(int l, int r, int d) { add_seg(1, 0, n, l, r, d); } void change(int v, int vl, int vr, int pos, int val) { if (vr - vl == 1) { t[v] = {val, 0}; } else { push(v); int vm = (vl + vr) / 2; if (pos < vm) change(2 * v, vl, vm, pos, val); else change(2 * v + 1, vm, vr, pos, val); upd(v); } } void change(int pos, int val) { change(1, 0, n, pos, val); } int get_max() { return t[1].mx; } segtree(int n_) : n(n_) { t = vector<node>(4 * n, {0, 0}); } }; struct fenwick { int n; vector<int> f; void add(int pos, int d) { for (int i = pos + 1; i <= n; i += i & -i) f[i] += d; } int sum(int r) { int s = 0; for (int i = r; i > 0; i -= i & -i) s += f[i]; return s; } int sum(int l, int r) { return sum(r) - sum(l); } fenwick(int n_) : n(n_) { f = vector<int>(n + 1, 0); } }; vector<int> countScans(vector<int> a, vector<int> x, vector<int> v) { int n = (int)a.size(), q = (int)x.size(); vector<int> all; for (int i = 0; i < n; ++i) all.push_back(a[i]); for (int i = 0; i < q; ++i) all.push_back(v[i]); sort(all.begin(), all.end()); all.erase(unique(all.begin(), all.end()), all.end()); for (int i = 0; i < n; ++i) a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin(); for (int i = 0; i < q; ++i) v[i] = lower_bound(all.begin(), all.end(), v[i]) - all.begin(); int m = (int)all.size(); vector<set<int>> st(m); segtree t(m); fenwick f(m); for (int i = 0; i < n; ++i) { st[a[i]].insert(i); f.add(a[i], 1); } static const int INF = (int)1e9; auto eval = [&](int ind) { if (st[ind].empty()) return -INF; return *st[ind].rbegin() - f.sum(ind) - (int)st[ind].size() + 1; }; for (int i = 0; i < m; ++i) t.change(i, eval(i)); vector<int> s; for (int i = 0; i < q; ++i) { t.add_seg(a[x[i]] + 1, m, 1); f.add(a[x[i]], -1); st[a[x[i]]].erase(x[i]); st[v[i]].insert(x[i]); f.add(v[i], 1); t.add_seg(v[i] + 1, m, -1); t.change(a[x[i]], eval(a[x[i]])); t.change(v[i], eval(v[i])); a[x[i]] = v[i]; s.push_back(max(0, t.get_max())); } return s; } #ifdef LOCAL int main() { freopen("input.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; vector<int> x(q), v(q); for (int i = 0; i < q; ++i) cin >> x[i] >> v[i]; auto s = countScans(a, x, v); for (int i = 0; i < q; ++i) cout << s[i] << '\n'; return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...