题解:P6270 [湖北省队互测 2014] 十万人的地铁

· · 题解

模拟赛场切了,简单题。

猜测线段相交连边构成的连通块内可以任意交换,考虑用线段树下放并查集标记后求出联通等价类,等价类内部按照 s,t 排序后匹配,发现第一问过了。

进一步地,求出这个等价类关系其实是在扯淡,直接全局按照 s,t 排序后匹配求出来的答案是一样的。

考虑构造方案,我们尝试增量构造。

每次找到 \max s,\max t,如果是同一个人就直接走过去,否则分讨两个人之间的关系,不难发现容易将局面拆解到先进行剥掉 \max s,\max t 并加入两者另一个端点连边后形成的局面再进行一个人走到另一个人的终点并交换的操作或者先让一个人走到另一个人的起点并交换然后进行剥掉 \max s,\max t 后形成的局面对应的操作,用优先队列维护 \max s,\max t 就做完了(删除可以惰性删除)。

具体的分类讨论细节可以见代码。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+114;
const int maxm = 1e6+114;
int fa[maxn];
int n,m;
int tag[maxm<<2];
void build(int cur,int lt,int rt){  
    tag[cur]=0;
    if(lt==rt) return ;
    int mid=(lt+rt)>>1;
    build(cur<<1,lt,mid);
    build(cur<<1|1,mid+1,rt);
}
int found(int u){
    return fa[u]=(fa[u]==u?u:found(fa[u]));
}
void sol(int cur,int lt,int rt,int l,int r,int c){
    if(rt<l||r<lt) return ;
    if(l<=lt&&rt<=r){
        if(tag[cur]==0) tag[cur]=found(c);
        else fa[found(tag[cur])]=found(c);
        return ;
    }
    int mid=(lt+rt)>>1;
    sol(cur<<1,lt,mid,l,r,c);
    sol(cur<<1|1,mid+1,rt,l,r,c);
}
void pushdown(int cur,int lt,int rt){
    if(lt==rt) return ;
    if(tag[cur]!=0){
        if(tag[cur<<1]==0) tag[cur<<1]=found(tag[cur]);
        else fa[found(tag[cur<<1])]=found(tag[cur]);
        if(tag[cur<<1|1]==0) tag[cur<<1|1]=found(tag[cur]);
        else fa[found(tag[cur<<1|1])]=found(tag[cur]);
    }
    int mid=(lt+rt)>>1;
    pushdown(cur<<1,lt,mid);
    pushdown(cur<<1|1,mid+1,rt);
}
vector<int> S[maxn],T[maxn],Id[maxn];
int s[maxn],t[maxn];
int chifan[maxn];
int tot;
priority_queue< pair< pair<int,int> ,pair<int,int> > > qS,qT;
vector< pair<int,pair<int,int> > > opt;
void build(){
    while(qS.size()>0&&qS.top().second.second!=chifan[qS.top().second.first]) qS.pop();
    while(qT.size()>0&&qT.top().second.second!=chifan[qT.top().second.first]) qT.pop();
    if(qS.size()==1&&qT.size()==1){
        int id=qS.top().second.first;
        int ps=qS.top().first.first;
        int pt=qS.top().first.second;
        chifan[id]=++tot;//惰性删除
        if(ps!=pt) opt.push_back({0,{id,pt}});
        return ;
    }
    int id1=qS.top().second.first;
    int s1=qS.top().first.first,t1=qS.top().first.second;
    int id2=qT.top().second.first;
    chifan[id1]=++tot,chifan[id2]=++tot;
    int s2=qT.top().first.second,t2=qT.top().first.first;
    //s max 和 t max
    if(id1==id2){
        build();
        if(s1!=t1) opt.push_back({0,{id1,t1}});
    }else if(t1>=s1){
        qS.push({{s2,t1},{id2,chifan[id2]}});
        qT.push({{t1,s2},{id2,chifan[id2]}});
        build();
        if(s1!=t1) opt.push_back({0,{id1,t1}});
        opt.push_back({1,{id1,id2}});
        if(t1!=t2) opt.push_back({0,{id2,t2}});
    }else{
        if(t2<=s2){
            if(s2!=s1) opt.push_back({0,{id1,s2}});
            opt.push_back({1,{id1,id2}});
            if(s2!=t2) opt.push_back({0,{id2,t2}});
            //id1 在 s2 且他要去 t1
            qS.push({{s2,t1},{id1,chifan[id1]}});
            qT.push({{t1,s2},{id1,chifan[id1]}});
            build();
        }else{
            if(s2>=t1){
                if(s2!=s1) opt.push_back({0,{id1,s2}});
                opt.push_back({1,{id1,id2}});
                if(s2!=t2) opt.push_back({0,{id2,t2}});
                //id1 在 s2 且他要去 t1
                qS.push({{s2,t1},{id1,chifan[id1]}});
                qT.push({{t1,s2},{id1,chifan[id1]}});
                build();
            }else{
                qS.push({{s2,t1},{id2,chifan[id2]}});
                qT.push({{t1,s2},{id2,chifan[id2]}});
                build();
                if(s1!=t1) opt.push_back({0,{id1,t1}});
                opt.push_back({1,{id1,id2}});
                if(t1!=t2) opt.push_back({0,{id2,t2}});
            }
        }
    }
}
void work(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) fa[i]=i;
    build(1,1,m);
    for(int i=1;i<=n;i++){
        cin>>s[i]>>t[i];
        sol(1,1,m,min(s[i],t[i]),max(s[i],t[i]),i);
    }
    pushdown(1,1,m);
    for(int i=1;i<=n;i++){
        S[found(i)].push_back(s[i]);
        T[found(i)].push_back(t[i]);
        Id[found(i)].push_back(i);
    }
    long long res=0;
    for(int i=1;i<=n;i++){
        if(found(i)==i){
            for(int z:Id[i]) chifan[z]=++tot,qS.push({{s[z],t[z]},{z,chifan[z]}}),qT.push({{t[z],s[z]},{z,chifan[z]}});
            build();
            tot=0;
            while(qS.size()>0) qS.pop();
            while(qT.size()>0) qT.pop();
            sort(S[i].begin(),S[i].end());
            sort(T[i].begin(),T[i].end());
            for(int j=0;j<S[i].size();j++) res+=abs(S[i][j]-T[i][j]);
        }
    }
    cout<<res<<" "<<opt.size()<<"\n";
    for(pair<int,pair<int,int> > now:opt){
        cout<<now.first<<" "<<now.second.first<<" "<<now.second.second<<"\n";
    }
    opt.clear();
    for(int i=0;i<=n;i++) fa[i]=i;
    for(int i=0;i<=n;i++){
        s[i]=t[i]=0;
        S[i].clear(),T[i].clear();
        Id[i].clear();
    }
    for(int i=0;i<(m<<2);i++){
        tag[i]=0;
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--) work();
    return 0;
}