Submission #901637

# Submission time Handle Problem Language Result Execution time Memory
901637 2024-01-09T19:13:52 Z alexander707070 Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
1460 ms 68412 KB
#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;

const int inf=1e9;

struct edge{
    int from,to,cost;
};

int n,m,a,b,c,pt,q,pos,l,r,mid,lt,rt;
int dsu[MAXN],sz[MAXN];
int mins[MAXN],maxs[MAXN];
long long incr[MAXN],sum,ans[MAXN],mult[MAXN],t,x[MAXN];
edge e[MAXN];

bool cmp(edge fr,edge sc){
    return fr.cost<sc.cost;
}

void reset_dsu(){
    for(int i=1;i<=n;i++){
        dsu[i]=i; sz[i]=1;
    }
}

int root(int x){
    if(dsu[x]==x)return x;
    dsu[x]=root(dsu[x]);
    return dsu[x];
}

void mergev(int x,int y){
    int rootx=root(x);
    int rooty=root(y);

    if(rootx==rooty)return;
    if(sz[rootx]<sz[rooty])swap(rootx,rooty);

    dsu[rooty]=rootx;
    sz[rootx]+=sz[rooty];
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>a>>b>>c;
        e[i]={a,b,c};
    }

    cin>>q;
    for(int i=1;i<=q;i++){
        cin>>x[i];
    }

    sort(e+1,e+m+1,cmp);

    for(int i=1;i<=m;i++){
        reset_dsu();

        pos=0; mins[i]=0;
        for(int f=i-1;f>=1;f--){
            mergev(e[f].from,e[f].to);
            if(root(e[i].from)==root(e[i].to)){
                pos=f; break;
            }
        }

        if(pos!=0){
            mins[i]=(e[pos].cost+e[i].cost)/2;

            if((e[pos].cost+e[i].cost)%2==1){
                mins[i]++;
            }
        }

        reset_dsu();

        pos=m+1; maxs[i]=inf;
        for(int f=i+1;f<=m;f++){
            mergev(e[f].from,e[f].to);
            if(root(e[i].from)==root(e[i].to)){
                pos=f; break;
            }
        }

        if(pos!=m+1){
            maxs[i]=(e[pos].cost+e[i].cost)/2;
            if((e[pos].cost+e[i].cost)%2==0){
                maxs[i]--;
            }
        }
    }

    for(int i=1;i<=m;i++){
        l=0; r=q+1;

        while(l+1<r){
            mid=(l+r)/2;
            if(x[mid]>=mins[i]){
                r=mid;
            }else{
                l=mid;
            }
        }

        lt=r;

        l=0; r=q+1;

        while(l+1<r){
            mid=(l+r)/2;
            if(x[mid]<=maxs[i]){
                l=mid;
            }else{
                r=mid;
            }
        }

        rt=l;

        l=0; r=q+1;

        while(l+1<r){
            mid=(l+r)/2;
            if(x[mid]<=e[i].cost){
                l=mid;
            }else{
                r=mid;
            }
        }

        if(lt>rt)continue;

        incr[lt]+=e[i].cost;
        mult[lt]--;

        incr[l+1]+=-2*e[i].cost;
        mult[l+1]+=2;

        incr[rt+1]+=e[i].cost;
        mult[rt+1]--;
    }

    for(int i=1;i<=q;i++){
        sum+=incr[i]; 
        t+=mult[i];
        ans[i]=sum+t*x[i];
    }

    for(int i=1;i<=q;i++){
        cout<<ans[i]<<"\n";
    }

    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 2 ms 16732 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
6 Correct 2 ms 16732 KB Output is correct
7 Correct 2 ms 16728 KB Output is correct
8 Correct 2 ms 16732 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 2 ms 16732 KB Output is correct
11 Correct 2 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16732 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 2 ms 16732 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16732 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 2 ms 16732 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
6 Correct 2 ms 16732 KB Output is correct
7 Correct 2 ms 16728 KB Output is correct
8 Correct 2 ms 16732 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 2 ms 16732 KB Output is correct
11 Correct 2 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16732 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 2 ms 16732 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16732 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 16732 KB Output is correct
20 Correct 1220 ms 21376 KB Output is correct
21 Correct 899 ms 21124 KB Output is correct
22 Correct 949 ms 21116 KB Output is correct
23 Correct 1096 ms 21116 KB Output is correct
24 Correct 1250 ms 21372 KB Output is correct
25 Correct 1174 ms 21372 KB Output is correct
26 Correct 1228 ms 21120 KB Output is correct
27 Correct 1225 ms 21104 KB Output is correct
28 Correct 1255 ms 21124 KB Output is correct
29 Correct 1255 ms 21120 KB Output is correct
30 Correct 1225 ms 21368 KB Output is correct
31 Correct 1222 ms 21120 KB Output is correct
32 Correct 1231 ms 21372 KB Output is correct
33 Correct 1224 ms 21124 KB Output is correct
34 Correct 1250 ms 21124 KB Output is correct
35 Correct 1236 ms 21128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 1236 ms 58236 KB Output is correct
5 Correct 1234 ms 66152 KB Output is correct
6 Correct 1223 ms 66176 KB Output is correct
7 Correct 700 ms 68076 KB Output is correct
8 Correct 517 ms 68180 KB Output is correct
9 Correct 449 ms 68412 KB Output is correct
10 Correct 1207 ms 66168 KB Output is correct
11 Correct 504 ms 66164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 2 ms 16732 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
6 Correct 2 ms 16732 KB Output is correct
7 Correct 2 ms 16728 KB Output is correct
8 Correct 2 ms 16732 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 2 ms 16732 KB Output is correct
11 Correct 2 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16732 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 2 ms 16732 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16732 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 16732 KB Output is correct
20 Correct 153 ms 55264 KB Output is correct
21 Correct 155 ms 61388 KB Output is correct
22 Correct 162 ms 61388 KB Output is correct
23 Correct 162 ms 61268 KB Output is correct
24 Correct 158 ms 61396 KB Output is correct
25 Correct 155 ms 61264 KB Output is correct
26 Correct 149 ms 61248 KB Output is correct
27 Correct 158 ms 61396 KB Output is correct
28 Correct 152 ms 61128 KB Output is correct
29 Correct 148 ms 61372 KB Output is correct
30 Correct 179 ms 61376 KB Output is correct
31 Correct 166 ms 61264 KB Output is correct
32 Correct 148 ms 51520 KB Output is correct
33 Correct 157 ms 59008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 2 ms 16732 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
6 Correct 2 ms 16732 KB Output is correct
7 Correct 2 ms 16728 KB Output is correct
8 Correct 2 ms 16732 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 2 ms 16732 KB Output is correct
11 Correct 2 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16732 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 2 ms 16732 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16732 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 16732 KB Output is correct
20 Correct 1220 ms 21376 KB Output is correct
21 Correct 899 ms 21124 KB Output is correct
22 Correct 949 ms 21116 KB Output is correct
23 Correct 1096 ms 21116 KB Output is correct
24 Correct 1250 ms 21372 KB Output is correct
25 Correct 1174 ms 21372 KB Output is correct
26 Correct 1228 ms 21120 KB Output is correct
27 Correct 1225 ms 21104 KB Output is correct
28 Correct 1255 ms 21124 KB Output is correct
29 Correct 1255 ms 21120 KB Output is correct
30 Correct 1225 ms 21368 KB Output is correct
31 Correct 1222 ms 21120 KB Output is correct
32 Correct 1231 ms 21372 KB Output is correct
33 Correct 1224 ms 21124 KB Output is correct
34 Correct 1250 ms 21124 KB Output is correct
35 Correct 1236 ms 21128 KB Output is correct
36 Correct 1244 ms 21120 KB Output is correct
37 Correct 915 ms 21380 KB Output is correct
38 Correct 957 ms 21128 KB Output is correct
39 Correct 1099 ms 21120 KB Output is correct
40 Correct 1231 ms 21328 KB Output is correct
41 Correct 1196 ms 21372 KB Output is correct
42 Correct 1241 ms 21120 KB Output is correct
43 Correct 1299 ms 21332 KB Output is correct
44 Correct 1253 ms 21116 KB Output is correct
45 Correct 1236 ms 21120 KB Output is correct
46 Correct 1241 ms 21336 KB Output is correct
47 Correct 1261 ms 21384 KB Output is correct
48 Correct 1236 ms 21128 KB Output is correct
49 Correct 1248 ms 21120 KB Output is correct
50 Correct 1229 ms 21376 KB Output is correct
51 Correct 1280 ms 21120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 2 ms 16732 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
6 Correct 2 ms 16732 KB Output is correct
7 Correct 2 ms 16728 KB Output is correct
8 Correct 2 ms 16732 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 2 ms 16732 KB Output is correct
11 Correct 2 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16732 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 2 ms 16732 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16732 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 16732 KB Output is correct
20 Correct 1220 ms 21376 KB Output is correct
21 Correct 899 ms 21124 KB Output is correct
22 Correct 949 ms 21116 KB Output is correct
23 Correct 1096 ms 21116 KB Output is correct
24 Correct 1250 ms 21372 KB Output is correct
25 Correct 1174 ms 21372 KB Output is correct
26 Correct 1228 ms 21120 KB Output is correct
27 Correct 1225 ms 21104 KB Output is correct
28 Correct 1255 ms 21124 KB Output is correct
29 Correct 1255 ms 21120 KB Output is correct
30 Correct 1225 ms 21368 KB Output is correct
31 Correct 1222 ms 21120 KB Output is correct
32 Correct 1231 ms 21372 KB Output is correct
33 Correct 1224 ms 21124 KB Output is correct
34 Correct 1250 ms 21124 KB Output is correct
35 Correct 1236 ms 21128 KB Output is correct
36 Correct 2 ms 16732 KB Output is correct
37 Correct 2 ms 16732 KB Output is correct
38 Correct 2 ms 16732 KB Output is correct
39 Correct 1236 ms 58236 KB Output is correct
40 Correct 1234 ms 66152 KB Output is correct
41 Correct 1223 ms 66176 KB Output is correct
42 Correct 700 ms 68076 KB Output is correct
43 Correct 517 ms 68180 KB Output is correct
44 Correct 449 ms 68412 KB Output is correct
45 Correct 1207 ms 66168 KB Output is correct
46 Correct 504 ms 66164 KB Output is correct
47 Correct 2 ms 16732 KB Output is correct
48 Correct 153 ms 55264 KB Output is correct
49 Correct 155 ms 61388 KB Output is correct
50 Correct 162 ms 61388 KB Output is correct
51 Correct 162 ms 61268 KB Output is correct
52 Correct 158 ms 61396 KB Output is correct
53 Correct 155 ms 61264 KB Output is correct
54 Correct 149 ms 61248 KB Output is correct
55 Correct 158 ms 61396 KB Output is correct
56 Correct 152 ms 61128 KB Output is correct
57 Correct 148 ms 61372 KB Output is correct
58 Correct 179 ms 61376 KB Output is correct
59 Correct 166 ms 61264 KB Output is correct
60 Correct 148 ms 51520 KB Output is correct
61 Correct 157 ms 59008 KB Output is correct
62 Correct 1244 ms 21120 KB Output is correct
63 Correct 915 ms 21380 KB Output is correct
64 Correct 957 ms 21128 KB Output is correct
65 Correct 1099 ms 21120 KB Output is correct
66 Correct 1231 ms 21328 KB Output is correct
67 Correct 1196 ms 21372 KB Output is correct
68 Correct 1241 ms 21120 KB Output is correct
69 Correct 1299 ms 21332 KB Output is correct
70 Correct 1253 ms 21116 KB Output is correct
71 Correct 1236 ms 21120 KB Output is correct
72 Correct 1241 ms 21336 KB Output is correct
73 Correct 1261 ms 21384 KB Output is correct
74 Correct 1236 ms 21128 KB Output is correct
75 Correct 1248 ms 21120 KB Output is correct
76 Correct 1229 ms 21376 KB Output is correct
77 Correct 1280 ms 21120 KB Output is correct
78 Correct 1418 ms 65300 KB Output is correct
79 Correct 1064 ms 67116 KB Output is correct
80 Correct 1118 ms 66064 KB Output is correct
81 Correct 1232 ms 66132 KB Output is correct
82 Correct 1390 ms 65044 KB Output is correct
83 Correct 1350 ms 65044 KB Output is correct
84 Correct 1394 ms 65048 KB Output is correct
85 Correct 1392 ms 65044 KB Output is correct
86 Correct 1419 ms 65036 KB Output is correct
87 Correct 1371 ms 66640 KB Output is correct
88 Correct 1381 ms 65044 KB Output is correct
89 Correct 1405 ms 65044 KB Output is correct
90 Correct 1399 ms 65152 KB Output is correct
91 Correct 1460 ms 64932 KB Output is correct
92 Correct 1388 ms 57856 KB Output is correct
93 Correct 1402 ms 66128 KB Output is correct