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
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; const int N = 5e4 + 10, B = 100, iB = 510, mod = 1e9 + 7; int tot, id[N], st[N], ed[N]; int n, m, p[N], w[N], ans, tc[iB][N], ts[iB][N]; void adj (int &x) { x += x >> 31 & mod; } void add (int x, int y, int op, int k) { for (int i = x; i <= tot; i += i & -i) for (int j = y; j <= n; j += j & -j) if (op) adj(tc[i][j] += 1 - mod), adj(ts[i][j] += k - mod); else adj(--tc[i][j]), adj(ts[i][j] -= k); } void ask (int x, int y, int op, int &c, int &s) { for (int i = x; i; i -= i & -i) for (int j = y; j; j -= j & -j) if (op) adj(c += tc[i][j] - mod), adj(s += ts[i][j] - mod); else adj(c -= tc[i][j]), adj(s -= ts[i][j]); } void query (int l, int r, int a, int b, int &c, int &s) { c = 0, s = 0; if (l > r) return; if (id[l] == id[r]) { for (int i = l; i <= r; ++i) if (p[i] >= a && p[i] <= b) adj(c += 1 - mod), adj(s += w[i] - mod); return; } for (int i = l; i <= ed[id[l]]; ++i) if (p[i] >= a && p[i] <= b) adj(c += 1 - mod), adj(s += w[i] - mod); ask(id[r] - 1, b, 1, c, s), ask(id[l], b, 0, c, s), ask(id[r] - 1, a - 1, 0, c, s), ask(id[l], a - 1, 1, c, s); for (int i = st[id[r]]; i <= r; ++i) if (p[i] >= a && p[i] <= b) adj(c += 1 - mod), adj(s += w[i] - mod); } int main () { read(n, m); for (int i = 1; i <= n; ++i) id[i] = (i - 1) / B + 1; tot = id[n]; for (int i = 1; i <= tot; ++i) st[i] = ed[i - 1] + 1, ed[i] = ed[i - 1] + B; ed[tot] = n; for (int i = 1; i <= n; ++i) { read(p[i], w[i]); int c, s; query(1, i - 1, p[i] + 1, n, c, s); ans = ((LL)c * w[i] + s + ans) % mod; add(id[i], p[i], 1, w[i]); } for (int a, b; m; --m) { read(a, b); if (a > b) swap(a, b); int c, s; if (p[a] > p[b]) adj(ans -= w[a] + w[b]); query(a + 1, b - 1, 1, p[a] - 1, c, s); ans = (-(LL)c * w[a] - s + ans) % mod; query(a + 1, b - 1, p[b] + 1, n, c, s); ans = (-(LL)c * w[b] - s + ans) % mod; add(id[a], p[a], 0, w[a]), add(id[b], p[b], 0, w[b]); swap(p[a], p[b]), swap(w[a], w[b]); if (p[a] > p[b]) adj(ans += w[a] + w[b] - mod); query(a + 1, b - 1, 1, p[a] - 1, c, s); ans = ((LL)c * w[a] + s + ans) % mod; query(a + 1, b - 1, p[b] + 1, n, c, s); ans = ((LL)c * w[b] + s + ans) % mod; add(id[a], p[a], 1, w[a]), add(id[b], p[b], 1, w[b]); adj(ans), write(ans), puts(""); } return 0; }
|