Submission #684793

#TimeUsernameProblemLanguageResultExecution timeMemory
684793vovamrBridges (APIO19_bridges)C++17
59 / 100
3070 ms158804 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

const int N = 5e4 + 100;

int pr[N], rnk[N];
inline void build(int n) {
	for (int i = 0; i < n; ++i) {
		pr[i] = i, rnk[i] = 1;
	}
}
inline int par(int v) {
	if (pr[v] == v) return v;
	return (pr[v] = par(pr[v]));
}
inline void uni(int a, int b) {
	a = par(a), b = par(b);
	if (a == b) return;
	if (rnk[a] > rnk[b]) swap(a, b);
	pr[a] = b, rnk[b] += rnk[a];
}

int bg = 0, en = 0;
int Qe[N];

int used[N];
ve<int> gr[N];

inline int bfs(int v) {
	int ans = 0;
	used[v] = 1; bg = en = 0;
	Qe[en++] = v;
	while (bg != en) {
		int v = Qe[bg++];
		ans += rnk[v];
		for (const auto &to : gr[v]) {
			if (used[to]) continue;
			used[to] = 1;
			Qe[en++] = to;
		}
	}
	return ans;
}

constexpr int b = 350;

const int M = 1e5 + 10;
const int Q = 1e5 + 10;

array<int,3> op[Q];
array<int,3> e[M];

array<int,3> que[Q]; int pq = 0;
array<int,3> nch[M]; int p = 0;
ve<array<int,3>> edges[Q];

bool used_in_block[M];

inline void solve() {
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < m; ++i) {
		int v, u, w;
		cin >> v >> u >> w, --v, --u;
		e[i] = {v, u, -w};
	}

	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int t;
		cin >> t;
		if (t == 1) {
			int id, w;
			cin >> id >> w;
			op[i] = {1, --id, -w};
		}
		else {
			int v, w;
			cin >> v >> w;
			op[i] = {2, --v, -w};
		}
	}

	ve<int> answer(q, -1);
	for (int l = 0; l < q; l += b) {

		build(n);
		int r = min(q - 1, l + b - 1);

		for (int i = l; i <= r; ++i) {
			const auto &[type, a, b] = op[i];
			if (type == 1) used_in_block[a] = 1;
		}

		p = 0;
		for (int i = 0; i < m; ++i) {
			if (!used_in_block[i]) {
				const auto &[v, u, w] = e[i];
				nch[p++] = {w, v, u};
			}
		}

		pq = 0;
		for (int i = l; i <= r; ++i) {
			const auto &[type, a, b] = op[i];
			if (type == 2) {
				que[pq++] = {b, a, i};
				for (int j = l; j <= r; ++j) {
					const auto &[tj, aj, bj] = op[j];
					if (tj == 1) edges[i].pb(array<int,3>{e[aj][0], e[aj][1], e[aj][2]});
				}
			}
			else {
				e[a][2] = b;
			}
		}

		sort(que, que + pq);
		sort(nch, nch + p);

		int ptr = 0;
		for (int id = 0; id < pq; ++id) {
			const auto &[w, v, i] = que[id];
			while (ptr < p && nch[ptr][0] <= w) {
				uni(nch[ptr][1], nch[ptr][2]);
				++ptr;
			}

			for (auto &[a, b, W] : edges[i]) {
				if (W > w) continue;
				a = par(a), b = par(b);
				gr[a].pb(b);
				gr[b].pb(a);
			}

			answer[i] = bfs(par(v));
			used[par(v)] = 0;

			for (auto &[a, b, w] : edges[i]) {
				gr[a].clear(); gr[a].shrink_to_fit();
				gr[b].clear(); gr[b].shrink_to_fit();
				used[a] = used[b] = 0;
			}
		}

		for (int i = l; i <= r; ++i) {
			const auto &[type, a, b] = op[i];
			if (type == 1) used_in_block[a] = 0;
		}
	}

	for (int i = 0; i < q; ++i) if (~answer[i]) cout << answer[i] << '\n';
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << 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...