# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1124599 | Zero_OP | Weirdtree (RMI21_weirdtree) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h> //sub 3
#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".ans", "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 = 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];
}
void cut(int l, int r, int k){
long long s = 0;
for(int i = l; i <= r; ++i) s += H[i];
if(s <= k){
for(int i = l; i <= r; ++i) H[i] = 0;
return;
}
int lo = 0, hi = 1e9, cut = -1;
while(lo <= hi){
int mid = lo + hi >> 1;
int cur = 0;
for(int i = l; i <= r; ++i){
cur += max(0, H[i] - mid);
if(cur >= k){
break;
}
}
if(cur >= k) cut = mid, lo = mid + 1;
else hi = mid - 1;
}
int res = 0;
for(int i = l; i <= r; ++i){
res += max(0, H[i] - cut);
}
int keep = res - k;
for(int i = r; i >= l; --i){
if(H[i] > cut){
H[i] = cut;
if(keep) ++H[i], --keep;
}
}
}
void magic(int i, int x){
H[i] = x;
}
long long inspect(int l, int r){
long long s = 0;
for(int i = l; i <= r; ++i) s += H[i];
return s;
}
#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;
cout << inspect(l, r) << '\n';
}
}
return 0;
}
#endif LOCAL