Submission #419530

#TimeUsernameProblemLanguageResultExecution timeMemory
419530codebuster_10Bridges (APIO19_bridges)C++17
100 / 100
2684 ms60840 KiB
#include <bits/stdc++.h>

using namespace std ;

#define int int64_t //be careful about this 
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)

#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define eb emplace_back
#define LB lower_bound  
#define UB upper_bound
#define PQ priority_queue

#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a, b, sizeof(a))

template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

template<typename T>
void __p(T a) {
  cout<<a; 
}
template<typename T, typename F>
void __p(pair<T, F> a) {
  cout<<"{";
  __p(a.first);
  cout<<",";
  __p(a.second);
  cout<<"}\n"; 
}
template<typename T>
void __p(std::vector<T> a) {
  cout<<"{";
  for(auto it=a.begin(); it<a.end(); it++)
    __p(*it),cout<<",}\n"[it+1==a.end()]; 
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
  __p(a1);
  __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cout<<name<<" : ";
  __p(arg1);
  cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  int bracket=0,i=0;
  for(;; i++)
    if(names[i]==','&&bracket==0)
      break;
    else if(names[i]=='(')
      bracket++;
    else if(names[i]==')')
      bracket--;
  const char *comma=names+i;
  cout.write(names,comma-names)<<" : ";
  __p(arg1);
  cout<<" | ";
  __f(comma+1,args...);
}

void setIO(string s = "") {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
  cin.exceptions(cin.failbit); 
	cout.precision(15);	cout << fixed;
  #ifdef ONLINE_JUDGE
  if(sz(s)){
  	freopen((s+".in").c_str(),"r",stdin);
  	freopen((s+".out").c_str(),"w",stdout);
  }
  #define __f(...) 0
  #endif
}




















// solution by :- https://usaco.guide/problems/apio-2019bridges/solution







int n, m, q;


const int BLOCK_SIZE = 1000;
const int MAX_N = 5e4 + 1, MAX_M = 1e5 + 1;

stack<int> st;
int SZ[MAX_N], parent[MAX_N];


void reset(){
	iota(parent + 1, parent + n + 1, 1);
	fill(SZ + 1, SZ + n + 1, 1);
	return;
}

int find(int a){
	while(parent[a] != a) a = parent[a];
	return a;
}

void join(int a,int b){
	a = find(a), b = find(b);
	if(a == b) return;
	if(SZ[a] > SZ[b]) swap(a,b);
	// a is child of b now.
	st.push(a);
	SZ[b] += SZ[a], parent[a] = b;
	return; 
}

void roll_back(int req_size){
	while(int(st.size()) > req_size){
		int x = st.top(); st.pop();
		SZ[parent[x]] -= SZ[x];
		parent[x] = x;
	}
	return;
}

int u[MAX_M], v[MAX_M], w[MAX_M];
int t[MAX_M], x[MAX_M], y[MAX_M];
bool changed[MAX_M];
vector<int> to_join[BLOCK_SIZE];
int ans[MAX_M];

signed main(){
  setIO();
	rd(n,m);
	f(i,1,m+1){
		rd(u[i],v[i],w[i]);
	}
	rd(q);
	f(i,1,q+1) rd(t[i],x[i],y[i]);
	
	for(int l = 1; l <= q; l += BLOCK_SIZE){
		int r = min(q + 1, l + BLOCK_SIZE);
		reset();
		fill(changed + 1, changed + m + 1, false);
		
		
		vt<int> upd, ask, unchanged;
		f(i,l,r){
			if(t[i] == 1){
				changed[x[i]] = true;
				upd.pb(i);
			}else{
				ask.pb(i);
			}
		}
		
		f(i,1,m+1) if(!changed[i]) unchanged.pb(i);
		
		
		f(i,l,r){
			if(t[i] == 1){
				w[x[i]] = y[i];
			}else{
				to_join[i-l].clear();
				for(auto j : upd) if (w[x[j]] >= y[i]) to_join[i-l].push_back(x[j]);
			}
		}
		
		sort(all(ask),[&](auto A,auto B){
			return y[A] > y[B];
		});
		
		sort(all(unchanged),[&](auto A, auto B){
			return w[A] > w[B];
		});
		
		int ptr = 0;
		for(auto i : ask){
			while(ptr < sz(unchanged) && w[unchanged[ptr]] >= y[i]) {
				join(u[unchanged[ptr]], v[unchanged[ptr]]);
				ptr++;
			}
			int prev_size = sz(st);
			for(auto j : to_join[i - l]) join(u[j], v[j]);
			ans[i] = SZ[find(x[i])];
			roll_back(prev_size);
		}
		
	}
	
	f(i,1,q+1) if(t[i] == 2) cout << ans[i] << 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...