正文
增加 51 行删除 131 行
27 行未改动28293031323334353611 行未改动48495051525354555657585960616263646566676828 行未改动9798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981999 行未改动20921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325418 行未改动2732742752762772782792802812822832842852862872883 行未改动29229329429529629729829930030130230330430530630734 行未改动34234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738827 行未改动```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;}}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;}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);}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);}pushup(p);}int query(int p, int l, int r, int ql, int qr, int typ) {if (ql <= l && r <= qr) {if (typ) {return t[p].sum;}else {return t[p].lsum;}}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);}}}seg[3];//0->len 1->xint 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);}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);}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);}}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);}}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);}}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++;}}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);}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++;qs[k].push_back(now);}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;}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];}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);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];}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);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);}}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);}}for (int i : change) {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';}return 0;}```::::题外话:翻阅提交记录发现好像至少是次长解,有一个接近 13 KB 的是输入输出优化和 modint,有一个 19 KB 的看不见。27 行未改动28293031323334353611 行未改动48495051525354555657585960616228 行未改动9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501519 行未改动16116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419518 行未改动2142152162172182192202212222232242252263 行未改动23023123223323423523623723823924024124234 行未改动27727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730827 行未改动```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;}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;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);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);pushup(p);}int query(int p, int l, int r, int ql, int qr, int typ) {//此处typ不是指该树对应的颜色段类型而是本次询问类型0->len 1->xif (ql <= l && r <= qr) {if (typ) return t[p].sum;else return t[p].lsum;}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);}}seg[3];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);}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);}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);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);}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);}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++;}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);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 - 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 });int QQ = Q;for (query q : qs[1]) {solve(q);Q--;}for (int t = 2; t <= n; t++) {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];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);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];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);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);}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);}for (int i : change) {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';return 0;}```::::题外话:翻阅提交记录发现好像至少是次长解,有一个接近 13 KB 的是输入输出优化和 modint,有一个 19 KB 的看不见。