题解:P15133 [ROIR 2026] 最后的滑动窗口问题
挑战全谷最长解
事先声明,这个解法是模拟赛时想出的,可能有很多能优化的地方,如有不便,敬请谅解。
首先发现询问长度为
但是这样复杂度是
然后注意到 subtask6 是
但是这个变换还是
发现颜色段相邻的两个颜色段数值恒定,除非这两个颜色段中有长度变为
考虑如何维护,对三种颜色段开三棵线段树,线段树节点记录管辖区间内颜色段的长度和,一次变换后长度和的变化量,颜色段数值和以及一次变换后数值和的变化量,注意,这里数值指颜色段数值乘颜色段长度,以后只指颜色段数值,一个
下面是代码细节。
每次变换就是将三棵线段树的长度变化量加到长度上,数值变化量加到数值上,打 tag 即可。更新一个颜色段就是先更新其长度,然后更新其类型,最后在这个类型对应的线段树上加入这个颜色段,删除一个颜色段只用在对应线段树上删除这个颜色段即可。
但是我们发现一个问题,如果删除一个颜色段导致其左右两侧两个数值相等的颜色段相邻,我们需要合并他们,否则他将不属于三种颜色段的任何一种,所以我们可以维护一个数组,从左到右删除颜色段,发现两个颜色段需要合并就给他们打上相同的值并放进 set 里,最后从左到右合并即可。由于是从左到右删除,所以如果一个颜色段在之前已经被合并,后面的与他合并的颜色段也会打上相同的值,合并之后的颜色段是组成他的颜色段长度之和,然后更新一下类型即可,最后看一下所有更新的颜色段中的
关于复杂度,有不超过
下面给出代码,可以发现代码有 10.43 KB ,可以结合上文阅读,为了方便删除和维护相邻颜色段,使用链表存储,相比于主流写法有差不多 3 倍常数。
::::info[代码]
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, Q, tot;
int ans[400010], id[400010];
bool vis[400010];
struct node {
int x, len, typ;
int pst, nxt;
bool del;
}a[400010];
struct query {
int l, r, id;
};
vector<query> qs[400010];
struct node2 {
int t, id;
bool operator>(const node2 x)const {
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];
void pushup(int p) {
t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum;
t[p].lsum = t[p << 1].lsum + t[p << 1 | 1].lsum;
t[p].num = t[p << 1].num + t[p << 1 | 1].num;
t[p].lnum = t[p << 1].lnum + t[p << 1 | 1].lnum;
}
void pushdown(int p) {
t[p << 1].sum += t[p << 1].num * t[p].tag;
t[p << 1].lsum += t[p << 1].lnum * t[p].tag;
t[p << 1].tag += t[p].tag;
t[p << 1 | 1].sum += t[p << 1 | 1].num * t[p].tag;
t[p << 1 | 1].lsum += t[p << 1 | 1].lnum * t[p].tag;
t[p << 1 | 1].tag += t[p].tag;
t[p].tag = 0;
}
void build(int p, int l, int r, int typ) {
if (l == r) {
if (a[l].typ == typ) {
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;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid, typ);
build(p << 1 | 1, mid + 1, r, typ);
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->x
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];
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;
int pst, len = 1;
cin >> pst;
for (int i = 2; i <= n; i++) {
int now;
cin >> now;
if (now != pst) {
a[++tot].x = pst;
a[tot].len = len;
a[tot - 1].nxt = tot;
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++;
seg[2].t[1].lsum += seg[2].t[1].lnum;
seg[2].t[1].sum += seg[2].t[1].num;
vector<int> del;
while (!mint.empty() && t == mint.top().t) {
del.push_back(mint.top().id);
a[a[mint.top().id].pst].nxt = a[mint.top().id].nxt;
a[a[mint.top().id].nxt].pst = a[mint.top().id].pst;
a[mint.top().id].del = 1;
seg[a[mint.top().id].typ + 1].modify(1, 1, tot, mint.top().id, -1, a[mint.top().id].typ);
mint.pop();
}
sort(del.begin(), del.end());
set<int> del2;
int ntot = 1;
for (int i : del) {
if (a[i].pst && !vis[a[i].pst]) {
vis[a[i].pst] = 1;
if (a[a[i].pst].x == a[a[a[i].pst].pst].x) {
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);
}
if (a[i].nxt && !vis[a[i].nxt]) {
vis[a[i].nxt] = 1;
if (a[a[i].nxt].x == a[a[a[i].nxt].pst].x) {
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);
}
}
if (del2.size()) {
int pst = id[*(del2.begin())];
auto it = del2.begin();
auto it2 = it;
it++;
for (; it != del2.end(); it++) {
if (id[*it] != pst) {
pst = id[*it];
auto now = it;
now--;
a[*now].len = lsum(*it2, *now);
for (; it2 != now; it2++) {
seg[a[*it2].typ + 1].modify(1, 1, tot, *it2, -1, a[*it2].typ);
a[a[*it2].pst].nxt = a[*it2].nxt;
a[a[*it2].nxt].pst = a[*it2].pst;
a[*it2].del = 1;
}
it2++;
seg[a[*now].typ + 1].modify(1, 1, tot, *now, -1, a[*now].typ);
a[*now].typ = chktyp(*now);
seg[a[*now].typ + 1].modify(1, 1, tot, *now, 1, a[*now].typ);
}
}
auto now = it;
now--;
a[*now].len = lsum(*it2, *now);
for (; it2 != now; it2++) {
seg[a[*it2].typ + 1].modify(1, 1, tot, *it2, -1, a[*it2].typ);
a[a[*it2].pst].nxt = a[*it2].nxt;
a[a[*it2].nxt].pst = a[*it2].pst;
}
seg[a[*now].typ + 1].modify(1, 1, tot, *now, -1, a[*now].typ);
a[*now].typ = chktyp(*now);
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 的看不见。