제출 #1332556

#제출 시각아이디문제언어결과실행 시간메모리
1332556anteknneSecurity Guard (JOI23_guard)C++20
100 / 100
548 ms99212 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const int MAXN = 200 * 1000 + 17;
ll s[MAXN];
vector<pair<ll, pii>> kraw;
int ojc[MAXN];
int roz[MAXN];
ll twyn[MAXN];
vector<pair<ll, pii>> mst;
int ojc2[MAXN];
priority_queue<pair<ll, pii>> wych[MAXN];
multiset<pair<ll, pii>> pq;
ll tminn[MAXN];

int findd (int x) {
    if (ojc[x] != x) {
        ojc[x] = findd(ojc[x]);
    }
    return ojc[x];
}

void unionn (int x, int y) {
    x = findd(x);
    y = findd(y);
    if (x == y) {
        return;
    }
    if (roz[x] < roz[y]) {
        swap(x, y);
    }
    ojc[y] = x;
    roz[x] += roz[y];
}

int findd2 (int x) {
    if (ojc2[x] != x) {
        ojc2[x] = findd2(ojc2[x]);
    }
    return ojc2[x];
}

void unionn2 (int x, int y) {
    x = findd2(x);
    y = findd2(y);
    if (x == y) {
        return;
    }
    if (int(wych[x].size()) < int(wych[y].size())) {
        swap(x, y);
    }

    if (!wych[x].empty()) {
        pair<ll, pii> el = wych[x].top();
        el.st *= (-1LL);
        el.st -= tminn[x];
        el.st *= (-1LL);
        pq.erase(pq.find(el));
    }

    if (!wych[y].empty()) {
        pair<ll, pii> el = wych[y].top();
        el.st *= (-1LL);
        el.st -= tminn[y];
        el.st *= (-1LL);
        pq.erase(pq.find(el));
    }

    while (!wych[y].empty()) {
        wych[x].push(wych[y].top());
        wych[y].pop();
    }

    tminn[x] = min(tminn[x], tminn[y]);
    if (!wych[x].empty()) {
        pair<ll, pii> el = wych[x].top();
        el.st *= (-1LL);
        el.st -= tminn[x];
        el.st *= (-1LL);
        pq.insert(el);
    }
    ojc2[y] = x;
}

int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

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

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

    int a, b;
    for (int i = 0; i < m; i ++) {
        cin >> a >> b;
        kraw.pb({s[a] + s[b], {a, b}});
    }

    for (int i = 1; i <= n; i ++) {
        ojc[i] = i;
        roz[i] = 1;
    }

    ll wyn = 0;
    ll minn = s[1];
    for (int i = 1; i <= n; i ++) {
        minn = min(minn, s[i]);
        wyn = max(wyn, s[i]);
    }

    wyn += ll(n - 2) * minn;

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

    for (auto x : kraw) {
        if (findd(x.nd.st) != findd(x.nd.nd)) {
            unionn(x.nd.st, x.nd.nd);
            mst.pb(x);
        }
    }

    for (int i = n - 1; i <= q; i ++) {
        twyn[i] = wyn;
    }

    for (int i = 1; i <= n; i ++) {
        ojc2[i] = i;
        tminn[i] = s[i];
    }
    for (auto x : mst) {
        wych[x.nd.st].push({(-1LL) * (x.st), x.nd});
        wych[x.nd.nd].push({(-1LL) * (x.st), x.nd});
    }

    for (int i = 1; i <= n; i ++) {
        pair<ll, pii> el = wych[i].top();
        el.st *= (-1LL);
        el.st -= tminn[i];
        el.st *= (-1LL);
        pq.insert(el);
    }

    for (int i = n - 2; i >= 0; i --) {
        wyn -= minn;
        while (true) {
            pair<ll, pii> x = *pq.rbegin();
            if (findd2(x.nd.st) != findd2(x.nd.nd)) {
                x.st *= (-1LL);
                wyn += x.st;
                unionn2(x.nd.st, x.nd.nd);
                twyn[i] = wyn;
                break;
            } else {
                pq.erase(pq.find(x));
                int v = findd2(x.nd.st);
                wych[v].pop();
                if (!wych[v].empty()) {
                    pair<ll, pii> el = wych[v].top();
                    el.st *= (-1LL);
                    el.st -= tminn[v];
                    el.st *= (-1LL);
                    pq.insert(el);
                }
            }
        }
    }

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

    return 0;
}

#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...
#Verdict Execution timeMemoryGrader output
Fetching results...