이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 500005
#define A 30
#define int long long
const int modulo = 1e9 + 7;
int range[2][N];
int dp[N][A], v[2][N];
int add(int a, int b)
{
if (a + b < modulo) return a + b;
return a + b - modulo;
}
int mul(int a, int b)
{
return (a * b) % modulo;
}
int subs(int a, int b)
{
if (a < b) return a - b + modulo;
return a - b;
}
int32_t main()
{
fastio();
int n, m;
cin>>n>>m;
for (int i = 1; i <= m; i++)
{
int a, b;
cin>>a>>b;
if (a > b) range[1][b + 1] = max(range[1][b + 1], a);
else range[0][a + 1] = max(range[0][a + 1], b);
}
set<int> undone[2];
vector<int> pre(A, 0), suf(A, 0);
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= 26; j++)
{
dp[i][j] = add(pre[j - 1], suf[j + 1]);
dp[i][j] = add(dp[i][j], 1);
}
for (int j = 0; j < 2; j++)
{
undone[j].insert(i);
for (int k = 1; k <= 26; k++)
v[j][k] = add(v[j][k], dp[i][k]);
auto it = undone[j].begin();
while(it != undone[j].end() && *it <= range[j ^ 1][i])
{
for (int k = 1; k <= 26; k++)
v[j][k] = subs(v[j][k], dp[*it][k]);
it = undone[j].erase(it);
}
}
for (int j = 1; j <= 26; j++)
pre[j] = add(pre[j - 1], v[1][j]);
for (int j = 26; j >= 1; j--)
suf[j] = add(suf[j + 1], v[0][j]);
}
int ans = 0;
for (int i = 0; i < 2; i++)
{
for (auto j : undone[i])
{
if (j == 1) continue;
for (int k = 1; k <= 26; k++)
{
if (i == 0) ans = add(ans, mul(k - 1, dp[j][k]));
else ans = add(ans, mul(26 - k, dp[j][k]));
}
}
}
ans = add(ans, 26);
assert((ans % modulo + modulo) % modulo == ans);
cout<<ans<<endl;
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\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... |