QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#353539 | #3050. Kuru Kuru Sushi | Kevin5307 | TL | 548ms | 6224kb | C++20 | 2.5kb | 2024-03-14 10:48:41 | 2024-03-14 10:48:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n,q;
ll w[100100],psum[100100];
int s[100100],t[100100],fa[100100];
ll f[100100],g[100100];
inline int anc(int x)
{
while(fa[x]!=x) x=fa[x]=fa[fa[x]];
return x;
}
mt19937_64 rnd(20210448);
void _sort(vector<int> vec,vector<int> &sorted)
{
if(!vec.size()) return ;
int ind=rnd()%vec.size();
int x=vec[ind];
int a=min(s[x],t[x]),b=max(s[x],t[x]);
vector<int> v1,v2;
for(auto y:vec)
if(y!=x)
{
if(a<=s[y]&&s[y]<=b&&a<=t[y]&&t[y]<=b)
v1.push_back(y);
else
v2.push_back(y);
}
sort(v1.begin(),v1.end(),[&](int u,int v)
{
if(s[x]<t[x])
return pair<int,int>(s[u]-s[x],t[x]-t[u])>pair<int,int>(s[v]-s[x],t[x]-t[v]);
return pair<int,int>(t[u]-t[x],s[x]-s[u])>pair<int,int>(t[v]-t[x],s[x]-s[v]);
});
for(auto x:v1)
sorted.push_back(x);
sorted.push_back(x);
sort(v2.begin(),v2.end(),[&](int u,int v)
{
if(s[x]<t[x])
return pair<int,int>((s[x]-s[u]+n)%n,(t[u]-t[x]+n)%n)<pair<int,int>((s[x]-s[v]+n)%n,(t[v]-t[x]+n)%n);
return pair<int,int>((t[x]-t[u]+n)%n,(s[u]-s[x]+n)%n)<pair<int,int>((t[x]-t[v]+n)%n,(s[v]-s[x]+n)%n);
});
for(auto x:v2)
sorted.push_back(x);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
fa[i]=i;
cin>>w[i];
psum[i]=psum[i-1]+w[i];
}
for(int i=1;i<=q;i++)
cin>>s[i]>>t[i];
for(int i=1;i<=q;i++)
for(int j=i+1;j<=q;j++)
{
int a=min(s[i],t[i]),b=max(s[i],t[i]);
if(((s[j]>a&&s[j]<b)^(t[j]>a&&t[j]<b))&&s[j]!=s[i]&&s[j]!=t[i]&&t[j]!=s[i]&&t[j]!=t[i])
{
fa[anc(i)]=anc(j);
break;
}
else if((a>=max(s[j],t[j])||b<=min(s[j],t[j]))^(s[j]<t[j])^(s[i]<t[i]))
{
fa[anc(i)]=anc(j);
break;
}
}
vector<int> vec,sorted;
for(int i=1;i<=q;i++)
if(fa[i]==i)
vec.push_back(i);
_sort(vec,sorted);
for(int i=1;i<=q;i++)
if(s[i]<t[i])
{
f[anc(i)]+=psum[t[i]]-psum[s[i]];
g[anc(i)]+=psum[n]-psum[t[i]]+psum[s[i]];
}
else
{
f[anc(i)]+=psum[n]-psum[s[i]]+psum[t[i]];
g[anc(i)]+=psum[s[i]]-psum[t[i]];
}
if(s[sorted[0]]<t[sorted[0]])
{
ll tot=0;
ll ans=0x3f3f3f3f3f3f3f3f;
for(auto x:sorted)
tot+=g[x];
ans=tot;
for(auto x:sorted)
{
tot+=f[x]-g[x];
ans=min(ans,tot);
}
cout<<ans<<endl;
}
else
{
ll tot=0;
ll ans=0x3f3f3f3f3f3f3f3f;
for(auto x:sorted)
tot+=f[x];
ans=tot;
for(auto x:sorted)
{
tot+=g[x]-f[x];
ans=min(ans,tot);
}
cout<<ans<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 548ms
memory: 6224kb
input:
100000 13965 6828 987 32 151 5 44713 513 369 1934 1913 90374 4 336 286 4116 59505 8184 42405 84543 319 140 2888 6181 1 32 4535 39 1781 11 2 59002 12 1001 895 9 12 26 9497 15169 40 197 8 793 6915 40 47 10 35 3 106 3 3348 5 540 3269 439 402 1309 1165 71 91007 58683 944 11098 1976 5267 12974 170 2161 2...
output:
3010719821217
result:
ok single line: '3010719821217'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3588kb
input:
4 4 1 1 1 1 0 1 0 3 2 3 3 0
output:
6
result:
ok single line: '6'
Test #3:
score: -100
Time Limit Exceeded
input:
100000 66336 384 935 2205 40262 8 70760 68 479 119 339 147 40344 2334 2 19936 62 1 1680 4 296 63976 60390 236 1 132 6104 9063 285 1 95 29 2 3 13723 3 67 45117 181 76 558 4075 405 15 76 578 265 78 5710 14 6577 2926 25300 1 88341 144 68741 1841 1 43533 1 84791 65657 3532 21 23820 1 1 88763 2 15185 149...