#include <bits/stdc++.h>
#ifndef LOCAL
#include "weirdtree.h"
#endif // LOCAL
using namespace std;
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
}
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
}
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;
const int MAX = 3e5 + 5;
int N, Q, H[MAX];
namespace subtask12{
pair<int, int> st[MAX * 4];
long long sum[MAX * 4];
void build(int id, int l, int r){
if(l == r){
st[id] = {H[l], -l};
sum[id] = H[l];
} else{
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = max(st[id << 1], st[id << 1 | 1]);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
pair<int, int> query(int id, int l, int r, int u, int v){
if(u <= l && r <= v) return st[id];
int mid = l + r >> 1;
if(u > mid) return query(id << 1 | 1, mid + 1, r, u, v);
if(mid >= v) return query(id << 1, l, mid, u, v);
return max(query(id << 1, l, mid, u, v), query(id << 1 | 1, mid + 1, r, u, v));
}
void update(int id, int l, int r, int pos, int val){
if(l == r) st[id].first = val, sum[id] = val;
else{
int mid = l + r >> 1;
if(pos <= mid) update(id << 1, l, mid, pos, val);
else update(id << 1 | 1, mid + 1, r, pos, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
long long query_sum(int id, int l, int r, int u, int v){
if(v < l || u > r) return 0;
if(u <= l && r <= v) return sum[id];
int mid = l + r >> 1;
return query_sum(id << 1, l, mid, u, v) + query_sum(id << 1 | 1, mid + 1, r, u, v);
}
}
void initialise(int _N, int _Q, int _H[]){
N = _N;
Q = _Q;
for(int i = 1; i <= N; ++i) H[i] = _H[i];
subtask12::build(1, 1, N);
}
void cut(int l, int r, int k){
// cout << "cut\n";
while(k--){
pair<int, int> cur = subtask12::query(1, 1, N, l, r);
if(cur.first == 0) return;
--cur.first;
subtask12::update(1, 1, N, -cur.second, cur.first);
}
}
void magic(int i, int x){
// cout << "magic\n";
subtask12::update(1, 1, N, i, x);
}
long long inspect(int l, int r){
// cout << "inspect\n";
return subtask12::query_sum(1, 1, N, l, r);
}
#ifdef LOCAL
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
file("C");
int N, Q;
cin >> N >> Q;
int H[N + 1];
for(int i = 1; i <= N; ++i) cin >> H[i];
initialise(N, Q, H);
while(Q--){
int t;
cin >> t;
if(t == 1){
int l, r, k;
cin >> l >> r >> k;
cut(l, r, k);
} else if(t == 2){
int i, x;
cin >> i >> x;
magic(i, x);
} else{
int l, r;
cin >> l >> r;
// inspect(l, r);
cout << inspect(l, r) << '\n';
}
}
return 0;
}
#endif LOCAL
Compilation message (stderr)
weirdtree.cpp:151:8: warning: extra tokens at end of #endif directive [-Wendif-labels]
151 | #endif LOCAL
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |