QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132322 | #242. RMQ Similar Sequence | kuaiqujuan | 100 ✓ | 72ms | 74420kb | C++14 | 1.7kb | 2023-07-29 15:47:25 | 2023-07-29 15:47:26 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i,j,k) for(i=(j);i<=(k);++i)
#define D(i,j,k) for(i=(j);i>=(k);--i)
using namespace std;
#define TIME 1e3*clock()/CLOCKS_PER_SEC
bool OP; double Mbe=TIME;
constexpr int N=2000003,mod=1000000007;
inline int read()
{
int x=0,y=1;char ch=getchar();
for(;ch<48||ch>57;ch=getchar()) if(ch=='-') y=-1;
while(ch>=48&&ch<=57) x=x*10+(ch&15),ch=getchar();
return x*y;
}
typedef long long LL;
inline int qpow(int base,int k=mod-2)
{
int res=1;
while(k)
{
if(k&1) res=(LL)res*base%mod;
base=(LL) base*base%mod;
k>>=1;
}
return res;
}
int n,ans=1,a[N],sz[N],ls[N],rs[N],stk[N],top;
int fac[N],inv[N];
void dfs(int u)
{
sz[u]=1;
if(ls[u]) dfs(ls[u]),sz[u]+=sz[ls[u]];
if(rs[u]) dfs(rs[u]),sz[u]+=sz[rs[u]];
ans=(LL)ans*inv[sz[u]]%mod;
}
int rt;
inline void mian()
{
int i,j,u,v;
n=read();
//大根笛卡尔树
top=0,ans=1;
F(i,1,n) ls[i]=rs[i]=stk[i]=0;
F(i,1,n)
{
a[i]=read();
while(top&&a[stk[top]]<a[i]) ls[i]=stk[top--];
if(top) rs[stk[top]]=i;
else rt=i;
stk[++top]=i;
}
dfs(rt);
printf("%d\n",(LL)ans*inv[2]%mod*n%mod);
}
bool ED;
signed main()
{
//fprintf(stderr,"%.4lf MB\n",(&OP-&ED)/1048576.0);
//#define Marsrayd //Look Out!
#ifdef Marsrayd
assert(freopen("data.in","r",stdin));
assert(freopen("zj.out","w",stdout));
#endif
int i,j;
fac[0]=1;
F(i,1,1000000) fac[i]=(LL)fac[i-1]*i%mod;
j=qpow(fac[1000000]);
D(i,1000000,1) inv[i]=(LL)j*fac[i-1]%mod,j=(LL)j*i%mod;
for(int T=read();T--;mian());
//fprintf(stderr,"time = %.4lf ms\n",TIME-Mbe);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 72ms
memory: 74420kb
input:
11119 1 1 2 1 2 3 1 1 3 4 3 1 1 2 5 5 5 2 4 2 6 2 6 4 4 3 3 7 3 1 1 3 5 5 4 8 4 8 4 7 1 1 7 6 9 9 3 6 3 7 6 1 7 6 10 5 1 10 2 5 1 7 7 4 1 10 4 5 4 5 10 1 5 1 2 5 10 1 2 3 6 2 9 9 7 6 2 10 2 8 3 5 2 4 10 4 8 4 10 2 9 10 3 1 1 2 10 7 7 10 5 7 7 8 10 6 4 3 10 6 10 1 8 9 3 2 4 2 5 5 9 10 4 7 9 2 5 10 10...
output:
500000004 500000004 250000002 83333334 41666667 520833337 760416672 760416672 190104168 1984127 952083340 650694449 253472224 625744052 301388891 625744052 952083340 63368056 387723217 462574408 500992067 835648154 877232149 476041670 31684028 354456021 877232149 916832017 559076007 650694449 126736...
result:
ok 11119 lines