QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#44463 | #4583. Concerto de Pandemic | eyiigjkn | TL | 1120ms | 19004kb | C++14 | 2.6kb | 2022-08-17 18:03:19 | 2022-08-17 18:03:21 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=1e6;
int n,m,K,lim,a[200010],b[200010],d[200010],nxt[20][200010],len[20][200010],cnt[200010],cnt1[200010];
ll sum[200010];
pair<int,int> upd[400010];
int add(int x,int y){return (x+=y)<INF?x:INF;}
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 x,int y){for(int i=x;i<=m;i+=i&-i) cnt[i]+=y;}
int query(int x)
{
int ans=cnt1[x];
for(int i=x;i;i-=i&-i) ans+=cnt[i];
return ans;
}
int getle(int l,int r)
{
if(l>r) return 0;
int cur=-query(--l),t=0,s;
for(int i=__lg(r);i>=0;i--)
if(t+(1<<i)<=r && cur+(s=cnt[t+(1<<i)]+cnt1[t+(1<<i)]-cnt1[t])<=0)
cur+=s,t+=1<<i;
return (++t)<=r?t:0;
}
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;
cnt1[R]++;
upd[++tot]={R+1,R};
upd[++tot]={L,-R};
}
else
{
if(t>R && t<L) continue;
upd[++tot]={R+1,R};
upd[++tot]={L,-R};
}
}
for(int i=1;i<=m;i++) cnt1[i]+=cnt1[i-1];
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(x,1);
else update(-x,-1);
}
nxt[0][i]=getle(i+1,m);
if(!nxt[0][i]) nxt[0][i]=getle(1,i-1);
}
for(int i=1;i<=m+1;i++) cnt[i]=cnt1[i]=0;
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]=add(len[i-1][j],len[i-1][nxt[i-1][j]]);
}
for(int i=1;i<=m;i++)
{
int u=i,j=0,cur=0,cnt=1;
while(j+1<=17 && len[j][u]<m) j++;
for(;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;
cin>>n>>m>>K>>lim;
ll l=0,r=n,mid;
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3824kb
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: 3844kb
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: 3992kb
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: 3764kb
input:
2 1 1 1 1 200000 2
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 1120ms
memory: 19004kb
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:
170531
result:
ok single line: '170531'
Test #6:
score: -100
Time Limit Exceeded
input:
198722 26425 169256 110599 33948 74442 51729 66300 40369 173859 42274 73043 117803 108716 149794 151005 147161 2675 148063 166634 132585 51612 141999 182365 32951 159790 120932 290 82655 150138 49337 10396 171146 129572 33311 193079 195115 171691 180568 77905 65397 110312 156436 149966 9377 55490 12...