This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 5e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
ll n, m, a[N], rmn[N], rmx[N], dp[27][N], pr[27][N], pr2[27][N];
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>m;
for(ll v,u,i=1; i<=m; i++) {
cin>>v>>u;
if(v<u) rmx[v] = max(rmx[v], u);
else rmn[u] = max(rmn[u], v);
}
vector<int> smn={1}, smx={1};
for(int i=1; i<=26; i++) dp[i][1]=pr2[i][1]=1, pr[i][1]=i;
for(int i=2; i<=n; i++) {
while(size(smx)>0&&rmx[smx.back()]<i) smx.pop_back();
while(size(smn)>0&&rmn[smn.back()]<i) smn.pop_back();
int mx=(size(smx)==0?0:smx.back());
int mn=(size(smn)==0?0:smn.back());
if(mn >= mx) {
for(int j=1; j<=26; j++) {
dp[j][i] = MOD(pr[26][i-1]-pr[26][mn]-(pr2[j][i-1]-pr2[j][mn])+
pr[26][mn]-pr[26][mx]-(pr[j][mn]-pr[j][mx]));
}
} else {
for(int j=1; j<=26; j++) {
dp[j][i] = MOD(pr[26][i-1]-pr[26][mx]-(pr2[j][i-1]-pr2[j][mx])+
(pr[j-1][mx]-pr[j-1][mn]));
}
}
for(int j=1; j<=26; j++) {
pr2[j][i] = MOD(pr2[j][i-1] + dp[j][i]);
pr[j][i] = MOD(pr2[j][i] + pr[j-1][i]);
}
while(size(smx)>0&&rmx[smx.back()]<=rmx[i]) smx.pop_back();
smx.pb(i);
while(size(smn)>0&&rmn[smn.back()]<=rmn[i]) smn.pop_back();
smn.pb(i);
}
cout<<pr[26][n]<<endl;
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... |