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>
#define fi first
#define se second
const int N = 500500;
const int mod = 1e9 + 7;
using namespace std;
void add(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
} else if (x < 0) {
x += mod;
}
}
int n;
int m;
int d[N][26];
int s[N][26];
vector<int> A[N], B[N];
int main() {
#ifdef zxc
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (a == b) {
continue;
}
if (a < b) {
A[a].push_back(b);
} else {
B[b].push_back(a);
}
}
for (int i = 0; i < 26; i++) {
d[1][i] = s[1][i] = 1;
}
set<pair<int, int>> S, T;
for (int i = 2; i <= n; i++) {
for (int j: A[i - 1]) {
S.insert({i - 1, j});
}
for (int j: B[i - 1]) {
T.insert({i - 1, j});
}
while (!S.empty() && (--S.end())->se < i) {
S.erase(--S.end());
}
while (!T.empty() && (--T.end())->se < i) {
T.erase(--T.end());
}
int p_1 = S.empty() ? 0 : (--S.end())->fi;
int p_2 = T.empty() ? 0 : (--T.end())->fi;
for (int j = 0; j < 26; j++) {
for (int h = 0; h < j; h++) {
add(d[i][j], s[i - 1][h] - s[max(p_1, p_2)][h]);
if (p_1 < p_2) {
add(d[i][j], s[p_2][h] - s[p_1][h]);
}
}
for (int h = j + 1; h < 26; h++) {
add(d[i][j], s[i - 1][h] - s[max(p_1, p_2)][h]);
if (p_1 > p_2) {
add(d[i][j], s[p_1][h] - s[p_2][h]);
}
}
s[i][j] = s[i - 1][j];
add(s[i][j], d[i][j]);
}
}
int res = 0;
for (int i = 0; i < 26; i++) {
add(res, s[n][i]);
}
cout << res << "\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... |