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 <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>
typedef long long llong;
const int MAXN = 500000 + 10;
const int ALPHABET = 26;
const int MOD = 1e9 + 7;
int n, m;
int l[MAXN];
int r[MAXN];
int type[MAXN];
int dp[MAXN][ALPHABET];
bool bl[MAXN][ALPHABET];
std::stack <int> active[ALPHABET][ALPHABET];
std::vector <int> begin[MAXN];
std::vector <int> end[MAXN];
int f(int pos, int letter)
{
if (bl[pos][letter])
{
return dp[pos][letter];
}
int cntOne = 0;
int cntTwo = 0;
bl[pos][letter] = true;
dp[pos][letter] = 1;
for (int i = pos ; i < n ; ++i)
{
for (const int &j : begin[i])
{
cntOne += (type[j] == 1);
cntTwo += (type[j] == 2);
}
for (const int &j : end[i])
{
if (l[j] < pos)
{
continue;
}
cntOne -= (type[j] == 1);
cntTwo -= (type[j] == 2);
}
if (cntOne == 0)
{
for (int j = letter + 1 ; j < 26 ; ++j)
{
dp[pos][letter] += f(i + 1, j);
if (dp[pos][letter] >= MOD) dp[pos][letter] -= MOD;
}
}
if (cntTwo == 0)
{
for (int j = letter - 1 ; j >= 0 ; --j)
{
dp[pos][letter] += f(i + 1, j);
if (dp[pos][letter] >= MOD) dp[pos][letter] -= MOD;
}
}
}
return dp[pos][letter];
}
void add(int &num, int &val)
{
num += val;
if (num >= MOD) num -= MOD;
}
void sub(int &num, int &val)
{
num -= val;
if (num < 0) num += MOD;
}
void solve()
{
for (int i = ALPHABET - 1 ; i >= 0 ; --i)
{
dp[n][i] = 1;
}
for (int pos = n - 1 ; pos >= 1 ; --pos)
{
for (int letter = 0 ; letter < ALPHABET ; ++letter)
{
dp[pos][letter] = dp[pos + 1][letter];
for (int addL = 0 ; addL < ALPHABET ; ++addL)
{
if (addL == letter)
{
continue;
}
add(dp[pos][letter], dp[pos + 1][addL]);
active[letter][addL].push(pos + 1);
}
for (const int &j : begin[pos])
{
if (type[j] == 1)
{
for (int remL = letter + 1 ; remL < ALPHABET ; ++remL)
{
while (active[letter][remL].size() && active[letter][remL].top() <= r[j])
{
sub(dp[pos][letter], dp[active[letter][remL].top()][remL]);
active[letter][remL].pop();
}
}
} else
{
for (int remL = letter - 1 ; remL >= 0 ; --remL)
{
while (active[letter][remL].size() && active[letter][remL].top() <= r[j])
{
sub(dp[pos][letter], dp[active[letter][remL].top()][remL]);
active[letter][remL].pop();
}
}
}
}
}
}
int sum = 0;
for (int i = 0 ; i < 26 ; ++i)
{
sum += dp[1][i];
if (sum >= MOD) sum -= MOD;
}
std::cout << sum << '\n';
}
void input()
{
std::cin >> n >> m;
for (int i = 1 ; i <= m ; ++i)
{
std::cin >> l[i] >> r[i];
type[i] = 1;
if (l[i] > r[i])
{
std::swap(l[i], r[i]);
type[i] = 2;
}
begin[l[i]].push_back(i);
end[r[i]].push_back(i);
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
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... |