LGP5204题解
@CF1327F
最小值看着有点怪,先转化成最大值吧。。。反正没啥区别。。。
考虑把最大值相同的区间和限制为这个最大值的区间都拿出来。然后离散化。问题变为让所有区间都满足最值为 \(c\)。
考虑 DP。设 \(dp[n][k]\) 表示到序列上的第 \(n\) 个位置后,上一个 \(c\) 在第 \(k\) 个位置。
设 \(L[n]\) 表示右端点为 \(n\) 的区间中,左端点最靠右的那个的左端点。如果没有就为 \(0\)。
转移:
\[dp[n][k]=[L[n]\leq k]dp[n-1][k]\times(c-1) \]\[dp[n][n]=\sum_{i=1}^{n-1}dp[n-1][k] \]可以直接令 \(dp[n]\) 继承 \(dp[n-1]\),然后动态维护这个有值的区间。每次操作的时候只需要支持全局乘和单点加就行了。
可以通过打标记的方法将复杂度降低至期望 \(O(n)\),具体见代码。
#include
#include
#include
#include
typedef unsigned ui;
typedef __uint128_t LL;
typedef unsigned long long ull;
const ui M=1e5+5,mod=1e9+7;
ui n,k,c[M],V[M],iV[M],cl[M];std::vectort[M],Q[M];
struct Hash_Table{
const ui P[20]={
110881,581551,319477,140869,307759,536729,791159,503851,614693,375127,
450299,263429,300761,796303,397373,732731,847009,913687,435401,665201
};
ui mod;ull B;
ui cnt,h[1000000];
struct Node{
ui V,nx;
}t[M];
inline void init(){
ui id=rand()%20;
mod=P[id];B=((LL(1)<<64)+mod-1)/mod;
}
inline ui Find(const ui&x){
for(ui E=h[x-mod*ui(LL(B)*x>>64)];E;E=t[E].nx)if(t[E].V==x)return E;return-1;
}
inline void Insert(const ui&x){
ui&head=h[x-mod*ui(LL(B)*x>>64)];t[++cnt]=(Node){x,head};head=cnt;
}
}Hash;
ui L(1),R(0),q[M];
inline ui max(const ui&a,const ui&b){
return a>b?a:b;
}
inline ui Solve(const ui&x){
static ui L[M],q[M],p[M],dp[M],pre1[M],pre2[M];
const ui&v=V[x]-1,&iv=iV[x],&len=t[x].size();
for(ui id(1),i=0;i>=1,a=1ull*a*a%mod)if(b&1)ans=1ull*ans*a%mod;return ans;
}
inline void swap(ui&a,ui&b){
ui c=a;a=b;b=c;
}
inline void getinv(){
static ui s[M],t[M];const ui&n=Hash.cnt;s[0]=1;
for(ui i=1;i<=n;++i)t[i]=V[i]-1,s[i]=1ull*s[i-1]*t[i]%mod;s[n]=pow(s[n],mod-2);
for(ui i=n;i>=1;--i)swap(s[i],s[i-1]),s[i]=1ull*s[i]*s[i-1]%mod,s[i-1]=1ull*s[i-1]*t[i]%mod;
for(ui i=1;i<=n;++i)iV[i]=s[i];
}
signed main(){
srand(time(NULL));srand(rand()*rand());
ui ans(1);
scanf("%u%u",&n,&k);Hash.init();
for(ui i=1;i<=n-k+1;++i){
scanf("%u",c+i);c[i]=1000000000-c[i]+1;
cl[i]=Hash.Find(c[i]);
if(!~cl[i])Hash.Insert(c[i]),V[Hash.cnt]=c[i],t[Hash.cnt].push_back(0),cl[i]=Hash.cnt;
Q[cl[i]].push_back(i);
}
for(ui i=1;i<=n;++i){
if(L<=R&&q[L]+k<=i)++L;
if(i+k-1<=n){
while(L<=R&&c[q[R]]>=c[i])--R;q[++R]=i;
}
t[cl[q[L]]].push_back(i);
}
getinv();
for(ui i=1;i<=Hash.cnt;++i)ans=1ull*ans*Solve(i)%mod;printf("%u",ans);
}