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;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e5+10, MOD = 1e9+7;
const int K = 26;
void add_self(int& a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
void sub_self(int& a, int b) {
a -= b;
if (a < 0) a += MOD;
}
void add_self(ar<int, K>& a, ar<int, K>& b) {
for (int i = 0; i < K; i++) add_self(a[i], b[i]);
}
void sub_self(ar<int, K>& a, ar<int, K>& b) {
for (int i = 0; i < K; i++) sub_self(a[i], b[i]);
}
int n, m;
int last_one[N], last_two[N];
ar<int, K> dp[N];
// keep stack for last_one, last_two, and both
// immediately lets you keep track of things
// store cumulative sums for each index
struct Stack {
vector<pair<int, ar<int, K>>> st;
ar<int, K> net;
Stack() {
st.emplace_back(N, ar<int, K>{});
net.fill(0);
}
void pop_less(int k) {
while (st.back().first < k) {
sub_self(net, st.back().second);
st.pop_back();
}
}
ar<int, K> sum() {
return net;
}
void push_final(int r, ar<int, K>& cur) {
st.emplace_back(r, cur);
add_self(net, cur);
}
};
void solve() {
cin >> n >> m;
memset(last_one, -1, sizeof(last_one));
memset(last_two, -1, sizeof(last_two));
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b, --a, --b;
if (a < b) {
last_one[a] = max(last_one[a], b);
} else {
last_two[b] = max(last_two[b], a);
}
}
dp[n-1].fill(1); // ending state
Stack st_one, st_two;
for (int i = n-2; i >= 0; i--) {
st_one.pop_less(last_one[i]);
st_two.pop_less(last_two[i]);
if (last_one[i] <= i) {
st_one.push_final(i, dp[i+1]);
}
if (last_two[i] <= i) {
st_two.push_final(i, dp[i+1]);
}
dp[i].fill(1);
ar<int, K> one = st_one.sum();
ar<int, K> two = st_two.sum();
for (int j = 0; j < K; j++) {
for (int nxt = 0; nxt < j; nxt++)
add_self(dp[i][j], one[nxt]);
for (int nxt = j+1; nxt < K; nxt++)
add_self(dp[i][j], two[nxt]);
}
/*
for (int j = K-1; j >= 0; j--) {
add_self(dp[i][j], 1);
int mx_one = -1, mx_two = -1;
for (int r = i; r < n-1; r++) {
mx_one = max(mx_one, last_one[r]);
mx_two = max(mx_two, last_two[r]);
if (mx_one <= r) { // i need to be greater than the next one
for (int nxt = 0; nxt < j; nxt++)
add_self(dp[i][j], dp[r+1][nxt]);
}
if (mx_two <= r) {
for (int nxt = j + 1; nxt < K; nxt++)
add_self(dp[i][j], dp[r+1][nxt]);
}
}
}
*/
}
int ans = 0;
for (int i = 0; i < K; i++) add_self(ans, dp[0][i]);
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
solve();
}
# | 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... |