参考博客:
#includeusing namespace std;typedef long long LL;//=========================================================namespace IO { const int MX = 4e7; //1e7占用内存11000kb char buf[MX]; int c, sz; void begin() { c = 0; sz = fread(buf, 1, MX, stdin); } inline bool read(int &t) { while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++; if(c >= sz) return false; bool flag = 0; if(buf[c] == '-') flag = 1, c++; for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0'; if(flag) t = -t; return true; } }//=========================================================const LL mod=1e9+7;const LL M=1e6+5;LL fac[M+5]; //阶乘 LL inv_of_fac[M+5]; //阶乘的逆元 LL qpow(LL x,LL n){ LL ret=1; for(; n; n>>=1) { if(n&1) ret=ret*x%mod; x=x*x%mod; } return ret;}void init(){ fac[0]=1; for(int i=1; i<=M; i++) fac[i]=fac[i-1]*i%mod; inv_of_fac[M]=qpow(fac[M],mod-2); for(int i=M-1; i>=0; i--) inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod;}LL C(LL a,LL b){ if(b<0||a rhs.r; }}a[M];int mark,rear,n;LL dfs(int l,int r){ if(mark) return 0; if(l>r) return 1; if(a[rear].l!=l||a[rear].r!=r) { mark=1; return 0; } node cur=a[rear++]; LL ret=C(cur.r-cur.l,cur.id-cur.l); ret=ret*dfs(cur.l,cur.id-1)%mod; ret=ret*dfs(cur.id+1,cur.r)%mod; return ret; }int main(){ init(); int kase=0; IO::begin(); while(IO::read(n)) { for(int i=1;i<=n;i++) IO::read(a[i].l); for(int i=1;i<=n;i++) IO::read(a[i].r),a[i].id=i; sort(a+1,a+1+n); mark=0,rear=1; printf("Case #%d: %lld\n", ++kase, dfs(1, n)); }}