Submission #546624

#TimeUsernameProblemLanguageResultExecution timeMemory
546624wmch13다리 (APIO19_bridges)C++14
100 / 100
2082 ms29244 KiB
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
#define ordered_set tree<pair<ll,int>, null_type,less<pair<ll,int>>, rb_tree_tag,tree_order_statistics_node_update>

const int INF = 1e8;
const ll lINF = 1e18;
const int mxN = 5e4 + 9;
const int mxM = 1e5 + 9;
const int MOD = 998244353;

void setIO (string s) {
	freopen ((s + ".in").c_str(), "r", stdin);
	freopen ((s + ".out").c_str(), "w", stdout);
	
	return;
}

int u[mxM], v[mxM], w[mxM], n, m, Q;
int type[mxM], x[mxM], y[mxM];
int pa[mxN], sz[mxN], ans[mxM];
bool changed[mxM];
stack <int> logStack;

void clearDSU () {
	for (int i = 1; i <= n; i++) {
		pa[i] = i;
		sz[i] = 1;
	}
	
	return;
}

int getPa (int x) {
	while (pa[x] != x)
		x = pa[x];
	
	return x;
}

void goUnion (int x, int y) {
	x = getPa (x), y = getPa (y);
	if (x == y)
		return;
		
	if (sz[x] > sz[y])
		swap (x, y);
	
	logStack.push (x);
	sz[y] += sz[x];
	pa[x] = pa[y];
	return;
}

void rollBack (int x) {
	while ((int) logStack.size() > x) {
		int lastUpdated = logStack.top();
		logStack.pop ();
		
		sz[pa[lastUpdated]] -= sz[lastUpdated];
		pa[lastUpdated] = lastUpdated;
	}
	
	return;
}

void solve () {
	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 >> type[i] >> x[i] >> y[i];
		
	int blockSize = 1000;
	vector <int> toJoin [blockSize + 9];
	
	for (int l = 1; l <= Q; l += blockSize) {
		int r = min (Q, l + blockSize - 1);
		
		clearDSU ();
		for (int i = 1; i <= m; i++)
			changed[i] = false;
			
		vector <int> ask, upd, unchanged;
		for (int i = l; i <= r; i++) {
			if (type[i] == 1) {
				changed[x[i]] = true;
				upd.pb (i);
			} else ask.pb (i);
		}
		
		for (int i = 1; i <= m; i++)
			if (!changed[i])
				unchanged.pb (i);
		
		for (int i = l; i <= r; i++) {
			if (type[i] == 1) {
				w[x[i]] = y[i];
			} else {
				toJoin[i - l].clear();
				for (int j: upd) 
					if (w[x[j]] >= y[i])
						toJoin[i - l].pb (x[j]);
			}
		}
		
		sort (ask.begin(), ask.end(), [&] (int a, int b) {
			return y[a] > y[b];
		});
		
		sort (unchanged.begin(), unchanged.end(), [&] (int a, int b) {
			return w[a] > w[b];
		});
		
		int ptr = 0;
		for (int i: ask) {
			while (ptr < (int) unchanged.size() && w[unchanged[ptr]] >= y[i]) {
				goUnion (u[unchanged[ptr]], v[unchanged[ptr]]);
				ptr++;
			}
			
			int lastSize = logStack.size();
			for (int j: toJoin[i - l])
				goUnion (u[j], v[j]);
			
			ans[i] = sz[getPa (x[i])];
			rollBack (lastSize);
		}
	}
	
	for (int i = 1; i <= Q; i++)
		if (type[i] == 2)
			cout << ans[i] << "\n";
	
	return;
}

int main () {
	int t = 1;
	//cin >> t;
	
	ios_base::sync_with_stdio (0);
	cin.tie (0);
	
	//setIO ("deleg");
	//preCalc ();
	while (t--) {
		//initialize common variables
		
		
		//go solve
		solve ();
	}
		
	return 0;
}

/*
 * 
 * 
 * 
 * 
 * 
 * 
 *8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

Compilation message (stderr)

bridges.cpp: In function 'void setIO(std::string)':
bridges.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |  freopen ((s + ".in").c_str(), "r", stdin);
      |  ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |  freopen ((s + ".out").c_str(), "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...