#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back mt19937 rng(2137); ll MLN = 1000'000; ll n, m, z; vc<ll> a; // time: nz+m // memory: n void sol1() { vc<ll> p(n, 0); for (ll q = 0; q < m + z; q++) { ll type; cin >> type; if (type == 1) { ll i, x; cin >> i >> x; i--; p[i] += x; } else { ll i, x; cin >> i >> x; i--; ll ans = 0, sum = 0; for (ll j = n - 1; j >= 0; j--) { sum += p[j]; if (a[j] >= x and j <= i) ans += sum; } cout << ans << "\n"; } } } // time = mzlogn struct Fenwick { vc<ll> t; ll n; Fenwick(ll _n) { n = _n; t.assign(n, 0); } void add(ll i, ll x) { for ( ; i < n; i = i | (i + 1)) t[i] += x; } ll sum(ll i) { ll ret = 0; for ( ; i >= 0; i = (i & (i + 1)) - 1) ret += t[i]; return ret; } }; struct Que { ll i, j, k, x; }; void sol2() { vc<ll> res(z, 0); vc<pll> mds; vc<Que> ques; ll j = 0; for (ll q = 0; q < m + z; q++) { ll type; cin >> type; if (type == 1) { ll i, x; cin >> i >> x; i--; mds.pub({i, x}); } else { ll i, x; cin >> i >> x; i--; for (auto &md : mds) { ques.pub({j, min(md.st, i), x, md.nd}); } j++; } } for (ll i = 0; i < n; i++) ques.pub({-1, i, a[i], 0}); sort(all(ques), [&](auto &p, auto &q) { if (p.k != q.k) return p.k > q.k; return p.i < q.i; }); Fenwick tree(n); for (auto &que : ques) { if (que.i == -1) { tree.add(que.j, 1); } else { res[que.i] += tree.sum(que.j) * que.x; } } for (ll i = 0; i < z; i++) cout << res[i] << "\n"; } void program() { cin >> n >> m >> z; a.resize(n); for (ll &ai : a) cin >> ai; if (n * z <= 10'000'069) { sol1(); return; } if (m * z <= 10'000'069) { sol2(); return; } ll A = n * z; ll B = m * z * __lg(m * z); if (A <= B) { sol1(); } else { sol2(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); program(); return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back mt19937 rng(2137); ll MLN = 1000'000; ll n, m, z; vc<ll> a; // time: nz+m // memory: n void sol1() { vc<ll> p(n, 0); for (ll q = 0; q < m + z; q++) { ll type; cin >> type; if (type == 1) { ll i, x; cin >> i >> x; i--; p[i] += x; } else { ll i, x; cin >> i >> x; i--; ll ans = 0, sum = 0; for (ll j = n - 1; j >= 0; j--) { sum += p[j]; if (a[j] >= x and j <= i) ans += sum; } cout << ans << "\n"; } } } // time = mzlogn struct Fenwick { vc<ll> t; ll n; Fenwick(ll _n) { n = _n; t.assign(n, 0); } void add(ll i, ll x) { for ( ; i < n; i = i | (i + 1)) t[i] += x; } ll sum(ll i) { ll ret = 0; for ( ; i >= 0; i = (i & (i + 1)) - 1) ret += t[i]; return ret; } }; struct Que { ll i, j, k, x; }; void sol2() { vc<ll> res(z, 0); vc<pll> mds; vc<Que> ques; ll j = 0; for (ll q = 0; q < m + z; q++) { ll type; cin >> type; if (type == 1) { ll i, x; cin >> i >> x; i--; mds.pub({i, x}); } else { ll i, x; cin >> i >> x; i--; for (auto &md : mds) { ques.pub({j, min(md.st, i), x, md.nd}); } j++; } } for (ll i = 0; i < n; i++) ques.pub({-1, i, a[i], 0}); sort(all(ques), [&](auto &p, auto &q) { if (p.k != q.k) return p.k > q.k; return p.i < q.i; }); Fenwick tree(n); for (auto &que : ques) { if (que.i == -1) { tree.add(que.j, 1); } else { res[que.i] += tree.sum(que.j) * que.x; } } for (ll i = 0; i < z; i++) cout << res[i] << "\n"; } void program() { cin >> n >> m >> z; a.resize(n); for (ll &ai : a) cin >> ai; if (n * z <= 10'000'069) { sol1(); return; } if (m * z <= 10'000'069) { sol2(); return; } ll A = n * z; ll B = m * z * __lg(m * z); if (A <= B) { sol1(); } else { sol2(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); program(); return 0; } |