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 MX=5e5+5, mod=1e9+7;
int N,M;
vector<pair<int,int>> open[MX], close[MX];
ll dp[MX][26], sdp[MX][26];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>M;
for(int i=0;i<M;i++) {
int a,b;
cin>>a>>b;
if(a<b) {
open[a].push_back({b,1}); // s_{i-1} > s{i}
close[b].push_back({a,1});
} else {
// a > b
open[b].push_back({a,0}); // s_{i-1} < s{i}
close[a].push_back({b,0});
}
}
for(int c=0;c<26;c++) dp[1][c]=1, sdp[1][c]=1;
multiset<int> st[2];
st[0].insert(0);
st[1].insert(0);
for(auto [j,t]:open[1]) st[t].insert(1);
for(int i=2;i<=N;i++) {
int p=*st[0].rbegin();
int q=*st[1].rbegin();
// [max(p,q)+1, i] any value bcs alrdy solved
// [min(p,q)+1, max(p,q)] put appropriate value
if(p<=q) {
ll cur=0;
for(int c=0;c<26;c++) {
dp[i][c]+=cur;
dp[i][c]%=mod;
cur+=sdp[max(p,q)][c];
cur-=sdp[min(p,q)][c];
cur+=mod;
cur%=mod;
}
}
if(p>=q) {
ll cur=0;
for(int c=25;c>=0;c--) {
dp[i][c]+=cur;
dp[i][c]%=mod;
cur+=sdp[max(p,q)][c];
cur-=sdp[min(p,q)][c];
cur+=mod;
cur%=mod;
}
}
ll cur=0;
for(int c=0;c<26;c++) {
dp[i][c]+=cur;
dp[i][c]%=mod;
cur+=sdp[i-1][c];
cur-=sdp[max(p,q)][c];
cur+=mod;
cur%=mod;
}
cur=0;
for(int c=25;c>=0;c--) {
dp[i][c]+=cur;
dp[i][c]%=mod;
cur+=sdp[i-1][c];
cur-=sdp[max(p,q)][c];
cur+=mod;
cur%=mod;
}
for(int c=0;c<26;c++) {
sdp[i][c]=sdp[i-1][c]+dp[i][c];
sdp[i][c]%=mod;
}
for(auto [j,t]:open[i]) st[t].insert(i);
for(auto [j,t]:close[i]) {
st[t].erase(st[t].find(j));
}
}
ll ans=0;
for(int c=0;c<26;c++) {
ans+=sdp[N][c];
ans%=mod;
}
cout<<ans<<'\n';
}
# | 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... |