// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int maxn=200005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=20;
const int maxa=1000000;
const int root=3;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
};
const int iroot=power(3,mod-2);
const int base=101;
struct node{
int Min=0,sum=0;
node(int cur=0,int val=0){
sum=val;
Min=min(cur,0LL);
}
friend node operator+(node a,node b){
return node(min(a.Min,a.sum+b.Min),a.sum+b.sum);
};
};
struct Segtree{
node tree[8*maxn];
void update(int l,int r,int id,int p,int val){
if(l==r){
tree[id]=node(val,val);
return;
}
int mid=(l+r)>>1;
if(p<=mid) update(l,mid,id<<1,p,val);
else update(mid+1,r,id<<1|1,p,val);
tree[id]=tree[id<<1]+tree[id<<1|1];
}
node query(int l,int r,int id,int tl,int tr){
if(tr<l || r<tl) return node();
if(tl<=l && r<=tr) return tree[id];
int mid=(l+r)>>1;
return query(l,mid,id<<1,tl,tr)+query(mid+1,r,id<<1|1,tl,tr);
}
}ST[3];
int n,q,a[maxn],aa[maxn];
namespace BIT{
int bit[maxn];
void update(int x,int val){
x=n-x+1;
for(int i=x;i<=n;i+=(i&(-i))) bit[i]+=val;
}
int query(int x){
x=n-x+1;
int res=0;
for(int i=x;i>=1;i-=(i&(-i))) res+=bit[i];
return res;
}
}
/*
ST[0]: total < l
ST[1]: all sticks that odd one out
ST[2]: sticks - total
*/
void update(int i,int val){
BIT::update(i,val/2-a[i]/2);a[i]=val;
ST[0].update(1,n,1,i,a[i]);
ST[1].update(1,n,1,i,a[i]&1);
val=(2*i-1<=n?(a[2*i-1]&1):0)-(a[i]/2);
ST[2].update(1,2*n,1,2*i-1,val);
if(i%2==0) ST[2].update(1,2*n,1,i,a[i]&1);
else{
int p=(i+1)/2;
ST[2].update(1,2*n,1,i,(a[i]&1)-a[p]/2);
}
}
void solve(){
cin >> n >> q;
for(int i=1;i<=n;i++) cin >> aa[i];
for(int i=n;i>=1;i--) update(i,aa[i]);
auto check = [&](int mid){
node cur;
if(mid-1) cur=ST[0].query(1,n,1,1,mid-1)+ST[1].query(1,n,1,mid,min(2*mid-2,n));
cur=cur+ST[2].query(1,2*n,1,2*mid-1,2*n);
return (cur.Min>=0);
};
node st,md,en;
auto check2 = [&](int mid,int p){
node mm;
if(p==2) mm=node(a[p-1]-3*mid,a[p-1]-3*mid);
else{
node s=node(a[p-1]-2*mid,a[p-1]-2*mid);
int val=(2*p-3<=n?(a[2*p-3]&1):0LL)-mid;
node t=node(val,val);
mm=s+md+t;
}
//cout << mid << ' ' << st.Min << ' ' << st.sum << ' ' << mm.Min << ' ' << mm.sum << ' ' << en.sum << ' ' << en.sum << '\n';
node res=st+mm+en;
return (res.Min>=0);
};
for(int i=1;i<=q;i++){
int p,d;cin >> p >> d;update(p,a[p]+d);
int l=1,r=n;p=n+1;
while(r>=l){
int mid=(l+r)>>1;
if(check(mid)) p=mid,r=mid-1;
else l=mid+1;
}
//cout << '*' << p << '\n';
if(p==1) cout << BIT::query(p) << '\n';
else{
if(p>2) st=ST[0].query(1,n,1,1,p-2);
else st=node();
if(p<=2*p-4) md=ST[1].query(1,n,1,p,min(2*p-4,n));
else md=node();
en=ST[2].query(1,2*n,1,2*p-2,2*n);
//cout << st.Min << ' ' << st.sum << ' ' << md.Min << ' ' << md.sum << ' ' << en.Min << ' ' << en.sum << '\n';
int total=BIT::query(p);
l=1,r=a[p-1]/2;
int res=0;
while(l<=r){
int mid=(l+r)>>1;
if(check2(mid,p)) res=mid,l=mid+1;
else r=mid-1;
}
//cout << total << '\n';
cout << total+res << '\n';
}
}
}
signed main(){
//freopen("F.INP","r",stdin);
//freopen("F.OUT","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
77912 KB |
Output is correct |
2 |
Correct |
11 ms |
77916 KB |
Output is correct |
3 |
Correct |
14 ms |
77912 KB |
Output is correct |
4 |
Correct |
13 ms |
77912 KB |
Output is correct |
5 |
Correct |
13 ms |
77912 KB |
Output is correct |
6 |
Correct |
16 ms |
78096 KB |
Output is correct |
7 |
Correct |
15 ms |
77912 KB |
Output is correct |
8 |
Correct |
15 ms |
77912 KB |
Output is correct |
9 |
Correct |
19 ms |
77916 KB |
Output is correct |
10 |
Correct |
16 ms |
77912 KB |
Output is correct |
11 |
Correct |
16 ms |
77912 KB |
Output is correct |
12 |
Correct |
16 ms |
77912 KB |
Output is correct |
13 |
Correct |
15 ms |
78168 KB |
Output is correct |
14 |
Correct |
14 ms |
77912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
77912 KB |
Output is correct |
2 |
Correct |
11 ms |
77916 KB |
Output is correct |
3 |
Correct |
14 ms |
77912 KB |
Output is correct |
4 |
Correct |
13 ms |
77912 KB |
Output is correct |
5 |
Correct |
13 ms |
77912 KB |
Output is correct |
6 |
Correct |
16 ms |
78096 KB |
Output is correct |
7 |
Correct |
15 ms |
77912 KB |
Output is correct |
8 |
Correct |
15 ms |
77912 KB |
Output is correct |
9 |
Correct |
19 ms |
77916 KB |
Output is correct |
10 |
Correct |
16 ms |
77912 KB |
Output is correct |
11 |
Correct |
16 ms |
77912 KB |
Output is correct |
12 |
Correct |
16 ms |
77912 KB |
Output is correct |
13 |
Correct |
15 ms |
78168 KB |
Output is correct |
14 |
Correct |
14 ms |
77912 KB |
Output is correct |
15 |
Correct |
11 ms |
77912 KB |
Output is correct |
16 |
Correct |
11 ms |
77912 KB |
Output is correct |
17 |
Correct |
20 ms |
78168 KB |
Output is correct |
18 |
Correct |
20 ms |
78168 KB |
Output is correct |
19 |
Correct |
17 ms |
78168 KB |
Output is correct |
20 |
Correct |
17 ms |
78168 KB |
Output is correct |
21 |
Correct |
17 ms |
77912 KB |
Output is correct |
22 |
Correct |
17 ms |
78168 KB |
Output is correct |
23 |
Correct |
18 ms |
78168 KB |
Output is correct |
24 |
Correct |
16 ms |
78168 KB |
Output is correct |
25 |
Correct |
17 ms |
78424 KB |
Output is correct |
26 |
Correct |
16 ms |
78168 KB |
Output is correct |
27 |
Correct |
16 ms |
77912 KB |
Output is correct |
28 |
Correct |
11 ms |
77912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
77912 KB |
Output is correct |
2 |
Correct |
15 ms |
77912 KB |
Output is correct |
3 |
Correct |
100 ms |
79288 KB |
Output is correct |
4 |
Correct |
1344 ms |
83856 KB |
Output is correct |
5 |
Correct |
1254 ms |
83360 KB |
Output is correct |
6 |
Correct |
1294 ms |
83544 KB |
Output is correct |
7 |
Correct |
1356 ms |
83676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
77912 KB |
Output is correct |
2 |
Correct |
18 ms |
78172 KB |
Output is correct |
3 |
Correct |
17 ms |
78168 KB |
Output is correct |
4 |
Correct |
17 ms |
77912 KB |
Output is correct |
5 |
Correct |
1439 ms |
84148 KB |
Output is correct |
6 |
Correct |
1532 ms |
87196 KB |
Output is correct |
7 |
Correct |
1412 ms |
87016 KB |
Output is correct |
8 |
Correct |
1489 ms |
86876 KB |
Output is correct |
9 |
Correct |
1479 ms |
87104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
77912 KB |
Output is correct |
2 |
Correct |
11 ms |
77916 KB |
Output is correct |
3 |
Correct |
14 ms |
77912 KB |
Output is correct |
4 |
Correct |
13 ms |
77912 KB |
Output is correct |
5 |
Correct |
13 ms |
77912 KB |
Output is correct |
6 |
Correct |
16 ms |
78096 KB |
Output is correct |
7 |
Correct |
15 ms |
77912 KB |
Output is correct |
8 |
Correct |
15 ms |
77912 KB |
Output is correct |
9 |
Correct |
19 ms |
77916 KB |
Output is correct |
10 |
Correct |
16 ms |
77912 KB |
Output is correct |
11 |
Correct |
16 ms |
77912 KB |
Output is correct |
12 |
Correct |
16 ms |
77912 KB |
Output is correct |
13 |
Correct |
15 ms |
78168 KB |
Output is correct |
14 |
Correct |
14 ms |
77912 KB |
Output is correct |
15 |
Correct |
11 ms |
77912 KB |
Output is correct |
16 |
Correct |
11 ms |
77912 KB |
Output is correct |
17 |
Correct |
20 ms |
78168 KB |
Output is correct |
18 |
Correct |
20 ms |
78168 KB |
Output is correct |
19 |
Correct |
17 ms |
78168 KB |
Output is correct |
20 |
Correct |
17 ms |
78168 KB |
Output is correct |
21 |
Correct |
17 ms |
77912 KB |
Output is correct |
22 |
Correct |
17 ms |
78168 KB |
Output is correct |
23 |
Correct |
18 ms |
78168 KB |
Output is correct |
24 |
Correct |
16 ms |
78168 KB |
Output is correct |
25 |
Correct |
17 ms |
78424 KB |
Output is correct |
26 |
Correct |
16 ms |
78168 KB |
Output is correct |
27 |
Correct |
16 ms |
77912 KB |
Output is correct |
28 |
Correct |
11 ms |
77912 KB |
Output is correct |
29 |
Correct |
16 ms |
77912 KB |
Output is correct |
30 |
Correct |
15 ms |
77912 KB |
Output is correct |
31 |
Correct |
100 ms |
79288 KB |
Output is correct |
32 |
Correct |
1344 ms |
83856 KB |
Output is correct |
33 |
Correct |
1254 ms |
83360 KB |
Output is correct |
34 |
Correct |
1294 ms |
83544 KB |
Output is correct |
35 |
Correct |
1356 ms |
83676 KB |
Output is correct |
36 |
Correct |
15 ms |
77912 KB |
Output is correct |
37 |
Correct |
18 ms |
78172 KB |
Output is correct |
38 |
Correct |
17 ms |
78168 KB |
Output is correct |
39 |
Correct |
17 ms |
77912 KB |
Output is correct |
40 |
Correct |
1439 ms |
84148 KB |
Output is correct |
41 |
Correct |
1532 ms |
87196 KB |
Output is correct |
42 |
Correct |
1412 ms |
87016 KB |
Output is correct |
43 |
Correct |
1489 ms |
86876 KB |
Output is correct |
44 |
Correct |
1479 ms |
87104 KB |
Output is correct |
45 |
Correct |
1493 ms |
88496 KB |
Output is correct |
46 |
Correct |
1434 ms |
86892 KB |
Output is correct |
47 |
Correct |
1530 ms |
88148 KB |
Output is correct |
48 |
Correct |
1417 ms |
83816 KB |
Output is correct |
49 |
Correct |
1370 ms |
86976 KB |
Output is correct |
50 |
Correct |
1623 ms |
85576 KB |
Output is correct |
51 |
Correct |
1203 ms |
85584 KB |
Output is correct |
52 |
Correct |
1419 ms |
88256 KB |
Output is correct |
53 |
Correct |
1114 ms |
85416 KB |
Output is correct |
54 |
Correct |
1287 ms |
84384 KB |
Output is correct |