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;
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 j = max(mx_a, mx_b) + 1; j < i; j++) {
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) {
for (int j = mx_a + 1; j <= mx_b; j++) {
for (int c = 0; c < A; c++) {
for (int cc = c + 1; cc < A; cc++) {
dp[i][cc] = ckadd(dp[i][cc], dp[j][c]);
}
}
}
} else {
for (int j = mx_b + 1; j <= mx_a; j++) {
for (int c = 0; c < A; c++) {
for (int cc = 0; cc < c; cc++) {
dp[i][cc] = ckadd(dp[i][cc], dp[j][c]);
}
}
}
}
/* for (int c = 0; c < A; c++) {
for (int j = min(mx_a, mx_b) + 1; j < i; j++) {
for (int cc = 0; cc < A; cc++) {
if (c == cc) {
continue;
}
bool ok = true;
if (cc < c && j <= mx_a) {
ok = false;
}
if (cc > c && j <= mx_b) {
ok = false;
}
if (ok) {
dp[i][c] = ckadd(dp[i][c], dp[j][cc]);
}
}
}
} */
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... |