Submission #1126313

#TimeUsernameProblemLanguageResultExecution timeMemory
1126313mariaclaraReconstruction Project (JOI22_reconstruction)C++17
100 / 100
2628 ms17912 KiB
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tuple<int,int,int> trio;
const int MAXN = 1e5+5;
#define W(x) get<0>(x)
#define U(x) get<1>(x)
#define V(x) get<2>(x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair 
#define pb push_back 
#define fr first 
#define sc second 

int n, m, q, tam[505], L[MAXN], R[MAXN];
vector<trio> edges, op;
vector<vector<pii>> graph;

int dfs(int no, int fim, int pai, int ind, int flag) {
    if(no == fim) return ind;

    for(auto [viz, i] : graph[no]) {
        if(viz == pai) continue;
        int new_ind = ind;
        if(W(edges[i])*flag < W(edges[ind])*flag) new_ind = i;
        
        int ret = dfs(viz, fim, no, new_ind, flag);
        if(ret != -1) return ret;
    }

    return -1;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    for(int i = 1, a, b, w; i <= m; i++) {
        cin >> a >> b >> w;
        edges.pb({w,a,b});
    }

    sort(all(edges));
    graph.resize(n+1);
    fill(R, R+m, 1e9);

    for(int i = 0; i < m; i++) {
        auto [w, a, b] = edges[i];

        // se existe um caminho entre a e b
        // removemos a menor aresta nele

        int sub = dfs(a, b, 0, i, 1);
        graph[a].pb({b, i});
        graph[b].pb({a, i});
        
        if(sub == -1)  continue;

        auto [w1, a1, b1] = edges[sub];
        R[sub] = (w1 + w - 1)/2;

        for(int j = 0; j < sz(graph[a1]); j++)
            if(graph[a1][j].sc == sub) {
                graph[a1].erase({graph[a1].begin() + j});
                break;
            }

        for(int j = 0; j < sz(graph[b1]); j++)
            if(graph[b1][j].sc == sub) {
                graph[b1].erase({graph[b1].begin() + j});
                break;
            }
    }

    graph.clear();
    graph.resize(n+1);

    for(int i = m-1; i >= 0; i--) {
        auto [w, a, b] = edges[i];

        // se existe um caminho entre a e b
        // removemos a menor aresta nele

        int sub = dfs(a, b, 0, i, -1);
        graph[a].pb({b, i});
        graph[b].pb({a, i});
        
        if(sub == -1)  continue;

        auto [w1, a1, b1] = edges[sub];
        L[sub] = (w + w1 + 1)/2;

        for(int j = 0; j < sz(graph[a1]); j++)
            if(graph[a1][j].sc == sub) {
                graph[a1].erase({graph[a1].begin() + j});
                break;
            }

        for(int j = 0; j < sz(graph[b1]); j++)
            if(graph[b1][j].sc == sub) {
                graph[b1].erase({graph[b1].begin() + j});
                break;
            }
    }

    set<int> active;
    for(int i = 0; i < m; i++) {
        if(L[i] > R[i]) continue;
        op.pb({L[i], i, 1});
        op.pb({R[i]+1, i, 0});
    }

    sort(all(op));

    cin >> q;

    int pnt = 0, count = 0;
    auto ptr = active.begin();
    ll SUM = 0, sum_first = 0, w;

    while(q--) {
        cin >> w;

        bool change = 0;
        while(pnt < sz(op) and W(op[pnt]) <= w) {
            change = 1;
            if(V(op[pnt]) == 1) active.insert(U(op[pnt])), SUM += W(edges[U(op[pnt])]);
            else active.erase(U(op[pnt])), SUM -= W(edges[U(op[pnt])]);
            pnt++;
        }

        if(change) ptr = active.begin(), count = 0, sum_first = 0;

        for( ; ptr != active.end(); ptr++, count++) {
            if(W(edges[*ptr]) > w) break;
            sum_first += W(edges[*ptr]);
        }

        cout << SUM - 2*sum_first + w*(2*count - n + 1) << endl;
    }
}
#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...