1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <bits/stdc++.h> using namespace std; namespace FastIO{ #define iL (1 << 20) char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL; #define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++) template<typename T> inline void read(T &a) { char ch;int sign = 0; for(ch = gc();!isdigit(ch);ch = gc()) if(ch == '-') sign = 1; a = ch & 15; for(ch = gc();isdigit(ch);ch = gc()) a = (a << 3) + (a << 1) + (ch & 15); if(sign) a = -a; } char Out[iL],*iter = Out; #define flush() fwrite(Out,1,iter - Out,stdout),iter = Out template<typename T> inline void write(T x,char end = '\n') { int c[40],l = 0;if(x < 0) *iter++ = '-',x = -x; do c[++l] = x % 10,x /= 10; while(x); while(l) *iter++ = c[l--] + '0'; *iter++ = end;flush(); } #undef iL #undef gc #undef flush } using namespace FastIO; const int N = 5e5 + 5,P = 1e9 + 7,C = 26; inline void Plus(int &x,const int &y) { x += y;if(x >= P) x -= P;} int n,m; int a[N],b[N]; bool tp[N]; int Mxr[2][N]; int L[N],R[N]; int f[N][C][2],sum[N][C][2]; int ST[2][20][N],lg[N]; inline int Qmax(int (*ST)[N],int l,int r) { int k = lg[r - l + 1]; return max(ST[k][l],ST[k][r-(1<<k)+1]); } int main() { read(n);read(m); for(int i = 1;i <= m;i++) read(a[i]),read(b[i]); for(int i = 1;i <= m;i++) if(a[i] < b[i]) Mxr[0][a[i]] = max(Mxr[0][a[i]],b[i] - 1); else Mxr[1][b[i]] = max(Mxr[1][b[i]],a[i] - 1); lg[0] = -1; for(int i = 1;i <= n;i++) lg[i] = lg[i >> 1] + 1; for(int i = 1;i <= n;i++) ST[0][0][i] = Mxr[0][i],ST[1][0][i] = Mxr[1][i]; for(int j = 1;j < 20;j++) for(int i = 1;i + (1 << j) - 1 <= n;i++) ST[0][j][i] = max(ST[0][j - 1][i],ST[0][j - 1][i + (1 << j - 1)]), ST[1][j][i] = max(ST[1][j - 1][i],ST[1][j - 1][i + (1 << j - 1)]); for(int i = 1;i <= n;i++) { int v1,v2; int lef = 0,righ = i; while(lef < righ) { int mid = lef + righ + 1 >> 1; if(Qmax(ST[0],mid,i) >= i) lef = mid; else righ = mid - 1; } v1 = lef; lef = 0;righ = i; while(lef < righ) { int mid = lef + righ + 1 >> 1; if(Qmax(ST[1],mid,i) >= i) lef = mid; else righ = mid - 1; } v2 = lef; L[i] = min(v1,v2); R[i] = max(v1,v2) - 1; if(v1 < v2) tp[i] = 1; else tp[i] = 0; }
f[0][0][0] = f[0][1][1] = sum[0][0][0] = sum[0][1][1] = 1; for(int i = 1;i <= n;i++) { if(R[i] < i) { int ss[C]; memset(ss,0,sizeof ss); for(int j = 0;j < C;j++) { ss[j] = sum[i - 1][j][0]; if(R[i] >= 0) Plus(ss[j],P - sum[R[i]][j][0]); j ? Plus(ss[j],ss[j - 1]) : void(); } for(int j = 1;j < C;j++) Plus(f[i][j][0],ss[j - 1]),Plus(f[i][j][1],ss[j - 1]); memset(ss,0,sizeof ss); for(int j = C - 1;j > 0;j--) { ss[j] = sum[i - 1][j][1]; if(R[i] >= 0) Plus(ss[j],P - sum[R[i]][j][1]); j != C - 1 ? Plus(ss[j],ss[j + 1]) : void(); } for(int j = 0;j < C - 1;j++) Plus(f[i][j][0],ss[j + 1]),Plus(f[i][j][1],ss[j + 1]); } if(R[i] >= 0 && L[i] <= R[i]) { int ss[C]; memset(ss,0,sizeof ss); for(int j = 0;j < C;j++) { Plus(ss[j],sum[R[i]][j][0]); if(L[i] > 0) Plus(ss[j],P - sum[L[i] - 1][j][0]); if(j) Plus(ss[j],ss[j - 1]); } for(int j = 1;j < C;j++) Plus(f[i][j][tp[i]],ss[j - 1]); memset(ss,0,sizeof ss); for(int j = C - 1;j > 0;j--) { Plus(ss[j],sum[R[i]][j][1]); if(L[i] > 0) Plus(ss[j],P - sum[L[i] - 1][j][1]); if(j != C - 1) Plus(ss[j],ss[j + 1]); } for(int j = 0;j < C - 1;j++) Plus(f[i][j][tp[i]],ss[j + 1]); } for(int j = 0;j < C;j++) sum[i][j][0] = sum[i - 1][j][0],Plus(sum[i][j][0],f[i][j][0]), sum[i][j][1] = sum[i - 1][j][1],Plus(sum[i][j][1],f[i][j][1]); } int ans = 0; for(int j = 0;j < C;j++) Plus(ans,f[n][j][0]); cout << ans << endl; return 0; }
|