Submission #1340939

#TimeUsernameProblemLanguageResultExecution timeMemory
1340939nguyenkhangninh99Reconstruction Project (JOI22_reconstruction)C++20
100 / 100
891 ms24572 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 1e5 + 5, inf = 2e18;
int p[505];

int find(int u){
    return (p[u] == u ? u : p[u] = find(p[u]));
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;

    vector<array<int, 3>> edge(m + 1);
    for(int i = 1; i <= m; i++) cin >> edge[i][1] >> edge[i][2] >> edge[i][0];


    sort(edge.begin() + 1, edge.end());

    vector<int> l(m + 1, -inf), r(m + 1, inf);

    for(int i = 1; i <= m; i++){
        for(int k = 1; k <= n; k++) p[k] = k;
        for(int j = i - 1; j >= 1; j--){
            p[find(edge[j][1])] = find(edge[j][2]);
            if(find(edge[i][1]) == find(edge[i][2])){
                l[i] = (edge[j][0] + edge[i][0]) / 2 + 1;
                r[j] = (edge[j][0] + edge[i][0]) / 2;
                break;
            }
        }
    }

    vector<array<int, 3>> banhhe;
    for(int i = 1; i <= m; i++){
        banhhe.push_back({l[i], -1, edge[i][0]});
        banhhe.push_back({edge[i][0], 2, -2 * edge[i][0]});
        banhhe.push_back({r[i] + 1, -1, edge[i][0]});
    }

    sort(banhhe.begin(), banhhe.end());

    int q; cin >> q;
    int s1 = 0, s2 = 0, ptr = 0;

    while(q--){
        int x; cin >> x;
        while(ptr < banhhe.size() && banhhe[ptr][0] <= x){
            s1 += banhhe[ptr][1];
            s2 += banhhe[ptr][2];
            ptr++;
        }
        cout << s1 * x + s2 << '\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...