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
|
#include <bits/stdc++.h> using namespace std; namespace FastIO { const std::size_t buf_size = 1 << 18; char I[buf_size], *p1, *p2, O[buf_size], s[128]; int p, q, f; inline char gc() { return p1 == p2 && (p2 = (p1 = I) + fread(I, 1, sizeof(I), stdin), p1 == p2) ? EOF : *p1++; } inline void pc(char c) { O[p++] = c; } inline void flush() { fwrite(O, 1, p, stdout), p = 0; } void chkf() { if (p >= buf_size >> 1) flush(); } struct AutoFlush { ~AutoFlush() { flush(); } } auto_flush;
template <typename T> void rd(T &x) { char c = gc(); x = 0, f = c == '-'; while (!isdigit(c)) c = gc(), c == '-' ? f = 1 : 0; while (isdigit(c)) x = x * 10 + c - '0', c = gc(); f ? x = -x : 1; } template <typename T> void wr(T x) { if (x < 0) x = -x, pc('-'); do s[++q] = (x % 10) | 48; while (x /= 10); do pc(s[q]); while (--q); chkf(); } template <typename T> void wr(T x, char c) { wr(x), pc(c); } } using FastIO::rd; using FastIO::wr; #define fo(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout) #define fi first #define se second typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; int n, q, b[1100010], mn[2100010], a[1100010]; ll s[2100010], ans[2100010], tag[2100010]; void add(int p, int l, int r, int ll, int rr, long long k) { if (l > rr || r < ll) return; if (l >= ll && r <= rr) return ans[p] += k, tag[p] += k, void(); int mid = l + r >> 1; add(p << 1, l, mid, ll, rr, k), add(p << 1 | 1, mid + 1, r, ll, rr, k); ans[p] = min(ans[p << 1], ans[p << 1 | 1]) + tag[p]; } int build(int p, int l, int r) { if (l == r) return s[p] = mn[p] = b[l]; int mid = l + r >> 1; mn[p] = min(build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r)); return s[p] = s[p << 1] + s[p << 1 | 1] + mn[p], mn[p]; } ll qry(int p, int l, int r, int x) { if (l == r) return b[x]; int mid = l + r >> 1; if (x <= mid) return b[x] + s[p << 1 | 1] + qry(p << 1, l, mid, x); return b[x] + s[p << 1] + qry(p << 1 | 1, mid + 1, r, x); }
void upd(int p, int l, int r, int k, int x) { if (l == r) { add(1, 1, n, k, k, 1ll * (x - b[k]) * (__lg(n) + a[k] + 1)), mn[p] = s[p] = x; return; } int mid = l + r >> 1; if (k <= mid) { tag[p << 1 | 1] -= s[p << 1], ans[p << 1 | 1] -= s[p << 1]; upd(p << 1, l, mid, k, x); tag[p << 1 | 1] += s[p << 1], ans[p << 1 | 1] += s[p << 1]; } else { tag[p << 1] -= s[p << 1 | 1], ans[p << 1] -= s[p << 1 | 1]; upd(p << 1 | 1, mid + 1, r, k, x); tag[p << 1] += s[p << 1 | 1], ans[p << 1] += s[p << 1 | 1]; } mn[p] = min(mn[p << 1], mn[p << 1 | 1]); s[p] = s[p << 1] + s[p << 1 | 1] + mn[p]; ans[p] = min(ans[p << 1], ans[p << 1 | 1]) + tag[p]; }
int main() { rd(n); for (int i = 1; i <= n; i++) rd(a[i]), a[i]--; for (int i = 1; i <= n; i++) rd(b[i]); build(1, 1, n); for (int i = 1; i <= n; i++) add(1, 1, n, i, i, qry(1, 1, n, i) + 1ll * a[i] * b[i]); rd(q); int id, d; while (q--) { rd(id), rd(d), upd(1, 1, n, id, b[id] + d), b[id] += d; wr(ans[1], '\n'); } return 0; }
|