Submission #862527

#TimeUsernameProblemLanguageResultExecution timeMemory
862527boris_mihovMisspelling (JOI22_misspelling)C++17
100 / 100
1134 ms166508 KiB
#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]; std::stack <int> active[ALPHABET][2]; std::vector <int> begin[MAXN]; 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][0].push(pos + 1); active[letter][1].push(pos + 1); for (const int &j : begin[pos]) { int t = type[j] - 1; if (type[j] == 1) { while (active[letter][t].size() && active[letter][t].top() <= r[j]) { int nextPos = active[letter][t].top(); for (int remL = letter + 1 ; remL < ALPHABET ; ++remL) { sub(dp[pos][letter], dp[nextPos][remL]); } active[letter][t].pop(); } } else { while (active[letter][t].size() && active[letter][t].top() <= r[j]) { int nextPos = active[letter][t].top(); for (int remL = letter - 1 ; remL >= 0 ; --remL) { sub(dp[pos][letter], dp[nextPos][remL]); } active[letter][t].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); } } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...