이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5;
const int MAX_M = 5e5;
const int SIGMA = 26;
const int MOD = 1e9 + 7;
int l[MAX_M], r[MAX_M], t[MAX_M], maxR1[MAX_N + 1], maxR2[MAX_N + 1];
long long dp[MAX_N + 1][SIGMA], sp[MAX_N + 1][SIGMA];
int main() {
int n, m;
cin >> n >> m;
for ( int i = 0; i < m; i++ ) {
cin >> l[i] >> r[i];
if ( l[i] > r[i] ) {
swap( l[i], r[i] );
t[i] = 2;
maxR2[l[i]] = max( maxR2[l[i]], r[i] );
} else {
t[i] = 1;
maxR1[l[i]] = max( maxR1[l[i]], r[i] );
}
}
for ( int l = 0; l < SIGMA; l++ )
dp[1][l] = 1;
vector<int> s1, s2;
for ( int i = 1; i <= n; i++ ) {
while ( !s1.empty() && maxR1[i - 1] >= maxR1[s1.back()] )
s1.pop_back();
s1.push_back( i - 1 );
while ( !s2.empty() && maxR2[i - 1] >= maxR2[s2.back()] )
s2.pop_back();
s2.push_back( i - 1 );
int st, dr;
st = -1, dr = s1.size();
while ( dr - st > 1 ) {
int p = (st + dr) / 2;
if ( maxR1[s1[p]] >= i )
st = p;
else
dr = p;
}
int p1 = (st == -1 ? 0 : s1[st]);
st = -1, dr = s2.size();
while ( dr - st > 1 ) {
int p = (st + dr) / 2;
if ( maxR2[s2[p]] >= i )
st = p;
else
dr = p;
}
int p2 = (st == -1 ? 0 : s2[st]);
for ( int l = 0; l < SIGMA; l++ ) {
for ( int m = 0; m < SIGMA; m++ ) {
if ( m == l )
continue;
dp[i][l] = (dp[i][l] + sp[i - 1][m] - sp[max( p1, p2 )][m] + MOD) % MOD;
}
}
if ( p1 > p2 ) {
for ( int l = 0; l < SIGMA; l++ ) {
for ( int m = l + 1; m < SIGMA; m++ )
dp[i][l] = (dp[i][l] + sp[p1][m] - sp[p2][m] + MOD) % MOD;
}
} else {
for ( int l = 0; l < SIGMA; l++ ) {
for ( int m = 0; m < l; m++ )
dp[i][l] = (dp[i][l] + sp[p2][m] - sp[p1][m] + MOD) % MOD;
}
}
for ( int l = 0; l < SIGMA; l++ )
sp[i][l] = (sp[i - 1][l] + dp[i][l]) % MOD;
}
int ans = 0;
for ( int i = 1; i <= n; i++ ) {
for ( int l = 0; l < SIGMA; l++ )
ans = (ans + dp[i][l]) % 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... |