Submission #1276692

#TimeUsernameProblemLanguageResultExecution timeMemory
1276692wedonttalkanymore다리 (APIO19_bridges)C++20
59 / 100
3095 ms12256 KiB
#include <bits/stdc++.h>
/*
    Brute to win
*/
using namespace std;
using ll = long long;

#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second

const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;

int n, m, q;
int u[N], v[N], w[N];
struct queries {
    int type, a, b;
};
queries query[N];
int L[N], R[N];
int ptr;
int ans[N];
vector <int> need[N];

void preprocess() {
    for (int i = 1; i <= q; i++) {
        if (L[i / block] == 0) L[i / block] = i;
        R[i / block] = i;
    }
}

struct DSU {
    int par[N], sz[N];
    pii s[N];
    void make() {
        for (int i = 1; i <= n; i++) {
            par[i] = i;
            sz[i] = 1;
        }
    }
    int find(int u) {
        if (u == par[u]) return u;
        return find(par[u]);
    }
    void join(int u, int v) {
    	u = find(u), v = find(v);
        if (u != v) {
            if (sz[u] < sz[v]) swap(u, v);
            s[++ptr] = make_pair(sz[v], v);
            par[v] = u;
            sz[u] += sz[v];
        }
    }
    void rollback(int pos) {
        while(ptr > pos) {
            auto [val, v] = s[ptr];
            int u = par[v];
            sz[u] -= val;
            sz[v] = val;
            par[v] = v;
            --ptr;
        }
    }
};

DSU dsu;

bool cmp(int a, int b) {
    return w[a] > w[b];
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> n >> m;
    for (int i = 1; i <= m; i++) cin >> u[i] >> v[i] >> w[i];
    cin >> q;
    for (int i = 1; i <= q; i++) {
        cin >> query[i].type >> query[i].a >> query[i].b;
    }
    preprocess();
    int bls = q / block;
//    cout << bls << '\n';
    for (int i = 0; i <= bls; i++) {
    	ptr = 0;
        dsu.make();
        vector <int> unchange;
        vector <pii> qry;
        vector <int> check(m + 1, 0);
        vector <int> upd;
//        cout << L[i] << ' ' << R[i] << '\n';
        for (int j = L[i]; j <= R[i]; j++) {
            if (query[j].type == 1) {
//            	cout << query[j].a << ' ';
                check[query[j].a] = 1;
                upd.push_back(j);
            }
            else qry.emplace_back(query[j].b, j);
        }
        for (int j = 1; j <= m; j++) if (!check[j]) unchange.emplace_back(j);
        for (int j = L[i]; j <= R[i]; j++) {
        	if (query[j].type == 1) {
        		w[query[j].a] = query[j].b;
			}
			else {
				need[j - L[i]].clear();
				for (auto x : upd) {
					if (w[query[x].a] >= query[j].b) need[j - L[i]].push_back(x);
				}
			}
		}
        sort(unchange.begin(), unchange.end(), cmp);
        sort(qry.begin(), qry.end(), greater <pii>());
//        cout << "ok: ";
//        for (auto x : unchange) cout << x << ' ';
//        cout << '\n';
        int l = 0;
        for (int j = 0; j < qry.size(); j++) {
            while(l < unchange.size()) {
                int t = unchange[l];
//                cout << "at: " << j << ' ' << t << ' ' << w[t] << '\n';
                if (w[t] < qry[j].fi) break;
                dsu.join(u[t], v[t]);
                l++;
            }
            int pre = ptr;
            for (auto x : need[qry[j].se - L[i]]) {
            	int idx = query[x].a;
            	dsu.join(u[idx], v[idx]);
			}
            int st = query[qry[j].se].a;
            int parent = dsu.find(st);
            ans[qry[j].se] = dsu.sz[parent];
            dsu.rollback(pre);
        }
//        for (auto x : upd) w[x.fi.fi] = x.fi.se;
    }
    for (int i = 1; i <= q; i++) if (query[i].type == 2) cout << ans[i] << '\n';
    return 0;
}

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
bridges.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...