版本比较
题解:P15133 [ROIR 2026] 最后的滑动窗口问题
正文
增加 51 行删除 131 行
27 行未改动2829303132323334353611 行未改动484950515252535455565758596061625455565764585960616228 行未改动9192939410110295103969798991001011021031121131141041051161171181191201211061071081091241101111261271281291301311121131141331341351151161371381391401411421431441451461171181191201211491501221231521531241251551261271571581281291601301311621631641651661671681691321331341351721731741751761771781791801361371381391821401411421851861871881891901911921931431441451461951471481491501519 行未改动16116216316421321421516516616716816917022122222317117217317417517617717817918023323418118218323723823924024118418518618718818919024824925019119219319419518 行未改动2142152162172772782182792192202212832842222232242252263 行未改动23023123223329629723429823523623730230323823924024124234 行未改动27727827928034634734834935035135235335428128228328428535735835936036136236336436528628728828929036836937029129229329429529629729829937938038138230030130230330430530630730827 行未改动```cpp#include<bits/stdc++.h>#define int long longusing namespace std;
int n, Q, tot;int ans[400010], id[400010];bool vis[400010];struct node {11 行未改动 return t > x.t; }};priority_queue<node2, vector<node2>, greater<node2>> mint;
int chktyp(int p) { if (a[p].x > a[a[p].pst].x && a[p].x > a[a[p].nxt].x) { return -1; } else if (a[p].x < a[a[p].pst].x && a[p].x < a[a[p].nxt].x) { return 1; } else { return 0; } if (a[p].x > a[a[p].pst].x && a[p].x > a[a[p].nxt].x) return -1; else if (a[p].x < a[a[p].pst].x && a[p].x < a[a[p].nxt].x) return 1; else return 0;}
struct Segtree { struct node { int sum, num, lnum, lsum, tag; }t[1600010];28 行未改动 pushup(p); } void modify(int p, int l, int r, int q, int x, int typ) { if (l == r) { if (x == -1) { t[p].num = t[p].sum = t[p].lnum = t[p].lsum = 0; if (x == -1) t[p].num = t[p].sum = t[p].lnum = t[p].lsum = 0; } else { t[p].sum = a[l].x * a[l].len; t[p].num = a[l].x * typ; t[p].lsum = a[l].len; t[p].lnum = typ; } return; } if (t[p].tag) { pushdown(p); } if (t[p].tag) pushdown(p); int mid = (l + r) >> 1; if (q <= mid) { modify(p << 1, l, mid, q, x, typ); } else { modify(p << 1 | 1, mid + 1, r, q, x, typ); } if (q <= mid) modify(p << 1, l, mid, q, x, typ); else modify(p << 1 | 1, mid + 1, r, q, x, typ); pushup(p); } int query(int p, int l, int r, int ql, int qr, int typ) { int query(int p, int l, int r, int ql, int qr, int typ) {//此处typ不是指该树对应的颜色段类型而是本次询问类型0->len 1->x if (ql <= l && r <= qr) { if (typ) { return t[p].sum; } else { return t[p].lsum; } if (typ) return t[p].sum; else return t[p].lsum; } if (t[p].tag) { pushdown(p); } if (t[p].tag) pushdown(p); int mid = (l + r) >> 1; if (qr <= mid) { return query(p << 1, l, mid, ql, qr, typ); } else if (mid < ql) { return query(p << 1 | 1, mid + 1, r, ql, qr, typ); } else { return query(p << 1, l, mid, ql, qr, typ) + query(p << 1 | 1, mid + 1, r, ql, qr, typ); } if (qr <= mid) return query(p << 1, l, mid, ql, qr, typ); else if (mid < ql) return query(p << 1 | 1, mid + 1, r, ql, qr, typ); else return query(p << 1, l, mid, ql, qr, typ) + query(p << 1 | 1, mid + 1, r, ql, qr, typ); }}seg[3];//0->len 1->x
int sum(int l, int r) { return seg[0].query(1, 1, tot, l, r, 1) + seg[1].query(1, 1, tot, l, r, 1) + seg[2].query(1, 1, tot, l, r, 1); return seg[0].query(1, 1, tot, l, r, 1) + seg[1].query(1, 1, tot, l, r, 1) + seg[2].query(1, 1, tot, l, r, 1);}
int lsum(int l, int r) { return seg[0].query(1, 1, tot, l, r, 0) + seg[1].query(1, 1, tot, l, r, 0) + seg[2].query(1, 1, tot, l, r, 0); return seg[0].query(1, 1, tot, l, r, 0) + seg[1].query(1, 1, tot, l, r, 0) + seg[2].query(1, 1, tot, l, r, 0);}
int fnd(int p, int l, int r, int q) { if (l == r) { return l; } for (int i = 0; i < 3; i++) { if (seg[i].t[p].tag) { seg[i].pushdown(p); } } if (l == r) return l; for (int i = 0; i < 3; i++) if (seg[i].t[p].tag) seg[i].pushdown(p); int mid = (l + r) >> 1; int lf = 0; for (int i = 0; i < 3; i++) { lf += seg[i].t[p << 1].lsum; } if (lf >= q) { return fnd(p << 1, l, mid, q); } else { return fnd(p << 1 | 1, mid + 1, r, q - lf); } for (int i = 0; i < 3; i++) lf += seg[i].t[p << 1].lsum; if (lf >= q) return fnd(p << 1, l, mid, q); else return fnd(p << 1 | 1, mid + 1, r, q - lf);}
void solve(query q) { int l = fnd(1, 1, tot, q.l), r = fnd(1, 1, tot, q.r); if (l == r) { ans[q.id] = a[l].x * (q.r - q.l + 1); } else if (r == l + 1) { ans[q.id] = a[l].x * (lsum(1, l) - q.l + 1) + a[r].x * (q.r - lsum(1, l)); } else { ans[q.id] = a[l].x * (lsum(1, l) - q.l + 1) + a[r].x * (q.r - lsum(1, r - 1)) + sum(l + 1, r - 1); } if (l == r) ans[q.id] = a[l].x * (q.r - q.l + 1); else if (r == l + 1) ans[q.id] = a[l].x * (lsum(1, l) - q.l + 1) + a[r].x * (q.r - lsum(1, l)); else ans[q.id] = a[l].x * (lsum(1, l) - q.l + 1) + a[r].x * (q.r - lsum(1, r - 1)) + sum(l + 1, r - 1);}
signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> Q;9 行未改动 a[tot].pst = tot - 1; pst = now; len = 1; } else { len++; } else len++; } a[++tot].x = pst; a[tot].len = len; a[tot - 1].nxt = tot; a[tot].pst = tot - 1; for (int i = 1; i <= tot; i++) { a[i].typ = chktyp(i); } for (int i = 1; i <= tot; i++) a[i].typ = chktyp(i); seg[0].build(1, 1, tot, -1); seg[1].build(1, 1, tot, 0); seg[2].build(1, 1, tot, 1); for (int i = 1; i <= Q; i++) { query now; cin >> now.l >> now.r; now.id = i; int k; cin >> k; now.r -= k; now.r++; now.r -= (k - 1); qs[k].push_back(now); } for (int i = 1; i <= tot; i++) { if (a[i].typ == -1) { mint.push({ a[i].len + 1,i }); } } for (int i = 1; i <= tot; i++) if (a[i].typ == -1) mint.push({ a[i].len + 1,i }); int QQ = Q; for (query q : qs[1]) { solve(q); Q--; } for (int t = 2; t <= n; t++) { if (!Q) { break; } if (!Q) break; seg[0].t[1].tag++; seg[0].t[1].lsum += seg[0].t[1].lnum; seg[0].t[1].sum += seg[0].t[1].num; seg[2].t[1].tag++;18 行未改动 if (id[a[a[i].pst].pst] != ntot) { id[a[a[i].pst].pst] = ++ntot; id[a[i].pst] = ntot; } else { id[a[i].pst] = id[a[a[i].pst].pst]; else id[a[i].pst] = id[a[a[i].pst].pst]; } del2.insert(a[i].pst); del2.insert(a[a[i].pst].pst); } a[a[i].pst].len = seg[a[a[i].pst].typ + 1].query(1, 1, tot, a[i].pst, a[i].pst, 0); a[a[i].pst].len = seg[a[a[i].pst].typ + 1].query(1, 1, tot, a[i].pst, a[i].pst, 0); seg[a[a[i].pst].typ + 1].modify(1, 1, tot, a[i].pst, -1, a[a[i].pst].typ); a[a[i].pst].typ = chktyp(a[i].pst); seg[a[a[i].pst].typ + 1].modify(1, 1, tot, a[i].pst, 1, a[a[i].pst].typ); }3 行未改动 if (id[a[a[i].nxt].pst] != ntot) { id[a[a[i].nxt].pst] = ++ntot; id[a[i].nxt] = ntot; } else { id[a[i].nxt] = id[a[a[i].nxt].pst]; else id[a[i].nxt] = id[a[a[i].nxt].pst]; } del2.insert(a[i].nxt); del2.insert(a[a[i].nxt].pst); } a[a[i].nxt].len = seg[a[a[i].nxt].typ + 1].query(1, 1, tot, a[i].nxt, a[i].nxt, 0); a[a[i].nxt].len = seg[a[a[i].nxt].typ + 1].query(1, 1, tot, a[i].nxt, a[i].nxt, 0); seg[a[a[i].nxt].typ + 1].modify(1, 1, tot, a[i].nxt, -1, a[a[i].nxt].typ); a[a[i].nxt].typ = chktyp(a[i].nxt); seg[a[a[i].nxt].typ + 1].modify(1, 1, tot, a[i].nxt, 1, a[a[i].nxt].typ); }34 行未改动 seg[a[*now].typ + 1].modify(1, 1, tot, *now, 1, a[*now].typ); } set<int> change; for (int i : del) { if (!a[i].del) { change.insert(i); } if (a[i].pst && !a[a[i].pst].del) { change.insert(a[i].pst); } if (a[i].nxt && !a[a[i].nxt].del) { change.insert(a[i].nxt); } if (!a[i].del) change.insert(i); if (a[i].pst && !a[a[i].pst].del) change.insert(a[i].pst); if (a[i].nxt && !a[a[i].nxt].del) change.insert(a[i].nxt); } for (int i : del) { if (!a[i].del) { change.insert(i); } if (a[i].pst && !a[a[i].pst].del) { change.insert(a[i].pst); } if (a[i].nxt && !a[a[i].nxt].del) { change.insert(a[i].nxt); } if (!a[i].del) change.insert(i); if (a[i].pst && !a[a[i].pst].del) change.insert(a[i].pst); if (a[i].nxt && !a[a[i].nxt].del) change.insert(a[i].nxt); } for (int i : change) { if (a[i].typ == -1) { mint.push({ t + a[i].len,i }); } if (a[i].typ == -1) mint.push({ t + a[i].len,i }); id[i] = 0; vis[i] = 0; } for (query q : qs[t]) { solve(q); Q--; } } for (int i = 1; i <= QQ; i++) { cout << ans[i] << '\n'; } for (int i = 1; i <= QQ; i++) cout << ans[i] << '\n';
return 0;}
```::::
题外话:翻阅提交记录发现好像至少是次长解,有一个接近 13 KB 的是输入输出优化和 modint,有一个 19 KB 的看不见。