Skip to content

Commit 2dae7ad

Browse files
authored
[20251001] BOJ / P5 / 지폐가 넘쳐흘러 / 권혁준
1 parent 64b9d01 commit 2dae7ad

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
using ll = long long;
5+
6+
int N, Q, LIMIT;
7+
ll w[262144]{}, l[524288]{}, r[524288]{}, x[524288]{};
8+
9+
void init(int n) {
10+
if(n >= LIMIT) return;
11+
init(n*2); init(n*2+1);
12+
l[n] = w[n] + max(l[n*2], r[n*2]);
13+
r[n] = w[n] + max(l[n*2+1], r[n*2+1]);
14+
x[n] = max({x[n*2], x[n*2+1], l[n] + r[n] - w[n]});
15+
}
16+
17+
int main(){
18+
cin.tie(0)->sync_with_stdio(0);
19+
20+
cin>>N;
21+
for(int i=1;i<=N;i++) cin>>w[i];
22+
LIMIT = N+1;
23+
init(1);
24+
cout<<x[1]<<'\n';
25+
for(cin>>Q;Q--;) {
26+
int i,d;
27+
cin>>i>>d;
28+
for(w[i]=d;i;i>>=1) {
29+
l[i] = w[i] + max(l[i*2], r[i*2]);
30+
r[i] = w[i] + max(l[i*2+1], r[i*2+1]);
31+
x[i] = max({x[i*2], x[i*2+1], l[i] + r[i] - w[i]});
32+
}
33+
cout<<x[1]<<'\n';
34+
}
35+
36+
}
37+
```

0 commit comments

Comments
 (0)