社区讨论
WA on 29求调
CF1981E Turtle and Intersected Segments参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @m34h9zfc
- 此快照首次捕获于
- 2024/11/05 21:20 去年
- 此快照最后确认于
- 2025/11/04 15:16 4 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
namespace Radon{
void Main();
}
int main(){
Radon::Main();
return 0;
}
namespace Radon{
#define int long long
#define N 500010
int n;
struct line{
int l,r,a;
}a[N<<2];
struct node{
int x,opt,a;
friend bool operator <(node x,node y){
if(x.a != y.a){
return x.a<y.a;
}else if(x.opt!=y.opt){
return x.opt<y.opt;
}else{
return x.x<y.x;
}
}
}b[N<<2];
struct edge{
int u,v,w;
}e[N<<2];
bool cmp(node x,node y){
if(x.x==y.x){
return x.opt<y.opt;
}
return x.x<y.x;
}
bool ccmp(edge x,edge y){
return x.w<y.w;
}
int cnt=0;
int ccnt=0;
int cccnt=0;
int l[N<<2];
int pre[N<<2];
int fa[N<<2];
int find(int x){
if(fa[x]==x){
return x;
}
fa[x]=find(fa[x]);
return fa[x];
}
void init(){
cnt=0;
ccnt=0;
cccnt=0;
}
void Main(){
// freopen("CF1981E.in","r",stdin);
// freopen("CF1981E.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin >> T;
while(T--){
init();
cin >> n;
for(int x=1;x<=n;x++){
cin >> a[x].l >> a[x].r >> a[x].a;
a[x].r++;
l[++cnt]=a[x].l;
l[++cnt]=a[x].r;
}
sort(l+1,l+1+(n*2));
cnt=unique(l+1,l+1+(n*2))-l-1;
for(int x=1;x<=n;x++){
a[x].l=lower_bound(l+1,l+1+cnt,a[x].l)-l;
a[x].r=lower_bound(l+1,l+1+cnt,a[x].r)-l;
b[++ccnt]={a[x].l,x,a[x].a};
b[++ccnt]={a[x].r,-x,a[x].a};
pre[x]=a[x].l;
}
set<node>s;
sort(b+1,b+1+ccnt,cmp);
// for(int x=1;x<=ccnt;x++){
// cout << x << ':' << b[x].x << ' ' << b[x].opt << ' ' << b[x].a << '\n';
// }
for(int x=1;x<=ccnt;x++){
// cout << "\n___________________________________\n";
node now=b[x];
// cout << x << ':' << now.x << ' ' << now.opt << ' ' << now.a << endl;
if(b[x].opt<0){
// cout << "erase.\n";
s.erase({pre[-now.opt],-now.opt,now.a});
// s.erase(s.find({pre[-now.opt],-now.opt,now.a}))
}else{
// cout << "insert.\n";
s.insert(now);
// cout << "now:" << now.x << ' ' << now.opt << ' ' << now.a << '\n';
// cout << "size:" << s.size() << "\n";
auto it=s.lower_bound({now.x,now.opt,now.a});
if(next(it)!=s.end()){
// cout << "edge it&it+1 ";
it++;
e[++cccnt]={(*it).opt,now.opt,abs(now.a-(*it).a)};
it--;
}
if(it!=s.begin()){
// cout << "edge it&it-1 ";
it--;
e[++cccnt]={(*it).opt,now.opt,abs(now.a-(*it).a)};
it++;
}
}
// cout << "\n___________________________________\n";
}
sort(e+1,e+1+cccnt,ccmp);
// cout << "cccnt:" << cccnt << endl;
// for(int x=1;x<=cccnt;x++){
// cout << x << ':' << e[x].u << ' ' << e[x].v << ' ' << e[x].w << '\n';
// }
int ans=0;
for(int x=1;x<=cnt+1;x++){
fa[x]=x;
}
int ccccnt=0;
for(int x=1;x<=cccnt;x++){
int u=e[x].u,v=e[x].v,w=e[x].w;
int fu=find(u),fv=find(v);
if(fu==fv){
continue;
}else{
fa[fu]=fv;
ans+=w;
ccccnt++;
}
}
if(ccccnt==n-1){
cout << ans << endl;
}else{
cout << -1 << endl;
}
// cout << ans << endl;
}
}
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...