QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#688652#5415. Ropewayphil071128TL 0ms0kbC++141.9kb2024-10-30 12:00:162024-10-30 12:00:17

Judging History

你现在查看的是最新测评结果

  • [2024-10-30 12:00:17]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-30 12:00:16]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
int read(){
	char c=getchar();int h=0,tag=1;
	while(!isdigit(c)) tag=(c=='-'?-1:1),c=getchar();
	while(isdigit(c)) h=(h<<1)+(h<<3)+(c^48),c=getchar();
	return h*tag;
}
void fil(){
	freopen("3.in","r",stdin);
	freopen("cablecar.out","w",stdout);
}
const int N=2e6+500;
int head=1,tail=0;
int q[N],a[N],f[N],g[N],h[N];
char s[N];
signed main(){
//	fil();
	int n=read(),k=read();
	for(int i=1;i<=n;i++) a[i]=read();
	scanf("%s",s+1);
	s[n+1]='1';
//	a[2]=3;
	q[++tail]=0;
	for(int i=1;i<=n+1;i++) {
		while(head<=tail&&i-q[head]>k) head++;
		f[i]=f[q[head]]+a[i];
		h[i]=f[i];
//		cout<<i<<" "<<f[i]<<"\n";
		while(head<=tail&&f[q[tail]]>f[i]) tail--;
		if(s[i]=='1') head=1,tail=0;
		q[++tail]=i;
	} 
	head=1,tail=0;
	q[++tail]=0;
	for(int i=n+1;i>=1;i--) {
		while(head<=tail&&q[head]-i>k) head++;
		g[i]=g[q[head]]+a[i];
		while(head<=tail&&g[q[tail]]>g[i]) tail--;
		if(s[i]=='1') head=1,tail=0;
		q[++tail]=i;
	}
//	int ans=1e9;
//	for(int i=1;i<=n;i++) ans=min(ans,f[i]+g[i]-a[i]);
//	cout<<ans<<"\n";
//	return 0;
	int m=read();
	for(int i=1;i<=m;i++) {
		int x=read(),y=read(),ans=1e17;
		head=1,tail=0;q[++tail]=0;
		for(int j=max(1ll,x-k);j<=x;j++) {
			while(head<=tail&&j-q[head]>k) head++;
			if(j==x) {
				f[j]=f[q[head]]+y;
				ans=min(ans,f[j]+g[j]-a[j]);
			}
			while(head<=tail&&f[q[tail]]>f[j]) tail--;
			if(s[j]=='1') head=1,tail=0;
			q[++tail]=j;
		}
		for(int j=x+1;j<=min(n+1,x+k);j++) {
			while(head<=tail&&j-q[head]>k) head++;
			f[j]=f[q[head]]+a[j];
			ans=min(ans,f[j]+g[j]-a[j]);
			while(head<=tail&&f[q[tail]]>f[j]) tail--;
			if(s[j]=='1') head=1,tail=0;
			q[++tail]=j;
		}
		for(int j=x;j<=min(n+1,x+k);j++) f[j]=h[j];
		cout<<ans<<"\n";
	}
	return 0;
}


詳細信息

Test #1:

score: 0
Time Limit Exceeded

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:


result: