Submission #356305

#TimeUsernameProblemLanguageResultExecution timeMemory
356305CSQ31다리 (APIO19_bridges)C++14
44 / 100
3103 ms197632 KiB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int blk = 2000;
const int MAXN = 555555;
vector<int>par(MAXN),szz(MAXN,1);
int find(int x){
	if(x == par[x])return x;
	return find(par[x]);
}
stack<int>stk;
void unite(int x,int y){
	x = find(x);
	y = find(y);
	if(x == y)return;
	if(szz[x] > szz[y])swap(x,y);
	stk.push(x);
	szz[y]+=szz[x];
	par[x] = par[y];
}
void rollback(int n){
	while(sz(stk) > n){
	    int x = stk.top();
	    szz[par[x]]-=szz[x];
	    par[x] = x;	
		stk.pop();
	}
}
void reset(int n){
	for(int i=1;i<=n;i++){szz[i] = 1;par[i] = i;}
	while(!stk.empty())stk.pop();
}
int main()
{
	owo
	int n,m;
	cin>>n>>m;
	vector<int>w(m+1),v(m+1),u(m+1);
	for(int i=1;i<=n;i++)par[i] = i;
	for(int i=1;i<=m;i++)cin>>v[i]>>u[i]>>w[i];
	int q;cin>>q;
	//blk = sqrt(q);
	vector<int>t(q),x(q),y(q),ans(q);
	vector<bool>change(m+1,0);
	vii join(q);
	for(int i=0;i<q;i++)cin>>t[i]>>x[i]>>y[i];
	for(int l=0;l<q;l+=blk){
		reset(n);
		for(int i=1;i<=m;i++)change[i] = 0;
		int r = min(q,l+blk);
		vector<int>ask,upd,unchange;
		for(int i=l;i<r;i++){
			if(t[i] == 2){
				ask.pb(i);
			}else{
				change[x[i]] = 1;
				upd.pb(i);
			}
		}
		for(int i=l;i<r;i++){
			if(t[i] == 2){
				for(int j:upd){
					if(w[x[j]] >= y[i])join[i].pb(x[j]);
				}
			}else w[x[i]] = y[i];
		}
		for(int i=1;i<=m;i++){
			if(!change[i])unchange.pb(i);
		}
		sort(all(ask),[&](int a,int b){return y[a] > y[b];});
		sort(all(unchange),[&](int a,int b){return w[a] > w[b];});
		int ptr = 0;
		for(int i : ask){
			while(ptr < sz(unchange) && w[unchange[ptr]] >= y[i]){
				int id = unchange[ptr];
				unite(v[id],u[id]);
				ptr++;
			}
			int prev = sz(stk);
			for(int j:join[i]){
				unite(u[j],v[j]);
			}
			ans[i] = szz[find(x[i])];
			rollback(prev);
		}
		for(int i=l;i<r;i++)join.clear();
	}
	
		for(int i=0;i<q;i++)if(t[i] == 2)cout<<ans[i]<<'\n';
}
#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...