Submission #259264

#TimeUsernameProblemLanguageResultExecution timeMemory
259264alishahali1382Bridges (APIO19_bridges)C++14
14 / 100
2330 ms7136 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=100010, SQ=530;

int n, m, q, u, v, x, y, t, a, b;
int E[MAXN][2], W[MAXN], W2[MAXN];
int typ[MAXN], A[MAXN], B[MAXN], ans[MAXN];
int ind[MAXN], ind1[MAXN], ind2[MAXN], m1, m2;
int par[MAXN], sz[MAXN], par2[MAXN], sz2[MAXN], comp[MAXN];
int bad[MAXN], vec[MAXN], ted;

inline bool cmp(int x, int y){ return W[x]>W[y];}
int getpar(int x){ return (par[x]==x?x:par[x]=getpar(par[x]));}
inline void join(int x, int y){
	x=getpar(x);
	y=getpar(y);
	if (x==y) return ;
	sz[x]+=sz[y];
	par[y]=x;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m;
	for (int i=1; i<=m; i++) cin>>E[i][0]>>E[i][1]>>W[i], ind[i]=i;
	sort(ind+1, ind+m+1, cmp);
	cin>>q;
	for (int i=0; i<q; i++) cin>>typ[i]>>A[i]>>B[i];
	for (int block=0; block*SQ<q; block++){
		for (int i=1; i<=n; i++) par[i]=i, sz[i]=1;
		int ql=block*SQ, qr=min(q, ql+SQ);
		for (int i=ql; i<qr; i++) if (typ[i]==1) bad[A[i]]=1;
		ted=0;
		for (int i=1; i<=m; i++) if (bad[i]) vec[ted++]=E[i][0], vec[ted++]=E[i][1];
		for (int i=ql; i<qr; i++) if (typ[i]==2) vec[ted++]=A[i];
		sort(vec, vec+ted);
		ted=unique(vec, vec+ted)-vec;
		
		vector<pii> Q;
		for (int i=ql; i<qr; i++) if (typ[i]==2) Q.pb({B[i], i});
		sort(all(Q));
		m1=m2=0;
		for (int i=1; i<=m; i++){
			if (bad[ind[i]]) ind1[++m1]=ind[i];
			else ind2[++m2]=ind[i];
		}
		
		ind2[m2+1]=0;
		for (int i=1; i<=m2+1; i++){
			while (Q.size() && Q.back().first>W[ind2[i]]){
				int id=Q.back().second;
				Q.pop_back();
				
				for (int j=0; j<ted; j++){
					int v=vec[j]=getpar(vec[j]);
					par2[v]=par[v], sz2[v]=sz[v];
				}
				for (int j=1; j<=m1; j++) W2[ind1[j]]=W[ind1[j]];
				for (int j=ql; j<id; j++) if (typ[j]==1) W2[A[j]]=B[j];
				
				for (int j=1; j<=m1; j++) if (W2[ind1[j]]>=B[id])
					join(E[ind1[j]][0], E[ind1[j]][1]);
				
				ans[id]=sz[getpar(A[id])];
				for (int j=0; j<ted; j++){
					int v=vec[j];
					par[v]=par2[v], sz[v]=sz2[v];
				}
			}
			join(E[ind2[i]][0], E[ind2[i]][1]);
		}
		for (int i=ql; i<qr; i++) if (typ[i]==1) W[A[i]]=B[i];
		sort(ind1+1, ind1+m1+1, cmp);
		merge(ind1+1, ind1+m1+1, ind2+1, ind2+m2+1, ind+1, cmp);
		
		for (int i=ql; i<qr; i++) if (typ[i]==1) bad[A[i]]=0;
	}
	for (int i=0; i<q; i++) if (typ[i]==2) cout<<ans[i]<<"\n";
	
	return 0;
}
/*
3 4
1 2 5
2 3 2
3 1 4
2 3 8
1
2 1 5


3 4
1 2 5
2 3 2
3 1 4
2 3 8
2
1 4 1
2 2 5


3 4
1 2 5
2 3 2
3 1 4
2 3 8
2
1 4 1
2 2 5


*/
#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...