Submission #596927

#TimeUsernameProblemLanguageResultExecution timeMemory
596927AlexLuchianovMisspelling (JOI22_misspelling)C++14
100 / 100
881 ms114708 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <set>
#include <numeric>

using ll = long long;

int const nmax = 500000;
int const modulo = 1000000007;
int const sigma = 26;
std::vector<int> g[5 + nmax];

int dp[5 + nmax][1 + sigma];
int sum[1 + sigma], type[5 + nmax];

std::set<int> myset[4];

void affectSum(int pos, int mode, int coef) {
  int acc = 0;
  for(int i = 0; i < sigma; i++) { 
    if((mode & 1) == 0)
      sum[i] = (1LL * modulo + sum[i] + coef * acc) % modulo;
    acc += dp[pos][i];
    if(modulo <= acc)
      acc -= modulo;
  }
  acc = 0;
  for(int i = sigma - 1; 0 <= i; i--) {
    if((mode & 2) == 0)
      sum[i] = (1LL * modulo + sum[i] + coef * acc) % modulo;
    acc += dp[pos][i];
    if(modulo <= acc)
      acc -= modulo;
  }
}

int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  int n, m;
  std::cin >> n >> m;
  for(int i = 1;i <= m; i++) {
    int x, y;
    std::cin >> x >> y;
    if(x < y)
      g[x].push_back(y);
    else
      g[y].push_back(-x);
  }

  for(int i = 0; i < sigma; i++)
    sum[i] = dp[n][i] = 1;
  
  std::vector<int> tempSet(n);
  std::iota(tempSet.begin(), tempSet.end(), 1);
  myset[0] = std::set<int>(tempSet.begin(), tempSet.end());
  affectSum(n, 0, 1);

  for(int i = n - 1; 1 <= i; i--) {
    for(auto to : g[i]) {
      if(0 < to) {
        std::set<int>::iterator it = myset[0].lower_bound(i + 1);
        while(it != myset[0].end() && *it <= to) {
          affectSum(*it, 0, -1);
          type[*it] = 1;
          affectSum(*it, 1, 1);
          myset[1].insert(*it);
          myset[0].erase(it++);
        }
        it = myset[2].lower_bound(i + 1);
        while(it != myset[2].end() && *it <= to) {
          affectSum(*it, 2, -1);
          type[*it] = 3;
          affectSum(*it, 3, 1);
          myset[3].insert(*it);
          myset[2].erase(it++);
        }
      } else {
        to = -to;
        std::set<int>::iterator it = myset[0].lower_bound(i + 1);
        while(it != myset[0].end() && *it <= to) {
          affectSum(*it, 0, -1);
          type[*it] = 2;
          affectSum(*it, 2, 1);
          myset[2].insert(*it);
          myset[0].erase(it++);
        }
        it = myset[1].lower_bound(i + 1);
        while(it != myset[1].end() && *it <= to) {
          affectSum(*it, 1, -1);
          type[*it] = 3;
          affectSum(*it, 3, 1);
          myset[3].insert(*it);
          myset[1].erase(it++);
        }
      }
    }
    for(int j = 0; j < sigma; j++)
      dp[i][j] = sum[j];
    affectSum(i, 0, 1);
  }
  
  int result = 0;
  for(int i = 0; i < sigma;i++) {
    result += dp[1][i];
    if(modulo <= result)
      result -= modulo;
  }
  std::cout << result << '\n';
  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...