版本比较
[ARC210] 做题记录
正文
增加 21 行删除 21 行
13 行未改动141516171819202122232425262728293031323334353637383912 行未改动5253545556575859606162636465666768697071727374757677787980818283848586878889909192939495968 行未改动10510610710810911011111211316 行未改动13 行未改动时间复杂度 $O(n\log n)$。```cppconst int maxn = 2e5 + 5;int n, q; ll a[maxn], ans;ll v[maxn];inline void add(int x, int d) { for (; x <= n; x+= x & -x) v[x]+= d; }inline ll qry(int x) { ll res = 0; for (; x; x-= x & -x) res+= v[x]; return res; }int main(){read(n, q); rep(i, 1, n) a[i] = i, add(i, 1), ans+= i;rep(i, 1, q){int x, y; read(x, y); add(x, y); add(x + 1, -y);if (x == n || qry(x) < qry(x + 1)) continue;ll tmp = qry(x) + 1 - qry(x + 1); ans+= tmp * (n - x); add(x + 1, tmp);}write(ans); pc(0xa);return 0;}```**Update:**12 行未改动细节见代码。```cppconst int maxn = 2e5 + 5;int n, m, q, a[maxn], b[maxn], c[maxn << 1], k; ll ans;multiset<int> st1, st2, st3;inline void change(int x, int y){if (st1.find(x) != st1.end()) st1.erase(st1.find(x)), ans-= x;else if (st2.find(x) != st2.end()) st2.erase(st2.find(x));else st3.erase(st3.find(x)), ans-= x;if (st2.size() && y <= *st2.begin()) st1.insert(y), ans+= y;else if (st2.size() && y >= *prev(st2.end())) st3.insert(y), ans+= y;else st2.insert(y);while (st1.size() < n / 2) { auto tmp = st2.begin(); ans+= *tmp; st1.insert(*tmp); st2.erase(tmp); }while (st1.size() > n / 2) { auto tmp = prev(st1.end()); ans-= *tmp; st2.insert(*tmp); st1.erase(tmp); }while (st3.size() < n / 2) { auto tmp = prev(st2.end()); ans+= *tmp; st3.insert(*tmp); st2.erase(tmp); }while (st3.size() > n / 2) { auto tmp = st3.begin(); ans-= *tmp; st2.insert(*tmp); st3.erase(tmp); }}int main(){read(n, m, q);rep(i, 1, n) read(a[i]), c[++k] = a[i];rep(i, 1, m) read(b[i]), c[++k] = b[i];sort(c + 1, c + k + 1);rep(i, 1, n / 2) st1.insert(c[i]), ans+= c[i];rep(i, n / 2 + 1, n / 2 + m) st2.insert(c[i]);rep(i, n / 2 + m + 1, n + m) st3.insert(c[i]), ans+= c[i];rep(i, 1, q){int t, x, y; read(t, x, y);if (t == 1) { change(a[x], y); a[x] = y; }else { change(b[x], y); b[x] = y; }write(ans); pc(0xa);}return 0;}```### [ARC210C Fair Coin Partition](https://atcoder.jp/contests/arc210/tasks/arc210_c)8 行未改动于是就容易做了。细节见代码。```cppconst int maxn = 2e5 + 105;int n, m, N; ll a[maxn], abl[maxn], ans[maxn], cst;inline void work(){16 行未改动13 行未改动141516171819202122232425262728293031323334353637383912 行未改动5253545556575859606162636465666768697071727374757677787980818283848586878889909192939495968 行未改动10510610710810911011111211316 行未改动13 行未改动时间复杂度 $O(n\log n)$。```cppconst int maxn = 2e5 + 5;int n, q; ll a[maxn], ans;ll v[maxn];inline void add(int x, int d) { for (; x <= n; x+= x & -x) v[x]+= d; }inline ll qry(int x) { ll res = 0; for (; x; x-= x & -x) res+= v[x]; return res; }int main(){read(n, q); rep(i, 1, n) a[i] = i, add(i, 1), ans+= i;rep(i, 1, q){int x, y; read(x, y); add(x, y); add(x + 1, -y);if (x == n || qry(x) < qry(x + 1)) continue;ll tmp = qry(x) + 1 - qry(x + 1); ans+= tmp * (n - x); add(x + 1, tmp);}write(ans); pc(0xa);return 0;}```**Update:**12 行未改动细节见代码。```cppconst int maxn = 2e5 + 5;int n, m, q, a[maxn], b[maxn], c[maxn << 1], k; ll ans;multiset<int> st1, st2, st3;inline void change(int x, int y){if (st1.find(x) != st1.end()) st1.erase(st1.find(x)), ans-= x;else if (st2.find(x) != st2.end()) st2.erase(st2.find(x));else st3.erase(st3.find(x)), ans-= x;if (st2.size() && y <= *st2.begin()) st1.insert(y), ans+= y;else if (st2.size() && y >= *prev(st2.end())) st3.insert(y), ans+= y;else st2.insert(y);while (st1.size() < n / 2) { auto tmp = st2.begin(); ans+= *tmp; st1.insert(*tmp); st2.erase(tmp); }while (st1.size() > n / 2) { auto tmp = prev(st1.end()); ans-= *tmp; st2.insert(*tmp); st1.erase(tmp); }while (st3.size() < n / 2) { auto tmp = prev(st2.end()); ans+= *tmp; st3.insert(*tmp); st2.erase(tmp); }while (st3.size() > n / 2) { auto tmp = st3.begin(); ans-= *tmp; st2.insert(*tmp); st3.erase(tmp); }}int main(){read(n, m, q);rep(i, 1, n) read(a[i]), c[++k] = a[i];rep(i, 1, m) read(b[i]), c[++k] = b[i];sort(c + 1, c + k + 1);rep(i, 1, n / 2) st1.insert(c[i]), ans+= c[i];rep(i, n / 2 + 1, n / 2 + m) st2.insert(c[i]);rep(i, n / 2 + m + 1, n + m) st3.insert(c[i]), ans+= c[i];rep(i, 1, q){int t, x, y; read(t, x, y);if (t == 1) { change(a[x], y); a[x] = y; }else { change(b[x], y); b[x] = y; }write(ans); pc(0xa);}return 0;}```### [ARC210C Fair Coin Partition](https://atcoder.jp/contests/arc210/tasks/arc210_c)8 行未改动于是就容易做了。细节见代码。```cppconst int maxn = 2e5 + 105;int n, m, N; ll a[maxn], abl[maxn], ans[maxn], cst;inline void work(){16 行未改动