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
const int mod = 1e9 + 7;
struct mint {
int v;
mint(long long x) {
if (x >= mod) x %= mod;
v = x;
}
mint() {v = 0;}
mint& operator+=(mint b) {
if ((v += b.v) >= mod) {
v -= mod;
}
return *this;
}
mint& operator-=(mint b) {
if ((v -= b.v) < 0) {
v += mod;
}
return *this;
}
mint& operator*=(mint b) {
v = 1ll * v * b.v % mod;
return *this;
}
mint power(mint a, int b) {
mint ans = 1;
while (b) {
if (b & 1) {
ans *= a;
}
a *= a, b >>= 1;
}
return ans;
}
mint& operator/=(mint b) {
v = 1ll * v * power(b, mod - 2).v % mod;
return *this;
}
friend mint operator+(mint a, mint b) {
return a += b;
}
friend mint operator-(mint a, mint b) {
return a -= b;
}
friend mint operator*(mint a, mint b) {
return a *= b;
}
friend mint operator/(mint a, mint b) {
return a /= b;
}
friend istream& operator>>(istream &is, mint a) {
return is >> a.v;
}
friend ostream& operator<<(ostream &os, mint a) {
return os << a.v;
}
};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> st[n][2];
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
if (a < b) {
//s[i - 1] > s[i]
st[a + 1][0].push_back(b + 1);
} else {
//s[i - 1] < s[i]
st[b + 1][1].push_back(a + 1);
}
}
mint dp[n + 1][26], prfx[n + 1][26], sufx[n + 1][26];
for (int j = 0; j < 26; j++) prfx[n][j] = sufx[n][j] = dp[n][j] = 1;
for (int j = 1; j < 26; j++) prfx[n][j] += prfx[n][j - 1];
for (int j = 24; j >= 0; j--) sufx[n][j] += sufx[n][j + 1];
set<int> active[2];
mint sum[26][2];
for (int i = n - 1; i >= 0; i--) {
active[0].insert(i);
active[1].insert(i);
for (int j = 0; j < 26; j++) {
if (j - 1 >= 0) sum[j][0] += prfx[i + 1][j - 1];
if (j + 1 < 26) sum[j][1] += sufx[i + 1][j + 1];
}
for (auto en : st[i][0]) {
auto it = active[1].lower_bound(i);
while (it != active[1].end() && *it < en) {
for (int j = 0; j + 1 < 26; j++) {
sum[j][1] -= sufx[*it + 1][j + 1];
}
active[1].erase(it);
it = active[1].lower_bound(i);
}
}
for (auto en : st[i][1]) {
auto it = active[0].lower_bound(i);
while (it != active[0].end() && *it < en) {
for (int j = 1; j < 26; j++) {
sum[j][0] -= prfx[*it + 1][j - 1];
}
active[0].erase(it);
it = active[0].lower_bound(i);
}
}
for (int j = 0; j < 26; j++) {
prfx[i][j] = sufx[i][j] = dp[i][j] = sum[j][0] + sum[j][1] + 1;
}
for (int j = 1; j < 26; j++) {
prfx[i][j] += prfx[i][j - 1];
}
for (int j = 24; j >= 0; j--) {
sufx[i][j] += sufx[i][j + 1];
}
}
mint ans = 0;
for (int j = 0; j < 26; j++) {
ans += dp[1][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... |