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 ll long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l) cout << *l << " \n"[l + 1 == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
if (a.empty()) return o << "{}";
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
vv (int n, int m, T v) : vector <vector <T>> (n, vector <T>(m, v)) {}
vv (int n, int m) : vector <vector <T>> (n, vector <T>(m)) {}
vv () {}
};
template <typename T> struct vvv : vector <vv <T>> {
vvv (int n, int m, int k, T v) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
vvv (int n, int m, int k) : vector <vv <T>> (n, vv <T>(m, k)) {}
vvv () {}
};
#ifdef Doludu
#define test(args...) abc("[" + string(#args) + "]", args)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(args...) void(0)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 1e9 + 7, N = 500002;
void add(int &i, int j) {
i += j;
if (i >= mod)
i -= mod;
}
template <typename T>
struct SparseTableMax {
// 0-indexed, [l, r)
vector <vector <T>> table;
SparseTableMax () = default;
SparseTableMax (vector <T> a) {
int n = a.size();
int m = __lg(n) + 1;
table.resize(n, vector <T> (m, 0));
for (int i = 0; i < n; ++i)
table[i][0] = a[i];
for (int j = 1; j < m; ++j) {
for (int i = 0; i + (1 << j) <= n; ++i) {
table[i][j] = max(table[i][j - 1], table[i + (1 << j - 1)][j - 1]);
}
}
}
T query(int l, int r) {
if (l >= r)
return -1 << 30;
int g = __lg(r - l);
return max(table[l][g], table[r - (1 << g)][g]);
}
};
int dp[N][29], pre[N][26];
void solve() {
int n, m;
cin >> n >> m;
vector <int> mx1(n), mx2(n);
auto add_edge = [&](int u, int v) {
if (u < v)
mx1[u] = max(mx1[u], v);
else
mx2[v] = max(mx2[v], u);
};
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v, --u, --v;
add_edge(u, v);
}
SparseTableMax <int> ST1(mx1), ST2(mx2);
// s[i - 1] < s[i]
for (int i = 0; i < 26; ++i)
dp[0][i] = 1, pre[1][i] = 1;
for (int i = 1; i < n; ++i) {
// dp[i] -> dp[j];
// s[i - 1] != s[i] -> only care range start from i
// s[j - 1] < s[j]
// no i -> j, i < j
{
int l = -1, r = i;
while (r - l > 1) {
int m = l + r >> 1;
if (ST1.query(m, i) < i)
r = m;
else
l = m;
}
int tmp = 0;
for (int c = 0; c < 26; ++c) {
add(dp[i][c], tmp);
add(tmp, mod - pre[r][c]);
add(tmp, pre[i][c]);
}
}
// s[j - 1] > s[j]
// no i -> j, i > j
{
int l = -1, r = i;
while (r - l > 1) {
int m = l + r >> 1;
if (ST2.query(m, i) < i)
r = m;
else
l = m;
}
int tmp = 0;
for (int c = 25; ~c; --c) {
add(dp[i][c], tmp);
add(tmp, mod - pre[r][c]);
add(tmp, pre[i][c]);
}
}
for (int c = 0; c < 26; ++c)
pre[i + 1][c] = pre[i][c], add(pre[i + 1][c], dp[i][c]);
}
int ans = 0;
for (int i = 0; i < n; ++i) for (int c = 0; c < 26; ++c) {
add(ans, dp[i][c]);
}
cout << ans << '\n';
}
int main () {
owo;
int t = 1;
// cin >> t;
while (t--)
solve();
}
Compilation message (stderr)
misspelling.cpp: In function 'void solve()':
misspelling.cpp:110:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | int m = l + r >> 1;
| ~~^~~
misspelling.cpp:129:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
129 | int m = l + r >> 1;
| ~~^~~
misspelling.cpp: In instantiation of 'SparseTableMax<T>::SparseTableMax(std::vector<_Tp>) [with T = int]':
misspelling.cpp:97:33: required from here
misspelling.cpp:69:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
69 | table[i][j] = max(table[i][j - 1], table[i + (1 << j - 1)][j - 1]);
| ~~^~~
# | 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... |