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
| #include<bits/stdc++.h> #define LL long long #define PII pair<int, int> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();} while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if(flag) res = -res; } template <class T, class ...Arg> inline void read(T &res, Arg &...com){ read(res), read(com...);} template <class T> void out(T res) { if(res > 9) out(res / 10); putchar(res % 10 + '0'); } template <class T> inline void write(T res) { if(res < 0) putchar('-'), res = -res; out(res), putchar('\n'); } const int N = 4e4 + 5; int n, m, l, r, root; vector<int>s[N]; namespace pol{ int x, y, z, tot; struct Node{ int s[2], id, v, sz; LL val; #define l(x) tr[x].s[0] #define r(x) tr[x].s[1] }tr[N << 2]; inline void pushup(int x) { tr[x].sz = tr[l(x)].sz + tr[r(x)].sz + 1; tr[x].val = tr[l(x)].val + tr[r(x)].val + 1ll * (tr[l(x)].sz + 1) * (tr[r(x)].sz + 1) * tr[x].v; } inline int make(int id, int v) { tr[++ tot].id = id, tr[tot].v = v, tr[tot].sz = 1; return tot; } int merge(int x, int y) { if(!x || !y) return x | y; if(tr[x].v > tr[y].v) { r(x) = merge(r(x), y); pushup(x); return x; } else{ l(y) = merge(x, l(y)); pushup(y); return y; } } void split(int now, int k, int &x, int &y) { if(!now) x = 0, y = 0; else{ if(tr[now].id <= k) x = now, split(r(now), k, r(now), y); else y = now, split(l(now), k, x, l(now)); pushup(now); } } inline void modify(int id, int v) { split(root, id - 1, x, y), split(y, id, y, z); tr[y].v = v; root = merge(x, merge(y, z)); } inline void insert(int id, int v) { split(root, id - 1, x, y); root = merge(x, merge(make(id, v), y)); } } LL ans; int main() { read(l, r, n); for(int i = 1, st, ed;i <= n;i ++) read(st, ed), s[st].push_back(ed); for(int i = 1;i <= r;i ++) pol:: insert(i, 0); for(int i = 1;i <= l;i ++) { for(int j : s[i]) pol:: modify(j, i); ans += 1ll * r * (r + 1) / 2 * i - pol:: tr[root].val; } write(1ll * l * (l + 1) / 2 * r * (r + 1) / 2 - ans); return 0; }
|