이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
int ckadd(int a, int b) {
return a + b >= md ? a + b - md : a + b;
}
int cksub(int a, int b) {
return a >= b ? a - b : a - b + md;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
--a[i]; --b[i];
}
const int A = 26;
vector<vector<int>> dp(n, vector<int>(A));
dp[0] = vector<int>(A, 1);
multiset<int> sa, sb;
vector<vector<vector<int>>> qs(n, vector<vector<int>>(2));
for (int i = 0; i < m; i++) {
if (a[i] == b[i]) {
continue;
}
if (a[i] < b[i]) {
sa.insert(a[i]);
qs[b[i]][0].push_back(a[i]);
}
if (a[i] > b[i]) {
sb.insert(b[i]);
qs[a[i]][1].push_back(b[i]);
}
}
vector<vector<int>> pref(n, vector<int>(A));
for (int i = 0; i < A; i++) {
pref[0][i] = dp[0][i];
}
auto GetSum = [&](int L, int R, int c) {
L = max(0, L);
R = min(R, n - 1);
if (L > R) {
return 0;
}
return cksub(pref[R][c], (L == 0 ? 0 : pref[L - 1][c]));
};
for (int i = 1; i < n; i++) {
int mx_a = -1;
int mx_b = -1;
if (!sa.empty()) {
auto it = sa.lower_bound(i);
if (it != sa.begin()) {
mx_a = *prev(it);
}
}
if (!sb.empty()) {
auto it = sb.lower_bound(i);
if (it != sb.begin()) {
mx_b = *prev(it);
}
}
{
int total = 0;
for (int c = 0; c < A; c++) {
total = ckadd(total, GetSum(max(mx_a, mx_b) + 1, i - 1, c));
}
for (int c = 0; c < A; c++) {
dp[i][c] = ckadd(dp[i][c], cksub(total, GetSum(max(mx_a, mx_b) + 1, i - 1, c)));
}
}
if (mx_a <= mx_b) {
int total = 0;
for (int j = 0; j < A; j++) {
total = ckadd(total, GetSum(mx_a + 1, mx_b, j));
}
for (int j = A - 1; j >= 0; j--) {
total = cksub(total, GetSum(mx_a + 1, mx_b, j));
dp[i][j] = ckadd(dp[i][j], total);
}
} else {
int total = 0;
for (int j = 0; j < A; j++) {
total = ckadd(total, GetSum(mx_b + 1, mx_a, j));
}
for (int j = 0; j < A; j++) {
total = cksub(total, GetSum(mx_b + 1, mx_a, j));
dp[i][j] = ckadd(dp[i][j], total);
}
}
for (int j = 0; j < A; j++) {
pref[i][j] = ckadd(pref[i - 1][j], dp[i][j]);
}
for (int v : qs[i][0]) {
sa.erase(sa.find(v));
}
for (int v : qs[i][1]) {
sb.erase(sb.find(v));
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < A; j++) {
ans = ckadd(ans, dp[i][j]);
}
}
cout << ans << '\n';
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... |