This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define Ls l.top()
#define Rs r.top()
#define calc(x) (fac[mp1[x]+mp2[x]]*inv[mp2[x]]%M*inv[mp1[x]]%M)
const int M=1e9+7,N=1<<18;
int H[N],fac[N],inv[N],res[N],mp1[N],mp2[N];
stack<int>bad[N],l,r;
int qp(int x,int k=M-2){
if(!k) return 1;
int y=qp(x,k/2);
y=y*y%M;
if(k&1)y=y*x%M;
return y;
}
signed main(){
l.push(0),r.push(0);
cin.tie(0)->sync_with_stdio(0);
fac[0]=1;
for(int i=1;i<N;i++)
fac[i]=fac[i-1]*i%M;
inv[N-1]=qp(fac[N-1]);
res[1]=1;
for(int i=N;--i;)
inv[i-1]=inv[i]*i%M;
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>H[i];
for(int i=n;i>1;mp2[H[i]]++,r.push(H[i--]))
while(Rs>H[i])
bad[i].push(Rs),
mp2[Rs]--,r.pop();
for(int i=2;i<=n;i++) {
res[i]=res[i-1];
while(Ls>H[i-1])
res[i]=res[i]*qp(calc(Ls))%M,
mp1[Ls]--,res[i]=res[i]*calc(Ls)%M,l.pop();
res[i]=res[i]*qp(calc(Rs))%M,mp2[Rs]--;
res[i]=res[i]*calc(Rs)%M,r.pop();
l.push(H[i-1]),res[i]=res[i]*qp(calc(Ls))%M;
mp1[Ls]++,res[i]=res[i]*calc(Ls)%M;
while(bad[i].size())
r.push(bad[i].top()),res[i]=res[i]*qp(calc(Rs))%M,
mp2[Rs]++,res[i]=res[i]*calc(Rs)%M,bad[i].pop();
}
while(k--){
int x;
cin>>x;
cout<<res[x]<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |