#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q;
ll a[200200], b[200200];
struct Seg1{
ll tree[800800];
void init(int i, int l, int r){
if (l==r){
tree[i] = a[l] / 2;
return;
}
int m = (l+r)>>1;
init(i<<1, l, m); init(i<<1|1, m+1, r);
tree[i] = tree[i<<1] + tree[i<<1|1];
}
void update(int i, int l, int r, int p, ll x){
if (r<p || p<l) return;
if (l==r){
tree[i] = x;
return;
}
int m = (l+r)>>1;
update(i<<1, l, m, p, x); update(i<<1|1, m+1, r, p, x);
tree[i] = tree[i<<1] + tree[i<<1|1];
}
int suf(int i, int l, int r, ll x){
if (tree[i] < x) return -1;
if (l==r) return l;
int m = (l+r)>>1;
int ret = suf(i<<1|1, m+1, r, x);
if (ret!=-1) return ret;
return suf(i<<1, l, m, x - tree[i<<1|1]);
}
ll query(int i, int l, int r, int s, int e){
if (r<s || e<l) return 0;
if (s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return query(i<<1, l, m, s, e) + query(i<<1|1, m+1, r, s, e);
}
}tree1;
struct Seg2{
ll tree[400400];
int sz;
void init(int n){
sz = n;
}
void update(int p, ll x){
p += sz;
tree[p] = x;
for (p>>=1;p;p>>=1) tree[p] = tree[p<<1] + tree[p<<1|1];
}
ll query(int l, int r){
++r;
ll ret = 0;
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret += tree[l++];
if (r&1) ret += tree[--r];
}
return ret;
}
}tree2, tree3;
struct Node{
ll prf, sum;
Node(){}
Node(ll _prf, ll _sum): prf(_prf), sum(_sum) {}
Node operator + (const Node &N) const{
return Node(min(prf, sum + N.prf), sum + N.sum);
}
};
struct Seg3{
Node tree[400400];
int sz;
void init(int n){
sz = n;
}
void update(int p, ll x){
p += sz;
tree[p] = Node(min(x, 0LL), x);
for (p>>=1;p;p>>=1) tree[p] = tree[p<<1] + tree[p<<1|1];
}
Node query(int l, int r){
++r;
Node retl(0, 0), retr(0, 0);
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) retl = retl + tree[l++];
if (r&1) retr = tree[--r] + retr;
}
return retl + retr;
}
}tree4;
int ok_naive(ll cnt){
for (int i=n;i;i--){
b[i] = min(a[i] / 2, cnt);
cnt -= b[i];
}
if (cnt > 0) return 0;
ll need = 0, avail = 0;
int pt = 0;
for (int i=1;i<=n;i++){
need += b[i];
for (;pt<min(n, i*2-1);pt++){
avail += a[pt+1] - b[pt+1] * 2;
}
if (need > avail) return 0;
}
return 1;
}
int ok(ll cnt){
if (tree1.tree[1] < cnt) return 0;
// printf(" ok cnt = %lld\n", cnt);
int pos = tree1.suf(1, 1, n, cnt);
assert(pos != -1);
ll sum = tree1.query(1, 1, n, pos+1, n);
ll need = cnt - sum;
ll avail = tree2.query(1, pos-1) + (a[pos] - need*2) + tree3.query(pos+1, min(n, pos*2-1));
assert(need*2 <= a[pos]);
// printf(" pos = %d, sum = %lld, need = %lld, avail = %lld\n", pos, sum, need, avail);
if (need > avail) return 0;
auto [prf, _] = tree4.query(pos+1, n);
if (avail - need + prf < 0) return 0;
return 1;
}
ll solve(){
ll l = 1, r = 1e14, ans = 0;
while(l<=r){
ll mid = (l+r)>>1;
if (ok(mid)) ans = mid, l = mid+1;
else r = mid-1;
}
return ans;
}
ll f(int x){
assert(x > 0);
ll ret = -(a[x]/2);
if (0 < x*2-2 && x*2-2 <= n) ret += a[x*2-2]&1;
if (0 < x*2-1 && x*2-1 <= n) ret += a[x*2-1]&1;
return ret;
}
int main(){
scanf("%d %d", &n, &q);
for (int i=1;i<=n;i++) scanf("%lld", a+i);
tree1.init(1, 1, n);
tree2.init(n+1);
tree3.init(n+1);
tree4.init(n+1);
for (int i=1;i<=n;i++){
tree2.update(i, a[i]);
tree3.update(i, a[i]&1);
tree4.update(i, f(i));
}
while(q--){
int x, y;
scanf("%d %d", &x, &y);
a[x] += y;
tree1.update(1, 1, n, x, a[x]/2);
tree2.update(x, a[x]);
tree3.update(x, a[x]&1);
tree4.update(x, f(x));
tree4.update(x/2+1, f(x/2+1));
printf("%lld\n", solve());
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:185:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
185 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:186:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
186 | for (int i=1;i<=n;i++) scanf("%lld", a+i);
| ~~~~~^~~~~~~~~~~~~
Main.cpp:201:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
11 ms |
468 KB |
Output is correct |
18 |
Correct |
11 ms |
516 KB |
Output is correct |
19 |
Correct |
11 ms |
516 KB |
Output is correct |
20 |
Correct |
12 ms |
520 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
10 ms |
528 KB |
Output is correct |
23 |
Correct |
6 ms |
468 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
10 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
86 ms |
604 KB |
Output is correct |
4 |
Correct |
672 ms |
19648 KB |
Output is correct |
5 |
Correct |
588 ms |
19916 KB |
Output is correct |
6 |
Correct |
664 ms |
19700 KB |
Output is correct |
7 |
Correct |
721 ms |
19792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
484 KB |
Output is correct |
2 |
Correct |
10 ms |
532 KB |
Output is correct |
3 |
Correct |
11 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
520 KB |
Output is correct |
5 |
Correct |
1018 ms |
20048 KB |
Output is correct |
6 |
Correct |
1898 ms |
21540 KB |
Output is correct |
7 |
Correct |
1909 ms |
21596 KB |
Output is correct |
8 |
Correct |
2048 ms |
21572 KB |
Output is correct |
9 |
Correct |
2031 ms |
21616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
11 ms |
468 KB |
Output is correct |
18 |
Correct |
11 ms |
516 KB |
Output is correct |
19 |
Correct |
11 ms |
516 KB |
Output is correct |
20 |
Correct |
12 ms |
520 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
10 ms |
528 KB |
Output is correct |
23 |
Correct |
6 ms |
468 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
10 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
31 |
Correct |
86 ms |
604 KB |
Output is correct |
32 |
Correct |
672 ms |
19648 KB |
Output is correct |
33 |
Correct |
588 ms |
19916 KB |
Output is correct |
34 |
Correct |
664 ms |
19700 KB |
Output is correct |
35 |
Correct |
721 ms |
19792 KB |
Output is correct |
36 |
Correct |
3 ms |
484 KB |
Output is correct |
37 |
Correct |
10 ms |
532 KB |
Output is correct |
38 |
Correct |
11 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
520 KB |
Output is correct |
40 |
Correct |
1018 ms |
20048 KB |
Output is correct |
41 |
Correct |
1898 ms |
21540 KB |
Output is correct |
42 |
Correct |
1909 ms |
21596 KB |
Output is correct |
43 |
Correct |
2048 ms |
21572 KB |
Output is correct |
44 |
Correct |
2031 ms |
21616 KB |
Output is correct |
45 |
Correct |
2043 ms |
21560 KB |
Output is correct |
46 |
Correct |
1970 ms |
21576 KB |
Output is correct |
47 |
Correct |
1998 ms |
21536 KB |
Output is correct |
48 |
Correct |
1095 ms |
19892 KB |
Output is correct |
49 |
Correct |
1952 ms |
21588 KB |
Output is correct |
50 |
Correct |
1167 ms |
20320 KB |
Output is correct |
51 |
Correct |
689 ms |
20300 KB |
Output is correct |
52 |
Correct |
1957 ms |
21812 KB |
Output is correct |
53 |
Correct |
353 ms |
19820 KB |
Output is correct |
54 |
Correct |
807 ms |
20100 KB |
Output is correct |