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
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int N = 1E5 + 100; struct P { LL dep, idx; }; bool operator < (const P& x, const P& y) { return x.dep < y.dep || (x.dep == y.dep && x.idx < y.idx); } priority_queue<P> S[N]; set<int> deleted; int n; LL a[N], Sum, ps[N];
inline pair<LL, LL> f(int x) { return {S[x].top().dep + ps[x], S[x].top().idx}; };
namespace seg { #define mid ((l + r) >> 1) #define lson o * 2, l, mid #define rson o * 2 + 1, mid + 1, r int mx[N << 2]; inline int Max(int a, int b) { if (a == -1) return b; if (b == -1) return a; return f(a) < f(b) ? b : a; } inline void up(int o) { mx[o] = Max(mx[o * 2], mx[o * 2 + 1]); } void build(int o, int l, int r) { if (l == r) mx[o] = l; else { build(lson); build(rson); up(o); } } void update(int k, int o, int l, int r) { if (k > r || k < l || l == r) return; update(k, lson); update(k, rson); up(o); } int query(int ql, int qr, int o, int l, int r) { if (qr < l || r < ql) return -1; if (ql <= l && r <= qr) return mx[o]; return Max(query(ql, qr, lson), query(ql, qr, rson)); }
} LL ans, clk;
void add(int x, int w, int sp) { S[x].push({sp * S[x].top().dep + w, clk}); seg::update(x, 1, 1, n); } void del(int x) { S[x].pop(); if (S[x].empty()) { deleted.insert(x); deleted.insert(x + n); } else seg::update(x, 1, 1, n); } int query(int x) { auto it = deleted.upper_bound(x); int ed = it == deleted.end() ? x + n - 1 : *it - 1; int t1 = seg::query(x, min(n, ed), 1, 1, n); int t2 = ed <= n ? -1 : seg::query(1, ed - n, 1, 1, n); auto v1 = f(t1), v2 = f(t2);
v1.first -= ps[x]; ans = v1.first; if (t2 == -1) return t1;
v2.first += Sum - ps[x]; ans = max(v1.first, v2.first);
return v1 > v2 ? t1 : t2; }
int main() { #ifdef zerol freopen("in", "r", stdin); #endif cin >> n; FOR (i, 1, n + 1) scanf("%lld", &a[i]); Sum = accumulate(a + 1, a + n + 1, 0LL); FOR (i, 1, n + 1) ps[i] = ps[i - 1] + a[i - 1]; FOR (i, 1, n + 1) S[i].push({0, 0}); seg::build(1, 1, n); int Qn; cin >> Qn; for (clk = 1; clk <= Qn; ++clk) { int tp, x; scanf("%d%d", &tp, &x); if (tp <= 2) { LL w; scanf("%lld", &w); if (tp == 1) add(query(x), w, 1); else add(x, w, 0); } else { if (tp == 3) del(query(x)); else { query(x); printf("%lld\n", ans); } } } }
|