-
Notifications
You must be signed in to change notification settings - Fork 3
/
F.cpp
54 lines (51 loc) · 929 Bytes
/
F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// LUOGU_RID: 157353490
#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=30;
long long cnt[maxn];
int main()
{
ios::sync_with_stdio(false),cin.tie(0);
int n,q,i,op,x;
long long k;
cin>>n>>q;
for(i=0;i<n;i++) cin>>cnt[i];
while(q--)
{
cin>>op>>x;
if(op==1) cin>>cnt[x];
else
{
long long rem=0,ans=0;
cin>>k;
for(i=0;i<=x;i++) k-=cnt[i],rem+=((1<<i)-1)*cnt[i];
for(i=x+1;i<n&&k>0;i++)
{
long long use=min(k>>(i-x),cnt[i]);
k-=use<<(i-x);
ans+=((1<<(i-x))-1)*use;
rem+=((1<<x)-1)*use<<(i-x);
if(use<cnt[i])
{
while(k>rem)
{
ans++;
i--;
if(k>=1<<(i-x))
{
k-=1<<(i-x);
ans+=(1<<(i-x))-1;
rem+=((1<<x)-1)<<(i-x);
}
}
ans+=k;
k=0;
break;
}
}
if(k>0&&k<=rem) ans+=k,k=0;
cout<<(k>0?-1:ans)<<'\n';
}
}
return 0;
}