题解:B landn(text on 2026.10.16)
\(\textsf{B tj}\) landn
题目大意
给定一个长度为 \(n\) 的数组 \(\{a_n\}\),对于一个合法的 \(\{X_n\}\),需满足:
\[\forall i\in [1,n], 1\le X_i\le a_i \\ \operatorname{and} \\ \forall i\in [1,n-1], X_i\neq X_{i+1} \]输出满足条件的 \(\{X_n\}\) 的方案数。
答案对 \(998244353\) 取模。
\(n\le 5\times 10^5, \forall i\in [1,n], 1\le a_i\le 10^9\)。
思路
嗯?好像之前考过类似的题?
使用 线段树优化 \(\texttt{dp}\)。
定义 \(dp_{i,j}\) 表示目前访问到 \(a_i\),出现 \(j\) 的可能性。
那么只要保证不从 \(dp_{i-1,j}\) 转移就行了。(因为需保证 \(X_i\ne X_{i+1}\))
并且如果 \(j > a_i\),是不可能转移的,(因为 \(X_i\le A_i\))所以状态转移方程就易求了:
发现这样做时间是 \(O(nV)\),空间是 \(O(V)\) 的,肯定会炸。(其中 \(V\) 表示 \(a_i\) 的上界,即 \(10^9\))
然后你注意到一个性质:
在转移 \(i-1\to i\) 中 \(\displaystyle\sum_{k=1}^{\infty} dp_{i-1,k}\) 是不变的。
所以对于 \(dp_{i,[1, a_i]}\),对自己取反后加上 \(\sum dp_{i-1}\) 就行了。
然后对于 \(dp_{i,[a_{i+1}, \infty]}\),全部更新为 \(0\) 就行了。
- 区间取反。(乘 \(-1\))
- 区间加。
- 区间修改为 \(0\)。
当当当当当当,线段树闪亮登场。
具体操作
所以我们用 \(add,pev,clear\) 三个懒标记用于更新区间加,乘,修改。
但是需要判断优先级。
在进行区间取反(乘)时,需同步更新 \(add\) 标记。(同步取反)
在进行区间修改为 \(0\) 时,需同时将 \(add,pev\) 重置。
根据上面,我们确定优先级为 \(clear > pev > add\)。
虽然说区间加乘维护区间 \([1,a_i]\),修改维护区间 \([a_i+1,\infty]\),看起来互不影响。
但是 \(a_i\) 是会改变的,有 \(add,pev\) 标记的地方可能也有 \(clear\) 标记。
于是正确懒标记下传 pushdown 为:
void pushdown(...){
if(tr[p].clear){
...
}
if(tr[p].pev){
...
}
if(tr[p].add){
...
}
}
然后因为 \(V\) 很大,可以动态开点,做到 \(O(n\log V)\)。
但是出现了几个问题。
首先,空间很卡。
并且在动态开点线段树内下传懒标记,常数无疑是巨大的。
所以还需要优化。
进一步优化
由于 \(a_i\) 数量不多,所以将 \(a_i\) 排序后 \([a_{i-1}+1, a_i]\) 这个区间是不变的!
所以就用线段树的每个点维护一个区间。
将 \(a\) 离散化即可。
对于每次 \(\texttt{dp}\) 的更新利用 lower_bound 找到 \(a_i\) 对应的区间。
然后你就可以过了!
时间复杂度:\(O(n\log n)\)。
空间复杂度:\(O(n)\)。
放代码:
\(\textsf{code}\)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5+10;
const int trN = 4*N;
const int mod = 998244353;
const int on = 1e9;
int n, a[N];
set<int> lsh;
int Lsh[N], tot;
struct SegmemtTree {
struct tree {
int sum, add, L, R; //lsh[i-1]+1 : lsh[i]
bool pev, clear;
} tr[trN];
int m(int x){
return (x % mod + mod) % mod;
}
int len(int p){
return tr[p].R - tr[p].L + 1;
}
int lp(int x){
return x << 1;
}
int rp(int x){
return x << 1 | 1;
}
void buildup(int p){
tr[p].L = tr[lp(p)].L;
tr[p].R = tr[rp(p)].R;
}
void pushup(int p){
tr[p].sum = m(tr[lp(p)].sum + tr[rp(p)].sum);
}
void pushdown(int p, int l, int mid, int r){
if(tr[p].clear){
tr[lp(p)].sum = 0;
tr[lp(p)].add = 0;
tr[lp(p)].pev = 0;
tr[lp(p)].clear = 1;
tr[rp(p)].sum = 0;
tr[rp(p)].add = 0;
tr[rp(p)].pev = 0;
tr[rp(p)].clear = 1;
tr[p].clear = 0;
}
if(tr[p].pev){
tr[lp(p)].sum = m(-tr[lp(p)].sum);
tr[lp(p)].add = m(-tr[lp(p)].add);
tr[lp(p)].pev ^= 1;
tr[rp(p)].sum = m(-tr[rp(p)].sum);
tr[rp(p)].add = m(-tr[rp(p)].add);
tr[rp(p)].pev ^= 1;
tr[p].pev = 0;
}
if(tr[p].add){
tr[lp(p)].sum += m(tr[p].add * len(lp(p)));
tr[lp(p)].sum = m(tr[lp(p)].sum);
tr[lp(p)].add = m(tr[lp(p)].add + tr[p].add);
tr[rp(p)].sum += m(tr[p].add * len(rp(p)));
tr[rp(p)].sum = m(tr[rp(p)].sum);
tr[rp(p)].add = m(tr[rp(p)].add + tr[p].add);
tr[p].add = 0;
}
}
void build(int p, int l, int r){
if(l == r){
tr[p].L = Lsh[l-1] + 1;
tr[p].R = Lsh[r];
tr[p].sum = (Lsh[l] <= a[1] ? 1 : 0) * len(p);
return;
}
int mid = (l + r) >> 1;
build(lp(p), l, mid);
build(rp(p), mid+1, r);
buildup(p);
pushup(p);
}
int find(){
return tr[1].sum;
}
void unq(int p, int x, int y, int l, int r){
if(x <= l && r <= y){
tr[p].sum = m(-tr[p].sum);
tr[p].add = m(-tr[p].add);
tr[p].pev ^= 1;
return;
}
int mid = (l + r) >> 1;
pushdown(p, l, mid, r);
if(x <= mid) unq(lp(p), x, y, l, mid);
if(y > mid) unq(rp(p), x, y, mid+1, r);
pushup(p);
}
void update(int p, int x, int y, int l, int r, int k){
if(x <= l && r <= y){
tr[p].sum += m(k * len(p));
tr[p].sum = m(tr[p].sum);
tr[p].add = m(tr[p].add + k);
return;
}
int mid = (l + r) >> 1;
pushdown(p, l, mid, r);
if(x <= mid) update(lp(p), x, y, l, mid, k);
if(y > mid) update(rp(p), x, y, mid+1, r, k);
pushup(p);
}
void clean(int p, int x, int y, int l, int r){
if(x > y) return;
if(x <= l && r <= y){
tr[p].sum = 0;
tr[p].add = 0;
tr[p].pev = 0;
tr[p].clear = 1;
return;
}
int mid = (l + r) >> 1;
pushdown(p, l, mid, r);
if(x <= mid) clean(lp(p), x, y, l, mid);
if(y > mid) clean(rp(p), x, y, mid+1, r);
pushup(p);
}
} T;
signed main(){
freopen("landn.in","r",stdin);
freopen("landn.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
lsh.insert(a[i]);
}
lsh.insert(on);
for(auto i : lsh) Lsh[++tot] = i;
T.build(1, 1, tot);
for(int i = 2; i <= n; i++){
int C = lower_bound(Lsh+1, Lsh+tot+1, a[i]) - Lsh;
int x = (T.find() % mod + mod) % mod;
T.unq(1, 1, tot, 1, tot);
T.update(1, 1, C, 1, tot, x);
T.clean(1, C+1, tot, 1, tot);
}
cout << (T.find() % mod + mod) % mod;
return 0;
}
算法:线段树优化 \(\texttt{dp}\) + 离散化。
菜鸭第一次写出了 \(\texttt{dp}\)!真是太好了!

浙公网安备 33010602011771号