Submission #1357915

#TimeUsernameProblemLanguageResultExecution timeMemory
1357915mahribanWind Turbines (EGOI25_windturbines)C++20
0 / 100
4094 ms12928 KiB
#include<bits/stdc++.h>
using namespace std;
vector<int> E[1000009];
int vis[1000009];
int h[1000009];
int main(){
    int n, m, q;
    vector<pair<int, pair<int, int>>> p;
    cin >> n >> m >> q;
    for (int a, b, c, i = 1; i <= m; i ++){
        cin >> a >> b >> c;
        p.push_back({c, {a, b}});
    }
    int x, y;
    sort(p.begin(), p.end());
    while (q --){
        long long ans = 0;
        cin >> x >> y;
        for (int i = x; i <= y; i ++)vis[i] = 1;
        int ind = 1;
        for (auto i : p){
            if (vis[i.second.first] == 1 && vis[i.second.second] == 1)continue;
            if (vis[i.second.first] == 1 || vis[i.second.second] == 1){
                ans += i.first;
                if (vis[i.second.first] == 0)vis[i.second.first] = 1;
                if (vis[i.second.second] == 0)vis[i.second.second] = 1;
                if (vis[i.second.first] == 1 && vis[i.second.second] == 2){
                    for (auto j : E[h[i.second.second]]){
                        vis[j] = 1;
                        h[j] = 0;
                    }
                }
                else{
                    for (auto j : E[h[i.second.first]]){
                        vis[j] = 1;
                        h[j] = 0;
                    }
                }
            }
            else {
                ans += i.first;
//                cout << "#";
                if (!vis[i.second.first] && !vis[i.second.second]){
//                    cout << "$";
                    E[ind].push_back(i.second.first);
                    E[ind].push_back(i.second.second);
                    vis[i.second.first] = 2;
                    vis[i.second.second] = 2;
                    h[i.second.first] = ind;
                    h[i.second.second] = ind;
                    ind ++;
                }
                else if (vis[i.second.first] == vis[i.second.second] == 2){
                    if (h[i.second.first] == h[i.second.first])continue;
                    int a = h[i.second.first];
                    int b = h[i.second.second];
                    if (E[a].size() > E[b].size())swap(a, b);
                    for (auto j : E[a]){
                        E[b].push_back(j);
                        h[i.second.second] = h[i.second.first] = b;
                    }
                }
                else if (vis[i.second.first] == 2){
                    E[h[i.second.first]].push_back(i.second.second);
                    h[i.second.second] = h[i.second.first];
                    vis[i.second.second] = 2;
                }
                else if (vis[i.second.second] == 2){
                    E[h[i.second.second]].push_back(i.second.first);
                    h[i.second.first] = h[i.second.second];
                    vis[i.second.first] = 2;
                }
            }
        }
        for (int i = 0; i < 1000009; i ++){
            vis[i] = h[i] = 0;
            E[i].clear();
        }
        cout << ans << endl;
    }
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...