QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#28240 | #2269. To be Connected, or not to be, that is the Question | Suika_predator# | WA | 557ms | 425852kb | C++20 | 2.7kb | 2022-04-12 21:38:29 | 2022-04-29 09:19:08 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct node
{
int l,r;
vector<tuple<int,int,int>> eg;
node *lson,*rson;
}*root,pool[7777777];
int top;
node *build(int l,int r)
{
node *ret=&pool[top++];
ret->l=l;ret->r=r;
return ret;
}
void ins(node *cur,int l,int r,const tuple<int,int,int> &eg)
{
// auto [x,y,z]=eg;
// cerr<<"ins "<<l<<' '<<r<<' '<<x<<' '<<y<<' '<<z<<endl;
if(cur->l==l and cur->r==r)
{
cur->eg.push_back(eg);
return;
}
int mid=(cur->l+cur->r)/2;
if(mid>=r)
{
if(not cur->lson)cur->lson=build(cur->l,mid);
ins(cur->lson,l,r,eg);
}
else if(mid+1<=l)
{
if(not cur->rson)cur->rson=build(mid+1,cur->r);
ins(cur->rson,l,r,eg);
}
else
{
if(not cur->lson)cur->lson=build(cur->l,mid);
if(not cur->rson)cur->rson=build(mid+1,cur->r);
ins(cur->lson,l,mid,eg);
ins(cur->rson,mid+1,r,eg);
}
}
int pa[111111],sz[111111];
int find(int x){return pa[x]?find(pa[x]):x;}
int cntv[2];
int cnte[2]={0,0};
vector<int> sr;
bool check(int mid)
{
while(sr.back()<=mid)sr.pop_back(),cntv[1]--,cntv[0]++;
// cerr<<"check "<<mid<<' '<<cntv[0]<<' '<<cntv[1]<<' '<<cnte[0]<<' '<<cnte[1]<<endl;
int neede=cntv[0]+cntv[1]-cnte[0]-cnte[1]-1;
return cntv[0]>=neede and cntv[1]>=neede;
}
void solve(node *cur)
{
// cerr<<"dfs "<<cur->l<<' '<<cur->r<<endl;
vector<pair<int*,int>> mod;
int tmp[2]={cnte[0],cnte[1]};
for(auto [u,v,ty]:cur->eg)
{
int pu=find(u),pv=find(v);
if(pu==pv)continue;
// cerr<<"merge "<<pu<<' '<<pv<<endl;
if(sz[pu]>sz[pv])swap(pu,pv);
mod.emplace_back(&pa[pu],pa[pu]);
pa[pu]=pv;
mod.emplace_back(&sz[pv],sz[pv]);
sz[pv]+=sz[pu];
cnte[ty]++;
}
// cerr<<"?? "<<endl;
int mid=(cur->l+cur->r)/2;
if(cur->lson)solve(cur->lson);
else if(check(cur->l))
{
cout<<cur->l<<endl;
exit(0);
}
if(cur->rson)solve(cur->rson);
else if(check(mid+1))
{
cout<<mid+1<<endl;
exit(0);
}
reverse(mod.begin(),mod.end());
for(auto [pt,x]:mod)*pt=x;
cnte[0]=tmp[0],cnte[1]=tmp[1];
}
int w[111111],u[111111],v[111111];
int main()
{
ios_base::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)sz[i]=1;
cntv[1]=n;
int minn=1e9,maxx=0;
for(int i=1;i<=n;i++)
{
cin>>w[i];
minn=min(minn,w[i]);
maxx=max(maxx,w[i]);
sr.push_back(w[i]);
}
sort(sr.begin(),sr.end(),greater<int>());
if(minn==maxx)
{
cout<<-1<<endl;
return 0;
}
root=build(minn,maxx-1);
for(int i=1;i<=m;i++)
{
cin>>u[i]>>v[i];
if(w[u[i]]>w[v[i]])swap(u[i],v[i]);
if(minn<w[u[i]])ins(root,minn,w[u[i]]-1,{u[i],v[i],1});
if(w[v[i]]<maxx)ins(root,w[v[i]],maxx-1,{u[i],v[i],0});
}
solve(root);
cout<<-1<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 68ms
memory: 368660kb
input:
2 0 861866350 106689920
output:
106689920
result:
ok single line: '106689920'
Test #2:
score: 0
Accepted
time: 87ms
memory: 369208kb
input:
3 0 582295931 120217528 845887275
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 92ms
memory: 370096kb
input:
52033 0 432755200 936478974 298744051 31368207 847742874 81290408 425992405 651328821 903238557 526933479 356290128 722885083 779029575 480262946 443316470 542413465 170562283 440427743 438763956 784112617 255213130 899556824 323259505 857165776 533714690 565510843 859610987 686006833 211894364 9600...
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 79ms
memory: 369924kb
input:
100000 0 514837648 759500586 899265033 24085608 545610751 221779667 568755229 169602804 284396186 169538713 571993850 645890208 375601406 769765103 805781464 228017324 648075651 874669052 771742115 269678248 190757592 220852391 275602630 816966668 111244645 208546040 715493307 277760893 770626133 25...
output:
-1
result:
ok single line: '-1'
Test #5:
score: -100
Wrong Answer
time: 557ms
memory: 425852kb
input:
100000 99999 299608910 294889223 380597480 583050141 733930013 271705935 623956575 293208851 168678637 517320846 970153874 376864085 620559158 384944405 959726556 311522848 233740144 852169507 874336822 670072232 182817184 163689537 962302870 278762094 916902553 742474244 377317908 941252256 1153825...
output:
18995
result:
wrong answer 1st lines differ - expected: '500886962', found: '18995'