QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#44460#4583. Concerto de PandemiceyiigjknWA 1539ms19508kbC++142.8kb2022-08-17 16:54:042022-08-17 16:54:06

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-17 16:54:06]
  • 评测
  • 测评结果:WA
  • 用时:1539ms
  • 内存:19508kb
  • [2022-08-17 16:54:04]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,K,lim,a[200010],b[200010],d[200010],nxt[20][200010],len[20][200010],cnt[600010];
ll sum[200010];
pair<int,int> seg[200010],upd[400010];
int goleft(int x,int k){return (x-=k)>0?x:x+m;}
int goright(int x,int k){return (x+=k)<=m?x:x-m;}
ll calc(int l,int r){return l<=r?sum[r]-sum[l-1]+r-l:sum[n]-sum[l-1]+sum[r]+n-l+r;}
int dis(int x,int y){return x<y?y-x:m-x+y;}
void update(int rt,int l,int r,int x,int y)
{
	if(l==r) return cnt[rt]+=y,void();
	int mid=(l+r)/2;
	if(x<=mid) update(rt*2,l,mid,x,y);
	else update(rt*2+1,mid+1,r,x,y);
	cnt[rt]=cnt[rt*2]+cnt[rt*2+1];
}
int query(int rt,int l,int r,int x,int y)
{
	if(l>y || r<x) return 0;
	if(l==r) return cnt[rt]?l:0;
	int mid=(l+r)/2;
	if(l>=x && r<=y)
		if(cnt[rt*2]) return query(rt*2,l,mid,x,y);
		else if(cnt[rt*2+1]) return query(rt*2+1,mid+1,r,x,y);
		else return 0;
	int L=query(rt*2,l,mid,x,y);
	if(L) return L;
	else return query(rt*2+1,mid+1,r,x,y);
}
void clear(int rt,int l,int r)
{
	cnt[rt]=0;
	if(l==r) return;
	int mid=(l+r)/2;
	clear(rt*2,l,mid);
	clear(rt*2+1,mid+1,r);
}
bool judge(ll x)
{
	int tot=0;
	for(int i=1;i<=K;i++)
	{
		int t=b[d[i]],l=0,r=m-1,mid,L,R;
		while(l<r)
		{
			mid=(l+r+1)/2;
			if(calc(a[goleft(t,mid)],d[i])<=x) l=mid;
			else r=mid-1;
		}
		L=goleft(t,l);l=0;r=m-1;
		while(l<r)
		{
			mid=(l+r+1)/2;
			if(calc(d[i],a[goright(t,mid)])<=x) l=mid;
			else r=mid-1;
		}
		R=goright(t,l);
		if(L<=R)
		{
			if(t<L || t>R) continue;
			upd[++tot]={R+1,R};
			upd[++tot]={1,R};upd[++tot]={L,-R};
		}
		else
		{
			if(t>R && t<L) continue;
			upd[++tot]={R+1,R};
			upd[++tot]={L,-R};
		}
	}
	sort(upd+1,upd+tot+1);
	for(int i=1,j=1;i<=m;i++)
	{
		for(;j<=tot && upd[j].first<=i;j++)
		{
			int x=upd[j].second;
			if(x>0) update(1,1,m,x,1);
			else update(1,1,m,-x,-1);
		}
		nxt[0][i]=query(1,1,m,i+1,m);
		if(!nxt[0][i]) nxt[0][i]=query(1,1,m,1,i-1);
	}
	clear(1,1,m);
	for(int i=1;i<=m;i++)
		if(!nxt[0][i]) return 1;
		else len[0][i]=dis(i,nxt[0][i]);
	for(int i=1;i<=17;i++)
		for(int j=1;j<=m;j++)
		{
			nxt[i][j]=nxt[i-1][nxt[i-1][j]];
			len[i][j]=len[i-1][j]+len[i-1][nxt[i-1][j]];
		}
	for(int i=1;i<=m;i++)
	{
		int u=i,cur=0,cnt=1;
		for(int j=17;j>=0;j--)
			if(cur+len[j][u]<m)
			{
				cur+=len[j][u];
				cnt+=1<<j;
				u=nxt[j][u];
			}
		if(cnt<=lim) return 1;
	}
	return 0;
}
int main()
{
	int x,y;
	ll l=0,r=0,mid;
	cin>>n>>m>>K>>lim;
	for(int i=1;i<=m;i++) scanf("%d%d",&x,&y),r+=(sum[x]=y);
	m=0;
	for(int i=1;i<=n;i++)
		if(!sum[i])
			a[b[i]=++m]=i;
	for(int i=1;i<=n;i++) sum[i]+=sum[i-1];
	for(int i=1;i<=K;i++) scanf("%d",&d[i]);
	while(l<r)
	{
		mid=(l+r)/2;
		if(judge(mid)) r=mid;
		else l=mid+1;
	}
	cout<<l<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3712kb

input:

10 4 3 2
1 2
4 4
6 2
7 5
2 5 8

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3984kb

input:

8 1 3 5
1 5
4 2 7

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3868kb

input:

5 2 2 1
1 14
2 14
3 5

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3700kb

input:

2 1 1 1
1 200000
2

output:

0

result:

ok single line: '0'

Test #5:

score: -100
Wrong Answer
time: 1539ms
memory: 19508kb

input:

190976 113222 55610 23475
51263 120558
10007 171596
46671 108981
117183 169457
18187 84735
149298 124718
79376 129184
28117 76880
109791 87521
114840 59510
38014 178362
41701 11344
27561 192741
173835 54534
71368 76692
122745 95537
152595 158352
43901 162441
98927 105784
22484 96000
19443 113614
370...

output:

5646613975

result:

wrong answer 1st lines differ - expected: '170531', found: '5646613975'