#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
o << "{";
for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif
struct BitSum {
struct E {
uint64_t b;
uint32_t s;
};
vector<E> v;
BitSum() = default;
BitSum(const vector<bool>& a) {
int n = ssize(a);
v.resize((n >> 6) + 1);
for (int i = 0; i < n; i++) {
v[i >> 6].b |= uint64_t(a[i]) << (i & 63);
}
for (int i = 1; i < (n >> 6) + 1; i++) {
v[i].s = v[i - 1].s + __builtin_popcountll(v[i - 1].b);
}
}
int get(int i) {
return v[i >> 6].s + __builtin_popcountll(v[i >> 6].b & ((uint64_t(1) << (i & 63)) - 1));
}
};
const int N = 1000100;
ll war[N], dod[N];
int T = 0;
struct Node {
int lo, hi;
BitSum s;
vi v;
vector<ll> ps; int t = 0;
Node *l = 0, *r = 0;
Node(auto st, auto ed, auto sst, auto a) : v(st, ed) {
int n = ed - st;
lo = sst[0];
hi = sst[n - 1] + 1;
ps = vector<ll>(n + 1);
if (lo + 1 < hi) {
int mid = sst[n / 2];
if (mid == sst[0]) mid = *upper_bound(sst, sst + n, mid);
vector<bool> b(n);
for (int i = 0; i < n; i++) {
b[i] = a[st[i]] < mid;
}
s = BitSum(b);
auto k = stable_partition(st, ed, [&](int x) {
return a[x] < mid;
});
auto sm = lower_bound(sst, sst + n, mid);
if (k != st) l = new Node(st, k, sst, a);
if (k != ed) r = new Node(k, ed, sm, a);
}
}
int count(int p, int k) {
if (hi <= k) return 0;
if (lo >= k) return p;
int x = s.get(p);
return (l ? l->count(x, k) : 0) + (r ? r->count(p - x, k) : 0);
}
ll suma(int p, int k) {
if (hi <= k) return 0;
if (lo >= k) {
if (t < T) {
rep(i, 0, sz(v)) ps[i + 1] = ps[i] + war[v[i]];
t = T;
}
return ps[p];
}
int x = s.get(p);
return (l ? l->suma(x, k) : 0) + (r ? r->suma(p - x, k) : 0);
}
};
int B;
int n, m, z;
Node* k = 0;
void zbuduj() {
vi a(n);
rep(i, 0, n) cin >> a[i];
vi sa(all(a)); sort(all(sa));
vi b(n); iota(all(b), 0);
k = new Node(all(b), sa.begin(), a.begin());
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> z;
zbuduj();
//B = llround(1ll * n * m / sqrt(1ll * n * m * z)) + 1;
double lg = n > 1 ? log(n) / log(2) : 1;
B = llround(1ll * n * m / sqrt(1ll * n * m * z * lg)) + 1;
//B = 45;
//B = max(1ll, llround(1ll * n * m / sqrt(1ll * n * m * z * log(n) / log(2))) - 100);
//B = 1;//20;
vector<pii> q;
rep(i, 0, m + z) {
int t;
cin >> t;
if (t == 1) {
int p, w;
cin >> p >> w;
q.push_back({p, w});
if (sz(q) == B) {
// rebuild
rep(j, 0, n) dod[j] = 0;
for (auto [x, y] : q) dod[x - 1] += y;
for (int j = n - 1; j >= 0; j--) {
dod[j] += dod[j + 1];
war[j] += dod[j];
}
T++;
//napraw(k);
q.clear();
}
} else {
int p, d;
cin >> p >> d;
ll odp = k->suma(p, d);
for (auto [x, y] : q) odp += 1ll * y * k->count(min(x, p), d);
cout << odp << '\n';
}
}
}
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 | #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif struct BitSum { struct E { uint64_t b; uint32_t s; }; vector<E> v; BitSum() = default; BitSum(const vector<bool>& a) { int n = ssize(a); v.resize((n >> 6) + 1); for (int i = 0; i < n; i++) { v[i >> 6].b |= uint64_t(a[i]) << (i & 63); } for (int i = 1; i < (n >> 6) + 1; i++) { v[i].s = v[i - 1].s + __builtin_popcountll(v[i - 1].b); } } int get(int i) { return v[i >> 6].s + __builtin_popcountll(v[i >> 6].b & ((uint64_t(1) << (i & 63)) - 1)); } }; const int N = 1000100; ll war[N], dod[N]; int T = 0; struct Node { int lo, hi; BitSum s; vi v; vector<ll> ps; int t = 0; Node *l = 0, *r = 0; Node(auto st, auto ed, auto sst, auto a) : v(st, ed) { int n = ed - st; lo = sst[0]; hi = sst[n - 1] + 1; ps = vector<ll>(n + 1); if (lo + 1 < hi) { int mid = sst[n / 2]; if (mid == sst[0]) mid = *upper_bound(sst, sst + n, mid); vector<bool> b(n); for (int i = 0; i < n; i++) { b[i] = a[st[i]] < mid; } s = BitSum(b); auto k = stable_partition(st, ed, [&](int x) { return a[x] < mid; }); auto sm = lower_bound(sst, sst + n, mid); if (k != st) l = new Node(st, k, sst, a); if (k != ed) r = new Node(k, ed, sm, a); } } int count(int p, int k) { if (hi <= k) return 0; if (lo >= k) return p; int x = s.get(p); return (l ? l->count(x, k) : 0) + (r ? r->count(p - x, k) : 0); } ll suma(int p, int k) { if (hi <= k) return 0; if (lo >= k) { if (t < T) { rep(i, 0, sz(v)) ps[i + 1] = ps[i] + war[v[i]]; t = T; } return ps[p]; } int x = s.get(p); return (l ? l->suma(x, k) : 0) + (r ? r->suma(p - x, k) : 0); } }; int B; int n, m, z; Node* k = 0; void zbuduj() { vi a(n); rep(i, 0, n) cin >> a[i]; vi sa(all(a)); sort(all(sa)); vi b(n); iota(all(b), 0); k = new Node(all(b), sa.begin(), a.begin()); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> z; zbuduj(); //B = llround(1ll * n * m / sqrt(1ll * n * m * z)) + 1; double lg = n > 1 ? log(n) / log(2) : 1; B = llround(1ll * n * m / sqrt(1ll * n * m * z * lg)) + 1; //B = 45; //B = max(1ll, llround(1ll * n * m / sqrt(1ll * n * m * z * log(n) / log(2))) - 100); //B = 1;//20; vector<pii> q; rep(i, 0, m + z) { int t; cin >> t; if (t == 1) { int p, w; cin >> p >> w; q.push_back({p, w}); if (sz(q) == B) { // rebuild rep(j, 0, n) dod[j] = 0; for (auto [x, y] : q) dod[x - 1] += y; for (int j = n - 1; j >= 0; j--) { dod[j] += dod[j + 1]; war[j] += dod[j]; } T++; //napraw(k); q.clear(); } } else { int p, d; cin >> p >> d; ll odp = k->suma(p, d); for (auto [x, y] : q) odp += 1ll * y * k->count(min(x, p), d); cout << odp << '\n'; } } } |
English