# 雅礼集训 暴力大神hxx

Posted by yjjr's blog on January 1, 2018

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define dep(i,a,b) for(ll i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}

const int maxn=1e5+6,p=12015858;
ll f[27][maxn],n,last[maxn],cnt=0,l[maxn],r[maxn];
ll ans=1;
char ch[10];
bool vis[maxn];
struct edge{ll to,next;}e[maxn<<1];
void insert(ll u,ll v){
e[++cnt]=(edge){v,last[u]};last[u]=cnt;
}

void dfs(int x){
reg(x)dfs(e[i].to);
rep(k,1,maxn-1){
f[x][k]=1;
reg(x){
if(!l[e[i].to]&&k>r[e[i].to]||!r[e[i].to]&&k<l[e[i].to]){f[x][k]=0;break;}//该层循环已经超出范围
if(!r[e[i].to])f[x][k]=1LL*f[x][k]*(f[e[i].to][k]-f[e[i].to][l[e[i].to]-1])%p;
else f[x][k]=1LL*f[x][k]*(f[e[i].to][r[e[i].to]]-f[e[i].to][k-1])%p;
}
f[x][k]=(f[x][k]+p)%p;
}
rep(i,1,maxn-1)f[x][i]=(f[x][i]+f[x][i-1])%p;//前缀和累和
}

int main()
{
rep(i,1,n){
scanf("%s",ch);
if(ch[0]>='a'&&ch[0]<='z')insert(ch[0]-'a'+1,i),vis[i]=1;//存在变量的循环直接将其建边，挂在父亲节点下面
else rep(j,0,strlen(ch)-1)l[i]=l[i]*10+ch[j]-'0';
scanf("%s",ch);
if(ch[0]>='a'&&ch[0]<='z')insert(ch[0]-'a'+1,i),vis[i]=1;
else rep(j,0,strlen(ch)-1)r[i]=r[i]*10+ch[j]-'0';
}
rep(i,1,n)
if(!vis[i]){
dfs(i);
ans=ans*1LL*(f[i][r[i]]-f[i][l[i]-1])%p;//前缀和求答案
}
cout<<(ans+p)%p<<endl;//防止ans为负数
return 0;
}