Submission #689544

#TimeUsernameProblemLanguageResultExecution timeMemory
689544NothingXDBridges (APIO19_bridges)C++17
14 / 100
1413 ms7384 KiB
/*

   Wei Wai Wei Wai
   Zumigat nan damu dimi kwayt rayt

*/

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
struct FastMod {
	ull b, m;
	FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
	ull reduce(ull a) {
		ull q = (ull)((L(m) * a) >> 64);
		ull r = a - q * b; // can be proven that 0 <= r < 2*b
		return r >= b ? r - b : r;
	}
};
FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 5e4 + 10;
const int maxm = 1e5 + 10;
const int sq = 320;

int n, m, q, dsu[maxn], edge[maxm][3], fw[maxm], idx[maxm], w[maxm], ans[maxm];
vector<int> Q[sq][2];
vector<int> g[maxn];
vector<int> E[2];
bool mark[maxm], vis[maxn];

int getdsu(int v){
	return (dsu[v] < 0? v: dsu[v] = getdsu(dsu[v]));
}

void merge(int u, int v){
	if ((u = getdsu(u)) == (v = getdsu(v))) return;
	if (dsu[u] > dsu[v]) swap(u, v);
	dsu[u] += dsu[v];
	dsu[v] = u;
}

vector<int> merge(vector<int> a, vector<int> b){
	int pta = 0, ptb = 0;
	vector<int> res;
	while(pta < a.size() || ptb < b.size()){
		if (ptb == b.size() || (pta < a.size() && edge[a[pta]][2] < edge[b[ptb]][2])){
			res.push_back(a[pta]);
			pta++;
		}
		else{
			res.push_back(b[ptb]);
			ptb++;
		}
	}
	return res;
}

bool cmp(int x, int y){
	return edge[x][2] > edge[y][2];
}

bool cmp2(int x, int y){
	return w[x] > w[y];
}

int dfs(int v){
	vis[v] = true;
	int res = -dsu[v];
	for (auto u: g[v]) if (!vis[u]) res += dfs(u);
	return res;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	memset(ans, -1, sizeof ans);

	cin >> n >> m;
	for (int i = 1; i <= m; i++){
		cin >> edge[i][0] >> edge[i][1] >> edge[i][2];
		E[0].push_back(i);
	}

	sort(all(E[0]), cmp);

	cin >> q;

	for (int i = 1; i <= q; i++){
		int t; cin >> t >> idx[i] >> w[i];
		Q[i/sq][t-1].push_back(i);
	}

	for (int i = 0; i * sq <= q; i++){
		memset(dsu, -1, sizeof dsu);
		vector<int> tmp[3];
		for (auto x: Q[i][0]){
			 mark[idx[x]] = true;
		}
		for (auto x: E[0]){
			if (mark[x]) tmp[2].push_back(x);
			else tmp[0].push_back(x);
		}
		for (auto x: E[1]){
			if (mark[x]) tmp[2].push_back(x);
			else tmp[1].push_back(x);
		}
		E[0] = merge(tmp[0], tmp[1]);
		E[1] = tmp[2];

		sort(all(Q[i][1]), cmp2);


		int ptr = 0;
		for (auto x: Q[i][1]){
			for (auto y: E[1]){
				fw[y] = edge[y][2];
			}
			while(ptr < E[0].size() && edge[E[0][ptr]][2] >= w[x]){
				merge(edge[E[0][ptr]][0], edge[E[0][ptr]][1]);
				ptr++;
			}
			for (auto y: Q[i][0]){
				if (y > x) break;
				fw[idx[y]] = w[y];
			}
			for (auto y: E[1]){
				if (fw[y] >= w[x]){
					int u = getdsu(edge[y][0]);
					int v = getdsu(edge[y][1]);
					g[u].push_back(v);
					g[v].push_back(u);
				}
			}
			ans[x] = dfs(getdsu(idx[x]));
			vis[getdsu(idx[x])] = false;
			for (auto y: E[1]){
				int u = getdsu(edge[y][0]);
				int v = getdsu(edge[y][1]);
				g[u].clear();
				g[u].shrink_to_fit();
				g[v].clear();
				g[v].shrink_to_fit();
				vis[u] = vis[v] = false;
			}
		}
		for (auto x: Q[i][0]){
			edge[idx[x]][2] = w[x];
			mark[idx[x]] = false;
		}
		sort(all(E[1]), cmp);
	}

	for (int i = 1; i <= q; i++) if (ans[i] != -1) cout << ans[i] << '\n';
	return 0;
}

Compilation message (stderr)

bridges.cpp: In function 'std::vector<int> merge(std::vector<int>, std::vector<int>)':
bridges.cpp:68:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |  while(pta < a.size() || ptb < b.size()){
      |        ~~~~^~~~~~~~~~
bridges.cpp:68:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |  while(pta < a.size() || ptb < b.size()){
      |                          ~~~~^~~~~~~~~~
bridges.cpp:69:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |   if (ptb == b.size() || (pta < a.size() && edge[a[pta]][2] < edge[b[ptb]][2])){
      |       ~~~~^~~~~~~~~~~
bridges.cpp:69:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |   if (ptb == b.size() || (pta < a.size() && edge[a[pta]][2] < edge[b[ptb]][2])){
      |                           ~~~~^~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:141:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |    while(ptr < E[0].size() && edge[E[0][ptr]][2] >= w[x]){
      |          ~~~~^~~~~~~~~~~~~
#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...