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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cctype> #include <vector> #include <queue> #include <stack> #include <cmath> #include <set> #include <map>
using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair<int, int> pii;
inline int rd() { register int x = 0, f = 0, c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); return f ? -x : x; }
const int maxn = 2e+5 + 5;
int n, m, a[maxn], b[maxn]; ll ans;
struct Treap { struct node { int ch[2], v, siz, k, w; ll sum; node() { ch[1] = ch[0] = 0; v = w = siz = k = 0; } }t[maxn]; int ptr, rt;
#define lson t[p].ch[0] #define rson t[p].ch[1] inline int newnode(int val) { ++ptr; t[ptr].v = val; t[ptr].w = rand(); t[ptr].siz = t[ptr].k = 1; t[ptr].sum = val; return ptr; } inline void pushup(int p) { t[p].siz = t[lson].siz + t[rson].siz + t[p].k; t[p].sum = t[lson].sum + t[rson].sum + 1ll * t[p].v * t[p].k; } inline void rturn(int &p) { int q = lson; lson = t[q].ch[1]; t[q].ch[1] = p; p = q; pushup(rson); pushup(p); } inline void lturn(int &p) { int q = rson; rson = t[q].ch[0]; t[q].ch[0] = p; p = q; pushup(lson); pushup(p); } void Insert(int &p, int x) { if (!p) { p = newnode(x); return; } t[p].siz++; t[p].sum += x; if (t[p].v == x) { t[p].k++; return; } else if (t[p].v < x) { Insert(rson, x); if (t[rson].w < t[p].w) lturn(p); } else { Insert(lson, x); if (t[lson].w < t[p].w) rturn(p); } } void Delete(int &p, int x) { if (!p) return; if (t[p].v == x) { if (t[p].k > 1) t[p].k--, t[p].siz--, t[p].sum -= x; else { if (!(lson && rson)) p = lson + rson; else if (t[rson].w > t[lson].w) rturn(p), Delete(p, x); else lturn(p), Delete(p, x); } } else { t[p].siz--; t[p].sum -= x; if (t[p].v < x) Delete(rson, x); else Delete(lson, x); } } ll GetLower(int p, int k) { if (!p) return 0; if (t[p].v == k) { return t[p].sum - t[rson].sum; } else if (t[p].v < k) { ll ret = t[p].sum - t[rson].sum + GetLower(rson, k); return ret; } else { ll ret = GetLower(lson, k); return ret; } } int GetLowerSize(int p, int k) { if (!p) return 0; if (t[p].v == k) { return t[p].siz - t[rson].siz; } else if (t[p].v < k) { return t[p].siz - t[rson].siz + GetLowerSize(rson, k); } else { return GetLowerSize(lson, k); } } #undef lson #undef rson }A, B;
int main() { freopen("graph.in", "r", stdin); freopen("graph.out", "w", stdout); n = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); A.Insert(A.rt, a[i]); } for (int i = 1; i <= n; ++i) { b[i] = rd(); B.Insert(B.rt, b[i]); } for (int i = 1; i <= n; ++i) { ll lsum = B.GetLower(B.rt, a[i]); int rsiz = n - B.GetLowerSize(B.rt, a[i]); ans += lsum + 1ll * rsiz * a[i]; } printf("%lld\n", ans); m = rd(); int typ, p, x; while (m--) { typ = rd(); p = rd(); x = rd(); if (typ) { ll lsum = A.GetLower(A.rt, b[p]); int rsiz = n - A.GetLowerSize(A.rt, b[p]); ans -= lsum + 1ll * rsiz * b[p]; B.Delete(B.rt, b[p]); b[p] = x; lsum = A.GetLower(A.rt, b[p]); rsiz = n - A.GetLowerSize(A.rt, b[p]); ans += lsum + 1ll * rsiz * b[p]; B.Insert(B.rt, b[p]); } else { ll lsum = B.GetLower(B.rt, a[p]); int rsiz = n - B.GetLowerSize(B.rt, a[p]); ans -= lsum + 1ll * rsiz * a[p]; A.Delete(A.rt, a[p]); a[p] = x; lsum = B.GetLower(B.rt, a[p]); rsiz = n - B.GetLowerSize(B.rt, a[p]); ans += lsum + 1ll * rsiz * a[p]; A.Insert(A.rt, a[p]); } printf("%lld\n", ans); } fclose(stdin); fclose(stdout); return 0; }
|