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;
int n,m;
int mxst1[500005], mxst2[500005];
int lift1[500005][21], lift2[500005][21];
int a[500005], b[500005];
long long dp[500005][28][2];
long long psum[500005][28][2], psum2[500005][28][2];
long long ans = 0;
long long mod = 1e9+7;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> a[i] >> b[i];
if(a[i] > b[i]) {
mxst1[b[i]] = max(mxst1[b[i]], a[i]);
} else {
mxst2[a[i]] = max(mxst2[a[i]], b[i]);
}
}
for(int i=0; i<=20; i++) {
for(int j=1; j<=n; j++) {
if(i==0) {
lift1[j][i] = mxst1[j];
lift2[j][i] = mxst2[j];
} else {
if(j-(1<<(i)) < 0) lift1[j][i] = lift2[j][i] = -1;
else {
lift1[j][i] = max(lift1[j][i-1], lift1[j-(1<<(i-1))][i-1]);
lift2[j][i] = max(lift2[j][i-1], lift2[j-(1<<(i-1))][i-1]);
}
}
}
}
for(int i=2; i<=n; i++) {
for(int j=1; j<=26; j++) {
// case 1: < last
if(j<26) {
int tmp = i-1;
for(int k=20; k>=0; k--) {
if(lift1[tmp][k]==-1) continue;
if(lift1[tmp][k] < i) {
tmp -= (1<<k);
}
}
if(tmp > 0) {
// have restrictions
dp[i][j][0] += psum2[i-1][j+1][1] - psum2[tmp][j+1][1] + psum2[i-1][j+1][0] - psum2[tmp][j+1][0];
} else {
// no restrictions
dp[i][j][0] += psum2[i-1][j+1][0] + psum2[i-1][j+1][1] + 26-j;
}
dp[i][j][0] %= mod;
}
// case 2: > last
if(j>1) {
int tmp = i-1;
for(int k=20; k>=0; k--) {
if(lift2[tmp][k]==-1) continue;
if(lift2[tmp][k] < i) {
tmp -= (1<<k);
}
}
if(tmp > 0) {
dp[i][j][1] += psum[i-1][j-1][0] - psum[tmp][j-1][0] + psum[i-1][j-1][1] - psum[tmp][j-1][1];
} else {
dp[i][j][1] += psum[i-1][j-1][0] + psum[i-1][j-1][1] + j-1;
}
dp[i][j][1] %= mod;
}
}
for(int j=1; j<=26; j++) {
psum[i][j][0] = psum[i][j-1][0] + dp[i][j][0];
psum[i][j][1] = psum[i][j-1][1] + dp[i][j][1];
}
for(int j=26; j>=1; j--) {
psum2[i][j][0] = psum2[i][j+1][0] + dp[i][j][0];
psum2[i][j][1] = psum2[i][j+1][1] + dp[i][j][1];
}
for(int j=1; j<=26; j++) {
psum[i][j][0] += psum[i-1][j][0];
psum[i][j][1] += psum[i-1][j][1];
psum2[i][j][0] += psum2[i-1][j][0];
psum2[i][j][1] += psum2[i-1][j][1];
psum[i][j][0] %= mod; psum[i][j][1] %= mod;
psum2[i][j][0] %= mod; psum2[i][j][1] %= mod;
}
for(int j=1; j<=26; j++) {
ans += (dp[i][j][0] + dp[i][j][1]);
ans %= mod;
}
}
ans = (ans+26)%mod;
if(ans<0) 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... |