Submission #567177

#TimeUsernameProblemLanguageResultExecution timeMemory
567177maomao90Misspelling (JOI22_misspelling)C++17
100 / 100
390 ms348084 KiB

// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 500005;
const int MOD = 1000000007;

int n, m;
int lr[MAXN][2];
//ll dp[2][MAXN][30];
ll dp[MAXN][30], psm[MAXN][30], rowsm[30][MAXN];
ll ans;

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n >> m;
    REP (i, 0, m) {
        int a, b; cin >> a >> b;
        bool s = 0;
        if (a > b) {
            s = 1;
            swap(a, b);
        }
        mxto(lr[a][s], b);
    }
    REP (c, 0, 26) {
        dp[1][c] = 1;
        psm[1][c] = c + 1;
        rowsm[c][1] = c + 1;
    }
    vii stk[2];
    int ptr[2] = {0, 0};
    REP (i, 2, n + 1) {
        REP (z, 0, 2) {
            while (!stk[z].empty() && stk[z].back().FI < lr[i - 1][z]) {
                stk[z].pop_back();
            }
            stk[z].pb({lr[i - 1][z], i - 1});
            while (ptr[z] >= SZ(stk[z]) || 
                    (ptr[z] >= 0 && stk[z][ptr[z]].FI < i)) {
                ptr[z]--;
            }
            while (ptr[z] + 1 < SZ(stk[z]) && stk[z][ptr[z] + 1].FI >= i) {
                ptr[z]++;
            }
            // increment range (stk[z][ptr[z]].SE, i - 1]
            int s = ptr[z] < 0 ? -1 : stk[z][ptr[z]].SE; // exclusive
            REP (c, 0, 26) {
                ll fi, se;
                if (z) {
                    if (c) {
                        fi = rowsm[c - 1][i - 1];
                        se = (s < 0 ? 0 : rowsm[c - 1][s]);
                    } else {
                        fi = 0;
                        se = 0;
                    }
                } else {
                    fi = rowsm[25][i - 1] - rowsm[c][i - 1];
                    se = (s < 0 ? 0 : (rowsm[25][s] - rowsm[c][s]));
                    if (fi < 0) {
                        fi += MOD;
                    }
                    if (se < 0) {
                        se += MOD;
                    }
                }
                dp[i][c] += fi - se;
                if (dp[i][c] < 0) {
                    dp[i][c] += MOD;
                }
                if (dp[i][c] >= MOD) {
                    dp[i][c] -= MOD;
                }
            }
        }
        psm[i][0] = dp[i][0];
        REP (c, 1, 26) {
            psm[i][c] = psm[i][c - 1] + dp[i][c];
            if (psm[i][c] >= MOD) {
                psm[i][c] -= MOD;
            }
        }
        REP (c, 0, 26) {
            rowsm[c][i] = rowsm[c][i - 1] + psm[i][c];
            if (rowsm[c][i] >= MOD) {
                rowsm[c][i] -= MOD;
            }
        }
    }
    REP (j, 1, n + 1) {
        REP (c, 0, 26) {
            ans += dp[j][c];
            if (ans >= MOD) {
                ans -= MOD;
            }
        }
    }
    /*
    int cur = 1, prv = 0;
    REP (c, 0, 26) {
        dp[cur][1][c] = 1;
    }
    REP (i, 2, n + 1) {
        swap(cur, prv);
        int r[2] = {0, 0};
        RREP (j, i - 1, 1) {
            mxto(r[0], lr[j][0]);
            mxto(r[1], lr[j][1]);
            ll lsm = 0, rsm = 0;
            REP (c, 0, 26) {
                rsm += dp[prv][j][c];
                if (rsm >= MOD) {
                    rsm -= MOD;
                }
                // stay the same
                dp[cur][j][c] = dp[prv][j][c]; 
            }
            REP (c, 0, 26) {
                rsm -= dp[prv][j][c];
                if (rsm < 0) {
                    rsm += MOD;
                }
                if (r[0] < i) {
                    dp[cur][i][c] += rsm;
                    if (dp[cur][i][c] >= MOD) {
                        dp[cur][i][c] -= MOD;
                    }
                }
                if (r[1] < i) {
                    dp[cur][i][c] += lsm;
                    if (dp[cur][i][c] >= MOD) {
                        dp[cur][i][c] -= MOD;
                    }
                }
                lsm += dp[prv][j][c];
                if (lsm >= MOD) {
                    lsm -= MOD;
                }
            }
        }
    }
    REP (j, 1, n + 1) {
        REP (c, 0, 26) {
            ans += dp[cur][j][c];
            if (ans >= MOD) {
                ans -= MOD;
            }
        }
    }
    */
    cout << ans << '\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...