이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mod = 1e9 + 7;
const int mxN = 5e5 + 5;
const int alpha = 26;
int dp[mxN][alpha];
vector<int> s[mxN], g[mxN];
int sl[mxN], gl[mxN];
void testCase() {
int n, m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
if (a < b) {
s[a].push_back(b);
} else {
g[b].push_back(a);
}
}
set<int> gst, lst;
for (int i = 1; i <= n; i++) gst.insert(i);
lst = gst;
for (int i = n; i >= 1; i--) {
sort(s[i].begin(), s[i].end());
sort(g[i].begin(), g[i].end());
if (!s[i].empty()) {
int x = s[i].back();
auto it = gst.upper_bound(x);
while (it != gst.begin() && *(--it) > i) {
gl[*it] = i;
gst.erase(it);
it = gst.upper_bound(x);
}
}
if (!g[i].empty()) {
int x = g[i].back();
auto it = lst.upper_bound(x);
while (it != lst.begin() && *(--it) > i) {
sl[*it] = i;
lst.erase(it);
it = lst.upper_bound(x);
}
}
}
for (int j = 0; j < 26; j++) dp[1][j] = 1;
for (int i = 2; i <= n; i++) {
int ss[26], gg[26];
memset(ss, 0, sizeof ss);
memset(gg, 0, sizeof gg);
for (int j = 0; j < 26; j++) {
if (j > 0) ss[j - 1] = dp[i - 1][j] - dp[sl[i]][j];
if (j < 25) gg[j + 1] = dp[i - 1][j] - dp[gl[i]][j];
}
for (int j = 0; j < 26; j++) {
ss[j] %= mod;
gg[j] %= mod;
}
for (int j = 1; j < 26; j++) {
gg[j] += gg[j - 1];
gg[j] %= mod;
}
for (int j = 24; j >= 0; j--) {
ss[j] += ss[j + 1];
ss[j] %= mod;
}
for (int j = 0; j < 26; j++) {
dp[i][j] = (ss[j] + gg[j]) % mod;
dp[i][j] += dp[i - 1][j];
dp[i][j] %= mod;
}
}
int ans = 0;
for (int j = 0; j < 26; j++) {
ans += dp[n][j];
ans %= mod;
}
if (ans < 0) ans += mod;
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
}
# | 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... |