Submission #568330

#TimeUsernameProblemLanguageResultExecution timeMemory
5683308e7Bridges (APIO19_bridges)C++17
100 / 100
2697 ms8008 KiB
//Challenge: Accepted
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T,class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30, bs = 450;

pii ed[maxn];
int wei[maxn];
struct DSU{
	int par[maxn], siz[maxn];
	vector<pii> op;
	void init(int n) {
		op.clear();
		for (int i = 1;i <= n;i++) par[i] = i, siz[i] = 1;
	}
	int find(int a) {
		return a == par[a] ? a : find(par[a]);
	}
	void Union(int a, int b, bool rec) {
		a = find(a), b = find(b);
		if (a == b) return;
		if (siz[a] < siz[b]) swap(a, b);
		par[b] = a;
		siz[a] += siz[b];
		if (rec) op.push_back({a, b}); //ss -> ff
	}
	void undo() {
		reverse(op.begin(), op.end());
		for (auto [a, b]:op) {
			par[b] = b;
			siz[a] -= siz[b];
		}
		op.clear();
	}
} d;
int ans[maxn];
struct obj{
	int type, x, y, id;
	obj(){type = x = y = id = 0;}
	obj(int a, int b, int c, int D){type = a, x = b, y = c, id = D;}
};
bool ch[maxn];
int main() {
	io
	int n, m;
	cin >> n >> m;
	for (int i = 0;i < m;i++) {
		int u, v, w;
		cin >> u >> v >> w;
		ed[i] = {u, v};
		wei[i] = w;
	}
	
	int q;
	cin >> q;
	vector<obj> bat;
	auto solve = [&] () {
		vector<pii> e;
		vector<obj> que, change;
		vector<pii> mod;
		for (auto ev:bat) {
			if (ev.type == 1) {
				ch[ev.x] = 1;	
				change.push_back(ev);
			} else {
				que.push_back(ev);
			}
		}
		sort(que.begin(), que.end(), [&] (obj x, obj y){return x.y > y.y;});
		for (int i = 0;i < m;i++) {
			if (!ch[i]) e.push_back({wei[i], i});
			else mod.push_back({i, wei[i]});
		}
		sort(e.begin(), e.end(), [&] (pii x, pii y){return x > y;});
		int ind = 0;
		d.init(n);
		for (auto [type, x, y, id]:que) {
			while (ind < e.size() && e[ind].ff >= y) {
				d.Union(ed[e[ind].ss].ff, ed[e[ind].ss].ss, 0);
				ind++;
			}
			for (auto ev:change) {
				if (ev.id >= id) break;
				wei[ev.x] = ev.y;
			}
			for (auto [i, j]:mod) {
				if (wei[i] >= y) d.Union(ed[i].ff, ed[i].ss, 1);
			}
			ans[id] = d.siz[d.find(x)];
			for (auto [i, j]:mod) wei[i] = j;
			d.undo();
		}
		for (int i = 0;i < m;i++) ch[i] = 0;		
		for (auto ev:change) {
			wei[ev.x] = ev.y;
		}
		bat.clear();
	};
	int qid = 1;
	int modcnt = 0;
	while (q--) {
		int type; cin >> type;
		if (type == 1) {
			int bi, ri;
			cin >> bi >> ri;
			bi--;
			bat.push_back(obj(type, bi, ri, qid-1));
			modcnt++;
		} else {
			int s, w;
			cin >> s >> w;
			bat.push_back(obj(type, s, w, qid++));
		}
		if (modcnt >= bs) {
			solve();
			modcnt = 0;
		}
	}
	solve();
	for (int i = 1;i < qid;i++) cout << ans[i] << "\n";
}
/*
7 6
1 2 4
1 3 5
2 4 2
2 5 1
3 6 7
3 7 3
5
2 3 3
1 4 3
2 3 5
2 2 2
2 1 6
*/

Compilation message (stderr)

bridges.cpp: In lambda function:
bridges.cpp:95:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |    while (ind < e.size() && e[ind].ff >= y) {
      |           ~~~~^~~~~~~~~~
#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...