이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 200010;
struct node {
int upd = 0, ans = 0;
} a[4*MAXN];
void update(int l, int r, int constl, int constr, int idx, int val) {
if(l<=constl && constr<=r) {
a[idx].upd += val;
a[idx].ans += val;
return;
}
int mid = (constl+constr)>>1;
if(mid<l || r<constl) update(l, r, mid+1, constr, 2*idx+2, val);
else if(constr<l || r<mid+1) update(l, r, constl, mid, 2*idx+1, val);
else {
update(l, r, constl, mid, 2*idx+1, val);
update(l, r, mid+1, constr, 2*idx+2, val);
}
a[idx].ans = a[idx].upd + min(a[2*idx+1].ans, a[2*idx+2].ans);
}
int query(int l, int r, int constl, int constr, int idx) {
if(l<=constl && constr<=r) return a[idx].ans;
int mid = (constl+constr)>>1;
if(mid<l || r<constl) return a[idx].upd + query(l, r, mid+1, constr, 2*idx+2);
else if(constr<l || r<mid+1) return a[idx].upd + query(l, r, constl, mid, 2*idx+1);
else {
return a[idx].upd + min(query(l, r, constl, mid, 2*idx+1), query(l, r, mid+1, constr, 2*idx+2));
}
}
int main() {
int N, Q, S, T; cin >> N >> Q >> S >> T;
int cnt0 = 0, cnt1 = 0;
for(int i=0; i<=N; i++) {
int ono;
cin >> ono;
update(i, i, 0, MAXN-1, 0, ono);
}
for(int i=1; i<=N; i++) {
if(query(i-1, i-1, 0, MAXN-1, 0) < query(i, i, 0, MAXN-1, 0)) cnt0 += query(i, i, 0, MAXN-1, 0) - query(i-1, i-1, 0, MAXN-1, 0);
else cnt1 += query(i-1, i-1, 0, MAXN-1, 0) - query(i, i, 0, MAXN-1, 0);
}
while(Q--) {
int l, r, x; cin >> l >> r >> x;
if(query(l-1, l-1, 0, MAXN-1, 0) < query(l, l, 0, MAXN-1, 0)) cnt0 -= query(l, l, 0, MAXN-1, 0) - query(l-1, l-1, 0, MAXN-1, 0);
else cnt1 -= query(l-1, l-1, 0, MAXN-1, 0) - query(l, l, 0, MAXN-1, 0);
if(r<N) {
if(query(r, r, 0, MAXN-1, 0) < query(r+1, r+1, 0, MAXN-1, 0)) cnt0 -= query(r+1, r+1, 0, MAXN-1, 0) - query(r, r, 0, MAXN-1, 0);
else cnt1 -= query(r, r, 0, MAXN-1, 0) - query(r+1, r+1, 0, MAXN-1, 0);
}
update(l, r, 0, MAXN-1, 0, x);
if(query(l-1, l-1, 0, MAXN-1, 0) < query(l, l, 0, MAXN-1, 0)) cnt0 += query(l, l, 0, MAXN-1, 0) - query(l-1, l-1, 0, MAXN-1, 0);
else cnt1 += query(l-1, l-1, 0, MAXN-1, 0) - query(l, l, 0, MAXN-1, 0);
if(r<N) {
if(query(r, r, 0, MAXN-1, 0) < query(r+1, r+1, 0, MAXN-1, 0)) cnt0 += query(r+1, r+1, 0, MAXN-1, 0) - query(r, r, 0, MAXN-1, 0);
else cnt1 += query(r, r, 0, MAXN-1, 0) - query(r+1, r+1, 0, MAXN-1, 0);
}
cout << -cnt0 * S + cnt1 * T << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |