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
|
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &a){ T w=1; a=0; char ch=getchar(); for(;ch < '0' || ch > '9';ch=getchar()) if(ch == '-') w=-1; for(;ch>='0'&&ch<='9';ch=getchar()) a=(a<<3)+(a<<1)+(ch^48); a*=w; } template <typename T> inline void ckmax(T &a, T b){a = a > b ? a : b;} template <typename T> inline void ckmin(T &a, T b){a = a < b ? a : b;} #define fi first #define se second #define pb push_back #define mp make_pair #define mii map<int, int> #define pii pair<int, int> #define ins insert #define era erase #define vi vector<int> #define Debug(x) cout << #x << " = " << x << endl #define For(i,l,r) for (int i = l; i <= r; ++i) #define foR(i,l,r) for (int i = l; i >= r; --i) const int N = 3e5 + 10; const int Mod = 1e9 + 7; inline int Mul (int x, int y) { return 1ll * x * y >= Mod ? (1ll * x * y % Mod) : (x * y); } inline int Add (int x, int y) { return (x + y >= Mod) ? (x + y - Mod) : x + y; } inline int Sub (int x, int y) { return (x - y < 0) ? (x - y + Mod) : (x - y); } int qpow (int a, int b) { int ans = 1, base = a; while (b) { if (b & 1) ans = Mul(ans, base); base = Mul(base, base); b >>= 1; } return ans; } int fac[N], ifac[N]; void Init (int M) { fac[0] = ifac[0] = 1; For (i, 1, M) fac[i] = Mul(fac[i - 1], i); ifac[M] = qpow(fac[M], Mod - 2); foR (i, M - 1, 1) ifac[i] = Mul(ifac[i + 1], i + 1); } int bin (int a, int b) { if (a < b || a < 0 || b < 0) return 0; return Mul(fac[a], Mul(ifac[a - b], ifac[b])); } int Inv (int s) { int a = qpow(s, Mod - 2); return (a ? a : 1); } set <int> mck; int a[N], s[N], tot; int Ans; int n, m; int eps(int x) { return x ? x : 1; } inline void Era (int x) { auto it1 = mck.lower_bound(x); it1--; int pre = *it1; Ans = Mul(Ans, Mul(eps(n - x), fac[x - pre - 1])); auto it2 = mck.upper_bound(x); int nxt = *it2; Ans = Mul(Ans, Mul(eps(n - nxt), fac[nxt - x - 1])); mck.erase(x); Ans = Mul(Ans, Mul(Inv(n - nxt), ifac[nxt - pre - 1])); } inline void Ins (int x) { auto it1 = mck.lower_bound(x), it2 = mck.upper_bound(x); it1--; int pre = *it1, nxt = *it2; Ans = Mul(Ans, Mul(eps(n - nxt), fac[nxt - pre - 1])); Ans = Mul(Ans, Mul(Inv(n - x), ifac[x - pre - 1])); Ans = Mul(Ans, Mul(Inv(n - nxt), ifac[nxt - x - 1])); mck.insert(x); }
int del[N];
int main() { Init(N - 10); read(n); read(m); For (i, 1, n) read(a[i]); s[++tot] = 0; mck.ins(0); For (i, 1, n - 1) if (a[i] < a[i + 1]) s[++tot] = i, mck.ins(i); s[++tot] = n; mck.ins(n); Ans = 1; For (i, 2, tot) Ans = Mul(Ans, Mul(Inv(n - s[i]), ifac[s[i] - s[i - 1] - 1])); printf ("%d\n", Mul(Ans, fac[n - 1])); For (i, 1, m) { int x, y; read(x); read(y);
if (y < n && !del[y]) if (a[y] < a[y + 1]) Era(y), del[y] = 1; if (y > 1 && !del[y - 1]) if (a[y - 1] < a[y]) Era(y - 1), del[y - 1] = 1; if (x < n && !del[x]) if (a[x] < a[x + 1]) Era(x), del[x] = 1; if (x > 1 && !del[x - 1]) if (a[x - 1] < a[x]) Era(x - 1), del[x - 1] = 1; swap(a[x], a[y]); del[x] = del[y] = del[x - 1] = del[y - 1] = 0; if (y < n && !del[y]) if (a[y] < a[y + 1]) Ins(y), del[y] = 1; if (y > 1 && !del[y - 1]) if (a[y - 1] < a[y]) Ins(y - 1), del[y - 1] = 1; if (x < n && !del[x]) if (a[x] < a[x + 1]) Ins(x), del[x] = 1; if (x > 1 && !del[x - 1]) if (a[x - 1] < a[x]) Ins(x - 1), del[x - 1] = 1; printf ("%d\n", Mul(Ans, fac[n - 1])); del[x] = del[y] = del[x - 1] = del[y - 1] = 0; } }
|