#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int n, m, z; void brute() { vector<int> a(n); REP(i, n) cin >> a[i]; vector<LL> v(n); REP(i, m + z) { int type; cin >> type; if (type == 1) { int p, w; cin >> p >> w; REP(j, p) { v[j] += w; } } else if (type == 2) { int p, d; cin >> p >> d; LL answer = 0; REP(j, p) { answer += (a[j] >= d) * v[j]; } cout << answer << '\n'; } else { assert(false); } } } struct WaveletTree { using vi = vector<int>; int lo, hi; WaveletTree *l, *r; vi b; //nos are in range [x,y] //array indices are [from, to) WaveletTree(int *from, int *to, int x, int y){ lo = x, hi = y; if(lo == hi or from >= to) return; int mid = (lo+hi)/2; auto f = [mid](int x){ return x <= mid; }; b.reserve(to-from+1); b.emplace_back(0); for(auto it = from; it != to; it++) b.emplace_back(b.back() + f(*it)); //see how lambda function is used here auto pivot = stable_partition(from, to, f); l = new WaveletTree(from, pivot, lo, mid); r = new WaveletTree(pivot, to, mid+1, hi); } //kth smallest element in [l, r] int kth(int l, int r, int k){ if(l > r) return 0; if(lo == hi) return lo; int inLeft = b[r] - b[l-1]; int lb = b[l-1]; //amt of nos in first (l-1) nos that go in left int rb = b[r]; //amt of nos in first (r) nos that go in left if(k <= inLeft) return this->l->kth(lb+1, rb , k); return this->r->kth(l-lb, r-rb, k-inLeft); } //count of nos in [l, r] Less than or equal to k int LTE(int l, int r, int k) { if(l > r or k < lo) return 0; if(hi <= k) return r - l + 1; int lb = b[l-1], rb = b[r]; return this->l->LTE(lb+1, rb, k) + this->r->LTE(l-lb, r-rb, k); } //count of nos in [l, r] equal to k int count(int l, int r, int k) { if(l > r or k < lo or k > hi) return 0; if(lo == hi) return r - l + 1; int lb = b[l-1], rb = b[r], mid = (lo+hi)/2; if(k <= mid) return this->l->count(lb+1, rb, k); return this->r->count(l-lb, r-rb, k); } }; void small_mz() { vector<int> a(n); REP(i, n) cin >> a[i]; WaveletTree tree(a.data(), a.data() + n, *min_element(a.begin(), a.end()), *max_element(a.begin(), a.end())); vector<pair<int, LL>> ms; ms.reserve(m); REP(i, m + z) { int type; cin >> type; if (type == 1) { int p, w; cin >> p >> w; ms.emplace_back(p, w); } else if (type == 2) { int p, d; cin >> p >> d; LL answer = 0; for (auto [other_p, w] : ms) { const int current_p = min(p, other_p); LL cnt = current_p - tree.LTE(1, current_p, d - 1); debug(i, type, p, d, other_p, w, current_p, cnt); answer += w * cnt; } cout << answer << '\n'; } else { assert(false); } } } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> z; const LL subtask_2 = 1e7; if (LL(m) * z <= subtask_2) { small_mz(); } else { brute(); } }
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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int n, m, z; void brute() { vector<int> a(n); REP(i, n) cin >> a[i]; vector<LL> v(n); REP(i, m + z) { int type; cin >> type; if (type == 1) { int p, w; cin >> p >> w; REP(j, p) { v[j] += w; } } else if (type == 2) { int p, d; cin >> p >> d; LL answer = 0; REP(j, p) { answer += (a[j] >= d) * v[j]; } cout << answer << '\n'; } else { assert(false); } } } struct WaveletTree { using vi = vector<int>; int lo, hi; WaveletTree *l, *r; vi b; //nos are in range [x,y] //array indices are [from, to) WaveletTree(int *from, int *to, int x, int y){ lo = x, hi = y; if(lo == hi or from >= to) return; int mid = (lo+hi)/2; auto f = [mid](int x){ return x <= mid; }; b.reserve(to-from+1); b.emplace_back(0); for(auto it = from; it != to; it++) b.emplace_back(b.back() + f(*it)); //see how lambda function is used here auto pivot = stable_partition(from, to, f); l = new WaveletTree(from, pivot, lo, mid); r = new WaveletTree(pivot, to, mid+1, hi); } //kth smallest element in [l, r] int kth(int l, int r, int k){ if(l > r) return 0; if(lo == hi) return lo; int inLeft = b[r] - b[l-1]; int lb = b[l-1]; //amt of nos in first (l-1) nos that go in left int rb = b[r]; //amt of nos in first (r) nos that go in left if(k <= inLeft) return this->l->kth(lb+1, rb , k); return this->r->kth(l-lb, r-rb, k-inLeft); } //count of nos in [l, r] Less than or equal to k int LTE(int l, int r, int k) { if(l > r or k < lo) return 0; if(hi <= k) return r - l + 1; int lb = b[l-1], rb = b[r]; return this->l->LTE(lb+1, rb, k) + this->r->LTE(l-lb, r-rb, k); } //count of nos in [l, r] equal to k int count(int l, int r, int k) { if(l > r or k < lo or k > hi) return 0; if(lo == hi) return r - l + 1; int lb = b[l-1], rb = b[r], mid = (lo+hi)/2; if(k <= mid) return this->l->count(lb+1, rb, k); return this->r->count(l-lb, r-rb, k); } }; void small_mz() { vector<int> a(n); REP(i, n) cin >> a[i]; WaveletTree tree(a.data(), a.data() + n, *min_element(a.begin(), a.end()), *max_element(a.begin(), a.end())); vector<pair<int, LL>> ms; ms.reserve(m); REP(i, m + z) { int type; cin >> type; if (type == 1) { int p, w; cin >> p >> w; ms.emplace_back(p, w); } else if (type == 2) { int p, d; cin >> p >> d; LL answer = 0; for (auto [other_p, w] : ms) { const int current_p = min(p, other_p); LL cnt = current_p - tree.LTE(1, current_p, d - 1); debug(i, type, p, d, other_p, w, current_p, cnt); answer += w * cnt; } cout << answer << '\n'; } else { assert(false); } } } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> z; const LL subtask_2 = 1e7; if (LL(m) * z <= subtask_2) { small_mz(); } else { brute(); } } |