Submission #737836

#TimeUsernameProblemLanguageResultExecution timeMemory
737836DeepessonReconstruction Project (JOI22_reconstruction)C++17
42 / 100
5034 ms235952 KiB
#include <bits/stdc++.h>
#define MAX 505
typedef std::pair<int,int> pii;
typedef std::pair<int,pii> pip;
int pai[MAX],size[MAX];
void limpa(void){
    for(int i=0;i!=MAX;++i){
        size[i]=1;
        pai[i]=i;
    }
}
int find(int a){
    if(pai[a]==a)
        return a;
    return pai[a]=find(pai[a]);
}
bool Union(int a,int b){
    a=find(a);
    b=find(b);
    if(a!=b){
        if(size[a]>size[b])std::swap(a,b);
        pai[a]=b;
        size[b]+=size[a];
        return 1;
    }
    return 0;
}
std::vector<pip> con;
using ll = long long;
ll ans[1100000];
std::vector<pii> contratos;
int N,M,Q;
std::vector<int> salva1[105000];
std::vector<pii> queries[105000];
void simula(void){
    std::vector<int> indices;
    for(int i=0;i!=M;++i){
        indices.push_back(i);
        int count=0;
        std::vector<int> novind;
        limpa();
        for(int j=indices.size()-1;j!=-1;--j){
            const int ref = indices[j];
            const int c = Union(con[ref].second.first,con[ref].second.second);
            if(c){
                ++count;
                novind.push_back(ref);
            }
        }
        std::reverse(novind.begin(),novind.end());
        indices=novind;
        salva1[i]=indices;
    }
    int cur=0;
    for(int i=0;i!=Q;++i){
        const pii ref = contratos[i];
        while((cur+1!=M)&&(abs(ref.first-con[cur].first)>=abs(ref.first-con[cur+1].first))){
            ++cur;
        }
        queries[cur].push_back(ref);
    }
    indices.clear();
    for(int i=M-1;i!=-1;--i){
        indices.push_back(i);
        int count=0;
        std::vector<int> novind;
        limpa();
        for(int j=indices.size()-1;j!=-1;--j){
            const int ref = indices[j];
            const int c = Union(con[ref].second.first,con[ref].second.second);
            if(c){
                ++count;
                novind.push_back(ref);
            }
        }
        std::reverse(novind.begin(),novind.end());
        indices=novind;
        for(auto&x:queries[i]){
            {
                int cur1=novind.size()-1,cur2=salva1[i].size()-1;
                int count=0;
                ll custo=0;
                limpa();
                for(;;){
                    pip add;
                    if(cur1>=0&&(cur2<0||abs(con[novind[cur1]].first-x.first)<abs(con[salva1[i][cur2]].first-x.first))){
                        add = con[novind[cur1]];
                        --cur1;
                    }else {
                        add = con[salva1[i][cur2]];
                        --cur2;
                    }
                    {
                        int c = Union(add.second.first,add.second.second);
                        if(c){
                            ++count;
                            custo += abs(add.first-x.first);
                        }
                    }
                    if(count==N-1)break;
                }
                ans[x.second]=custo;
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin>>N>>M;
    for(int i=0;i!=M;++i){
        int a,b,c;
        std::cin>>a>>b>>c;
        --a;--b;
        con.push_back({c,{a,b}});
    }
    std::sort(con.begin(),con.end());
    std::cin>>Q;
    for(int i=0;i!=Q;++i){
        int x;
        std::cin>>x;
        contratos.push_back({x,i});
    }
    std::sort(contratos.begin(),contratos.end());
    simula();
    for(int i=0;i!=Q;++i){
        std::cout<<ans[i]<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...