Submission #546123

# Submission time Handle Problem Language Result Execution time Memory
546123 2022-04-06T12:17:18 Z leaked Reconstruction Project (JOI22_reconstruction) C++14
0 / 100
2876 ms 842504 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define m_p make_pair
#define fast_prep ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=501;
int p[N],sz[N];
void make(int v){
    p[v]=v;sz[v]=1;
}
int get(int v){
    return p[v]=(p[v]==v?v:get(p[v]));
}
bool mg(int a,int b){
    a=get(a),b=get(b);
    if(a==b) return 0;
    if(sz[a]<sz[b]) swap(a,b);
    p[b]=a;sz[a]+=sz[b];
    return 1;
}


signed main(){
    fast_prep;
    int n,m;
    cin>>n>>m;
    vec<array<int,3>> arr;
    for(int i=0;i<m;i++){
        int v,u,w;
        cin>>v>>u>>w;--v;--u;
        arr.pb({w,v,u});
    }
    arr.pb({(int)-1e9-10,0,0});
    sort(all(arr));m++;
    vec<vec<int>> lft(m+1);
    auto rgt=lft;
    /// let's try
    {
        vec<int> cur;
        for(int i=m-1;i>=0;i--){
            for(int j=0;j<n;j++) make(j);
            cur.insert(cur.begin(),i);
            for(auto &z : cur){
                if(mg(arr[z][1],arr[z][2]))
                    rgt[i].pb(z);
            }
            cur=rgt[i];
        }
    }
    {
        vec<int> cur;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++) make(j);
            cur.insert(cur.begin(),i);
            for(auto &z : cur){
                if(mg(arr[z][1],arr[z][2]))
                    lft[i].pb(z);
            }
            cur=lft[i];
        }
    }
    vec<int> kek;
//    for(int i=0;i<m;i++){
//        for(auto &z : lft[i])
//            cout<<z[0]<<' '<<z[1]<<' '<<z[2]<<endl;
//    }
    for(auto &z : arr) kek.pb(z[0]);
//    for(int i=1;i<m;i++)
//        kek.pb((arr[i][0]+arr[i][1])/2);
    for(int it=0;it<2;it++){
        sort(all(kek));kek.erase(unique(all(kek)),kek.end());
        vec<int> nkek=kek;
        for(int i=1;i<sz(kek);i++)
            nkek.pb((kek[i]+kek[i-1])/2);
        sort(all(nkek));nkek.erase(unique(all(nkek)),nkek.end());
        swap(kek,nkek);
//        cout<<"HEY "<<sz(kek)<<' '<<it<<endl;
//        kek.pb((arr[i][0]+arr[i][1])/2);

    }
    int j;
    vec<int> lower;
    vec<int> same;
    vec<ll> ans;
    arr.pb({(int)1e9+1,-1,-1});
    int q;
    cin>>q;

    vec<int> x(q);
    for(auto &z : x) cin>>z;
//    kek=x;
    vec<array<int,3>> my;
    auto stupid=[&](int x){
        my=arr;
        for(auto &z : my) z[0]=abs(x-z[0]);
        sort(all(my));
        for(int i=0;i<n;i++) make(i);
        ll ans=0;
        for(auto &z : my){
            if(mg(z[1],z[2]))
                ans+=z[0];
        }
        return ans;
    };
    for(int i=1;i<q;i++) assert(kek[i]>kek[i-1]);
    for(int j=1;j<m;j++) assert(arr[j][0]>=arr[j-1][0]);
    j=-1;
    for(auto &z : kek){
        while(j+1<m && arr[j+1][0]<=z) ++j;
//        cout<<"HEY "<<z<<' '<<arr[j][0]<<' '<<arr[j+1][0]<<' '<<z<<endl;
        ///j and j+1
        int f=0,s=0;
        for(int i=0;i<n;i++) make(i);
        int rem=n-1;
        int lw=0;
        ll cur=0;
        int _sam=0;
//        assert(arr[j+1][0]>z && arr[j][0]<=z);
        while(rem--){
            while(f!=sz(lft[j]) && get(arr[lft[j][f]][1])==get(arr[lft[j][f]][2])) ++f;
            while(s!=sz(rgt[j+1]) && get(arr[rgt[j+1][s]][1])==get(arr[rgt[j+1][s]][2])) ++s;
//            cout<<"HEY "<<f<<' '<<s<<' '<<sz(lft[j])<<' '<<sz(rgt[j+1])<<endl;
            if(s!=sz(rgt[j+1]) && (f==sz(lft[j]) || (z-arr[lft[j][f]][0])>(arr[rgt[j+1][s]][0]-z))){
                assert(mg(arr[rgt[j+1][s]][1],arr[rgt[j+1][s]][2]));
//                assert(rgt[j+1][s][0]>=z);

                cur+=arr[rgt[j+1][s]][0]-z;
                ++s;
            }
            else{
                assert(mg(arr[lft[j][f]][1],arr[lft[j][f]][2]));
                lw+=(z>arr[lft[j][f]][0]);
                _sam+=(z==arr[lft[j][f]][0]);
                cur+=z-arr[lft[j][f]][0];
                ++f;
            }
        }
//        cout<<"WOW "<<z<<' '<<cur<<' '<<lw<<endl;
        ans.pb(cur);
        same.pb(_sam);
        lower.pb(lw);
    }
//    int q;
//    cin>>q;
    for(int i=0;i<q;i++){
        int xt=x[i];
//        cin>>x;
        ll anst=1e18;
        int j=upper_bound(all(kek),xt)-kek.begin();
        if(j!=sz(kek)){
            umin(anst,ans[j]-1ll*lower[j]*abs(kek[j]-xt)+1ll*(n-1-lower[j])*abs(kek[j]-xt));
        }
        --j;
////        assert(kek[j]==xt);
         if(j!=sz(kek)){
            umin(anst,ans[j]+1ll*(lower[j]+same[j])*abs(kek[j]-xt)-1ll*(n-1-lower[j]-same[j])*abs(kek[j]-xt));
        }
////        assert(anst>=stupid(xt));
        cout<<anst<<'\n';
    }
    return 0;
}
/*
5 10
1 2 8
1 3 13
1 4 5
1 5 11
1 5 3
2 3 7
2 4 15
3 4 6
3 5 6
4 5 2
1
10
17
*/

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:106:10: warning: variable 'stupid' set but not used [-Wunused-but-set-variable]
  106 |     auto stupid=[&](int x){
      |          ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Runtime error 2876 ms 842504 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -