QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#261721 | #5415. Ropeway | vp_account# | WA | 2ms | 14200kb | C++14 | 2.6kb | 2023-11-23 09:25:33 | 2023-11-23 09:25:34 |
Judging History
answer
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=500005;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,K,a[N],pre[N],suf[N];
char s[N];
ll f[N],g[N],ff[N],gg[N];
int Q[N],L,R;
int read() {
int x=0,f=1; char c=getchar();
for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-48;
return x*f;
}
void solve() {
n=read(); K=read();
for(int i=1;i<=n;i++) a[i]=read();
scanf("%s",s+1);
for(int i=2;i<=n;i++) {
pre[i]=pre[i-1];
if(s[i-1]=='1') pre[i]=i-1;
}
suf[n]=n+1;
for(int i=n-1;i>=1;i--) {
suf[i]=suf[i+1];
if(s[i+1]=='1') suf[i]=i+1;
}
Q[L=R=1]=0;
for(int i=1;i<=n;i++) {
while(Q[L]<pre[i]||i-Q[L]>K) L++;
f[i]=f[Q[L]]+a[i];
while(L<=R&&f[i]<=f[Q[R]]) R--;
Q[++R]=i;
}
g[n+1]=0; Q[L=R=1]=n+1;
for(int i=n;i>=1;i--) {
while(Q[L]>suf[i]||Q[L]-i>K) L++;
g[i]=g[Q[L]]+a[i];
while(L<=R&&g[i]<=g[Q[R]]) R--;
Q[++R]=i;
}
for(int q=read();q--;) {
int x=read(),v=read();
if(s[x]=='1') printf("%lld\n",f[x]+g[x]-2*a[x]+v);
else {
if(suf[x]-pre[x]<=K) {
printf("%lld\n",f[pre[x]]+g[pre[x]]-a[pre[x]]);
continue;
}
int l=max(x-K,pre[x]),r=min(x+K,suf[x]);
for(int i=l;i<=r;i++) ff[i]=f[i],gg[i]=g[i];
L=1; R=0;
for(int i=l;i<x;i++) {
while(L<=R&&ff[i]<=ff[Q[R]]) R--;
Q[++R]=i;
}
ff[x]=ff[Q[L]]+v;
while(L<=R&&ff[x]<=ff[Q[R]]) R--;
Q[++R]=x;
for(int i=x+1;i<=r;i++) {
while(i-Q[L]>K) L++;
ff[i]=ff[Q[L]]+a[i];
while(L<=R&&ff[i]<=ff[Q[R]]) R--;
}
L=1; R=0;
for(int i=r;i>x;i--) {
while(L<=R&&gg[i]<=gg[Q[R]]) R--;
Q[++R]=i;
}
gg[x]=gg[Q[L]]+v;
while(L<=R&&gg[x]<=gg[Q[R]]) R--;
Q[++R]=x;
for(int i=x-1;i>=l;i--) {
while(Q[L]-i>K) L++;
gg[i]=gg[Q[L]]+a[i];
while(L<=R&&gg[i]<=gg[Q[R]]) R--;
}
ll ans=INF;
int o=a[x]; a[x]=v;
for(int i=l;i<=r;i++) ans=min(ans,ff[i]+gg[i]-a[i]);
a[x]=o;
printf("%lld\n",ans);
}
}
}
int main() {
for(int T=read();T--;) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 14200kb
input:
3 10 3 5 10 7 100 4 3 12 5 100 1 0001000010 2 2 3 6 15 5 6 1 1 1 1 1 00000 1 3 100 5 6 1 1 1 1 1 00100 1 3 100
output:
206 214 0 100
result:
ok 4 number(s): "206 214 0 100"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 14056kb
input:
500 19 6 285203460 203149294 175739375 211384407 323087820 336418462 114884618 61054702 243946442 19599175 51974474 285317523 222489944 26675167 300331960 1412281 324105264 33722550 169011266 1111111110110100011 18 3 127056246 5 100630226 14 301161052 2 331781882 5 218792226 2 190274295 12 49227476 ...
output:
2472886431 2299111966 2796055445 2650202148 2417273966 2508694561 2285479513 2521569560 2521569560 2240907690 2577284958 2521569560 2766700195 2511807344 2521569560 2438434986 2669077215 2682112324 470735446 0 211705888 564509290 715543137 0 470735446 18 19 19 19 20 19 54 849950346 849950346 8499503...
result:
wrong answer 20th numbers differ - expected: '470735446', found: '0'