Submission #1338190

#TimeUsernameProblemLanguageResultExecution timeMemory
1338190shiba_ensureBridges (APIO19_bridges)C++20
100 / 100
1265 ms6700 KiB
#include <bits/stdc++.h>
#include <stdio.h>

#define __Shibae__      signed main()
#define IOS             ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fiopen(Path)    freopen(Path".INP", "r", stdin); freopen(Path".OUT", "w", stdout);
#define fipen(Path)     freopen(Path".INP", "r", stdin);
#define sz(s)           (int)s.size()
#define all(x)          x.begin(), x.end()
#define maxHeap         priority_queue<int>
#define minHeap         priority_queue<int, vector<int>, greater<int>>
#define getBit(x, k)    (((x) >> (k)) & 1)
#define MASK(i)         (1LL << (i))
#define SQR(x)          (1LL * ((x) * (x)))
#define db              double
#define ld              long double
#define ui              unsigned int
#define ll              long long
#define ii              pair<int, int>
#define pli             pair<ll, int>
#define pil             pair<int, ll>
#define pll             pair<ll, ll>
#define fi              first
#define se              second

#define FOR(i, a, b)    for(int i = a, _b = b; i <= _b; i += 1)
#define FOD(i, a, b)    for(int i = a, _b = b; i >= _b; i -= 1)
#define REP(i, a)       for(int i = 0, _a = a; i < _a; i++)
#define pb              push_back
#define fau(u, a)       for(auto &u : a)
#define debug           return cout << "debug", void();

using namespace std;

const ll mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int MAX = 5e5+5;

const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

ll Rand(ll l, ll r)
{
    return uniform_int_distribution<ll>(l, r)(rd);
}

template<class SHIBA, class ENGINE>
    bool minimize(SHIBA &x, const ENGINE y)
    {
        if(x > y)
        {
            x = y;
            return true;
        }
        else return false;
    }
template<class SHIBA, class ENGINE>
    bool maximize(SHIBA &x, const ENGINE y)
    {
        if(x < y)
        {
            x = y;
            return true;
        }
        else return false;
    }


/* Template by: Nguyen Nhat Anh from Luong Van Chanh High School for the gifted */
/* From Min Tuoi with love */
        /**       TRY HARD        **/
        /**          ORZ          **/

/* -----------------[ MAIN CODE ]----------------- */

struct edge
{
    int u, v, w;
}e[MAX];

struct query
{
    int t, id, w;
}qr[MAX];

int n, m, q;
vector<int> id;
bool is_changed[MAX];
int used[MAX];
int res[MAX];

struct DisjointSet
{
    int n;
    vector<int> par;
    vector<int> sz;
    stack<pair<int &, int>> history;

    DisjointSet() {}

    DisjointSet(int _n)
    {
        resize(_n);
    }

    void resize(int _n)
    {
        n = _n;
        sz.assign(n+3, 1);
        par.assign(n+3, 0);
        iota(all(par), 0);
    }

    int size(int u)
    {
        return sz[find_set(u)];
    }

    int find_set(int u)
    {
        while(u != par[u]) u = par[u];
        return u;
    }

    bool join(int u, int v)
    {
        u = find_set(u);
        v = find_set(v);

        if (u == v) return false;
        if (sz[u] < sz[v]) swap(u, v);

        history.push({par[v], par[v]});
        history.push({sz[u], sz[u]});

        par[v] = u;
        sz[u] += sz[v];
        return true;
    }

    int snapshot()
    {
        return history.size();
    }

    void rollback(int x)
    {
        while(snapshot() > x)
        {
            history.top().fi = history.top().se;
            history.pop();
        }
    }
}dsu;

void input()
{
    cin >> n >> m;

    FOR(i, 1, m)
    {
        cin >> e[i].u >> e[i].v >> e[i].w;
        id.pb(i);
    }

    cin >> q;
    REP(i, q)
    {
        cin >> qr[i].t >> qr[i].id >> qr[i].w;
    }
}

bool cmp(const int &x, const int &y)
{
    return e[x].w > e[y].w;
}

void solve()
{
    sort(all(id), cmp);
    dsu.resize(n);

    int bl = 1000;

    for (int _ = 0; _ * bl < q; _++)
    {
        int l = _ * bl;
        int r = min(l + bl - 1, q-1);

        dsu.rollback(0);

        vector<int> ans;

        FOD(i, r, l)
        {
            if (qr[i].t == 2) ans.pb(i);
            else is_changed[qr[i].id] = 1;
        }

        vector<int> same, change;
        REP(i, m) 
        {
            if (!is_changed[id[i]]) same.pb(id[i]);
            else change.pb(id[i]);
        }

        // fau(x, same) cout << x << " "; cout << "\n";
//        break;

        sort(all(ans), [&](const int &x, const int &y) {
            return qr[x].w > qr[y].w;
        });

        int j = 0;

        fau(cur, ans)
        {
            while(j < same.size() && e[same[j]].w >= qr[cur].w)
            {
                dsu.join(e[same[j]].u, e[same[j]].v);
                j++;
            }
            // cout << qr << " ";
            // cout << j << " ";
            int snap = dsu.snapshot();

            FOD(i, cur, l) if (qr[i].t == 1 && used[qr[i].id] == 0) used[qr[i].id] = qr[i].w;

            fau (i, change)
            {
                int w = (used[i] ? used[i] : e[i].w);
                if (w >= qr[cur].w) dsu.join(e[i].u, e[i].v);
            }

            res[cur] = dsu.size(qr[cur].id);
            FOR(i, l, cur) if (qr[i].t == 1) used[qr[i].id] = 0;
            dsu.rollback(snap);
        }
        FOR(i, l, r) if (qr[i].t == 1) 
        {
            is_changed[qr[i].id] = 0;
            e[qr[i].id].w = qr[i].w;
        }
        sort(all(change), cmp);
        id.clear();
        merge(all(same), all(change), back_inserter(id), cmp);
    }
    REP(i, q) if (qr[i].t == 2) cout << res[i] << "\n";
}

__Shibae__
{
    IOS
    // fipen("test")

    const bool multitest = 0;
    int tt = 1; if(multitest) cin >> tt;

    while( tt-- ){
        input();
        solve();
        if(tt) cout << "\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...