递归爆栈,改成非递归的模板题
#include <bits/stdc++.h>
using namespace std;
typedef int lint;
typedef long long LL;
typedef pair<lint,lint> pii;
const lint maxn = 1000005;
lint l[maxn],r[maxn];
const LL mod = 1e9 + 7;
LL frac[maxn], carf[maxn];
LL inv(LL x){
if (x==1) return 1;
else return (mod-mod/x)*inv(mod%x)%mod;
}
void init(){
frac[0]=1;
for (int i=1;i<=1000003;i++)
frac[i]=frac[i-1]*i%mod;
carf[1000003]=inv(frac[1000003]);
for (int i=1000003;i>=1;i--)
carf[i-1]=carf[i]*i%mod;
}
LL C(int n, int m){
if (m>n) return 0;
else return frac[n]*carf[n-m]%mod*carf[m]%mod;
}
map<pii,lint> ma;
struct type{
lint l,r,cnt;
bool f;
lint flag;
type( lint ccnt = 0,lint ll=0,lint rr = 0,bool ff = false,lint fff = 0 ){
l = ll;r=rr;f =ff;
flag = fff;
cnt = ccnt;
}
};
type st[maxn];
lint top;
LL dfs( lint cnt,lint l,lint r,bool f ){
/*if( l > r ) return 1LL;
LL ans = 1;
if(f)ans = ans * C( cnt,r-l+1 )%mod;
auto p = ma.find(pii(l,r));
lint pos = p->second;
ma.erase(p);
ans = ans * dfs( r-l,pos+1,r,true ) % mod;
ans = ans*dfs( r-l,l,pos-1,false ) %mod;
return ans;*/
LL ans = 1;
st[++top] = type( cnt,l,r,f,0 );
while(top>0){
type cur = st[top];
if(cur.f&&!cur.flag)ans = ans * C( cur.cnt,cur.r-cur.l+1 )%mod;
auto p = ma.find(pii(cur.l,cur.r));
if( p == ma.end() ) return 0;
lint pos = p->second;
//ma.erase(p);
if( !cur.flag ){
st[top].flag++;
if( pos + 1<= cur.r ){
st[++top] = type( cur.r-cur.l,pos+1,cur.r,true );
}
}else if( cur.flag == 1 ){
st[top].flag++;
if( cur.l <= pos-1 ){
st[++top] = type( cur.r-cur.l,cur.l,pos-1,false );
}
}else{
top--;
}
}
return ans;
}
int main(){
lint n;
init();
lint ca = 0;
while( 1 == scanf("%d",&n) ){
printf("Case #%d: ",++ca);
ma.clear();
top = 0;
for( lint i = 1;i <= n;i++ ) scanf("%d",&l[i]);
for( lint i = 1;i <= n;i++ ) scanf("%d",&r[i]);
for( lint i = 1;i <= n;i++ ) ma[pii(l[i],r[i])] = i;
LL ans = dfs( n,1,n,true );
printf("%I64d\n",ans);
}
return 0;
}
[close]