#pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
// #include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
/*
for a block of them.
just do it with O(A) memory?
offline it.
*/
const int A = 1e6+1;
int where[A];
struct block {
vector<ll> c, cp;
ll lazy=0;
vi cnt;
int l,r;
vi ord;
vi iord;
block(const vi& a, int l, int r) : l(l),r(r) {
int n = a.size();
c.assign(n,0);
cp.assign(n,0);
ord.resize(n);
iota(all(ord),0);
sort(all(ord),[&](int i, int j) {return a[i]<a[j];});
iord.resize(n);
for(int i=0;i<n;++i) {
iord[ord[i]]=i;
}
int last=-1;
fill(where,where+A,oo);
for(int i : ord) {
int place = iord[i];
for(int j=last+1;j<=a[i];++j) {
where[j]=place;
}
last=a[i];
}
}
void update(int x, ll w) {
if(x<=l) {
return; // nope
}
if(x>=r) {
// easy case
lazy+=w;
return;
}
for(int i=l;i<x;++i) {
c[iord[i-l]]+=w;
}
// rebuild block
partial_sum(c.rbegin(),c.rend(),cp.rbegin());
}
ll query(int x, int y) {
if(x<=l) return 0LL;
int nxt = where[y];
if(nxt==oo) return 0;
if(x>=r) {
return cp[nxt] + (r-l - nxt)*lazy;
}
ll ans=0;
for(int i=l;i<x;++i) {
int j = iord[i-l];
if(j>=nxt) ans+=c[j]+lazy;
}
return ans;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,z;
cin >> n >> m >> z;
vi a(n);
for(auto& i : a) cin >> i;
vector<array<int,3>> qs(m+z);
for(auto& [t,a,b] : qs) {
cin >> t >> a >> b;
}
const int B= sqrt(n*2);
int l=0;
vector<ll> ans(z);
while(l<n) {
int r = min(n,l+B);
block bl(vi{a.begin()+l,a.begin()+r},l,r);
int Q=0;
for(auto& [t,a,b] : qs) {
if(t==1) {
// modification
bl.update(a,b);
} else {
ans[Q++]+=bl.query(a,b);
}
}
l+=B;
}
// boom
for(auto x : ans) cout << x << '\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 | #pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL // #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; /* for a block of them. just do it with O(A) memory? offline it. */ const int A = 1e6+1; int where[A]; struct block { vector<ll> c, cp; ll lazy=0; vi cnt; int l,r; vi ord; vi iord; block(const vi& a, int l, int r) : l(l),r(r) { int n = a.size(); c.assign(n,0); cp.assign(n,0); ord.resize(n); iota(all(ord),0); sort(all(ord),[&](int i, int j) {return a[i]<a[j];}); iord.resize(n); for(int i=0;i<n;++i) { iord[ord[i]]=i; } int last=-1; fill(where,where+A,oo); for(int i : ord) { int place = iord[i]; for(int j=last+1;j<=a[i];++j) { where[j]=place; } last=a[i]; } } void update(int x, ll w) { if(x<=l) { return; // nope } if(x>=r) { // easy case lazy+=w; return; } for(int i=l;i<x;++i) { c[iord[i-l]]+=w; } // rebuild block partial_sum(c.rbegin(),c.rend(),cp.rbegin()); } ll query(int x, int y) { if(x<=l) return 0LL; int nxt = where[y]; if(nxt==oo) return 0; if(x>=r) { return cp[nxt] + (r-l - nxt)*lazy; } ll ans=0; for(int i=l;i<x;++i) { int j = iord[i-l]; if(j>=nxt) ans+=c[j]+lazy; } return ans; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m,z; cin >> n >> m >> z; vi a(n); for(auto& i : a) cin >> i; vector<array<int,3>> qs(m+z); for(auto& [t,a,b] : qs) { cin >> t >> a >> b; } const int B= sqrt(n*2); int l=0; vector<ll> ans(z); while(l<n) { int r = min(n,l+B); block bl(vi{a.begin()+l,a.begin()+r},l,r); int Q=0; for(auto& [t,a,b] : qs) { if(t==1) { // modification bl.update(a,b); } else { ans[Q++]+=bl.query(a,b); } } l+=B; } // boom for(auto x : ans) cout << x << '\n'; } |
English