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;
typedef long long ll;
const int mod=1e9+7;
int n,m;
int lastmare[500005],lastmic[500005];
int dp[3][28][500005]; /// 0 -> egal, 1 -> mai mic, 2 -> mai mare
int spref[3][28][500005],ssuf[3][28][500005];
int rmq[2][21][500005]; /// 0 -> lastmic, 1 -> lastmare
int loga[500005];
int getmax(int tip,int l,int r)
{
int lg=loga[r-l+1];
return max(rmq[tip][lg][l],rmq[tip][lg][r-(1<<lg)+1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
if(a<b)
lastmic[a]=max(lastmic[a],b);
if(a>b)
lastmare[b]=max(lastmare[b],a);
}
for(int i=1;i<=n;i++)
{
for(int bit=0;bit<=20;bit++)
if((1<<bit)<=i)
loga[i]=bit;
rmq[0][0][i]=lastmic[i];
rmq[1][0][i]=lastmare[i];
}
for(int tip=0;tip<=1;tip++)
for(int j=1;j<=20;j++)
for(int i=1;i<=n;i++)
{
rmq[tip][j][i]=rmq[tip][j-1][i];
int poz=i+(1<<(j-1));
if(poz<=n)
rmq[tip][j][i]=max(rmq[tip][j][i],rmq[tip][j-1][poz]);
}
for(int i=1;i<=26;i++)
dp[2][i][1]=1;
for(int tip=1;tip<=2;tip++)
{
int suma=0;
for(int c=1;c<=26;c++)
{
suma=(suma+dp[tip][c][1])%mod;
spref[tip][c][1]=(spref[tip][c][0]+suma)%mod;
}
}
for(int tip=1;tip<=2;tip++)
{
int suma=0;
for(int c=26;c>=1;c--)
{
suma=(suma+dp[tip][c][1])%mod;
ssuf[tip][c][1]=(ssuf[tip][c][0]+suma)%mod;
}
}
for(int i=2;i<=n;i++)
{
int pmare=i,pmic=i;
/// mai mare
int st=1;
int dr=i-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(getmax(0,mij,i-1)<i)
{
pmare=mij;
dr=mij-1;
}
else
st=mij+1;
}
/// mai mic
st=1;
dr=i-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(getmax(1,mij,i-1)<i)
{
pmic=mij;
dr=mij-1;
}
else
st=mij+1;
}
for(int c=1;c<=26;c++)
{
dp[0][c][i]=(1LL*dp[0][c][i-1]+1LL*dp[1][c][i-1]+1LL*dp[2][c][i-1])%mod;
int x=(spref[1][c-1][i-1]-spref[1][c-1][pmic-1]+mod)%mod;
int y=(spref[2][c-1][i-1]-spref[2][c-1][pmic-1]+mod)%mod;
dp[1][c][i]=(x+y)%mod;
x=(ssuf[1][c+1][i-1]-ssuf[1][c+1][pmare-1]+mod)%mod;
y=(ssuf[2][c+1][i-1]-ssuf[2][c+1][pmare-1]+mod)%mod;
dp[2][c][i]=(x+y)%mod;
}
for(int tip=1;tip<=2;tip++)
{
int suma=0;
for(int c=1;c<=26;c++)
{
suma=(suma+dp[tip][c][i])%mod;
spref[tip][c][i]=(spref[tip][c][i-1]+suma)%mod;
}
}
for(int tip=1;tip<=2;tip++)
{
int suma=0;
for(int c=26;c>=1;c--)
{
suma=(suma+dp[tip][c][i])%mod;
ssuf[tip][c][i]=(ssuf[tip][c][i-1]+suma)%mod;
}
}
}
int ans=0;
for(int c=1;c<=26;c++)
{
ans=(ans+dp[0][c][n])%mod;
ans=(ans+dp[1][c][n])%mod;
ans=(ans+dp[2][c][n])%mod;
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |