Submission #941666

#TimeUsernameProblemLanguageResultExecution timeMemory
941666BaizhoBridges (APIO19_bridges)C++14
100 / 100
1554 ms207768 KiB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
 
// #pragma GCC optimize("Ofast,unroll-loops,fast-math")
// #pragma GCC target("popcnt,sse3,avx")
 
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
 
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define pb push_back
 
const int mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353;  // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const int N = ll(1e5)+100;
const int M = ll(2e5) + 100;
const long long inf = 5e18;
//const long double eps = 1e-15L;
 
ll lcm(ll a, ll b) { return (a / __gcd(a,b))*b; }
 
ll binpow(ll a, ll b, ll m) { ll res=1; a %= m; while(b>0){ if(b&1)res=(res * a) % m; a=(a * a) % m; b/=2; } return res%m;}
 
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }

int n, m, q, u[N], v[N], d[N], t[N], x[N], y[N], par[N], siz[N], ans[N], changed[N];
vector<int> unchanged, ask, change, ex[N];

const int B = 1000;

int findpar(int v) {
	if(v == par[v]) return v;
	return findpar(par[v]);
}

vector<pair<pair<int, int>, int> > dat;

void rollback(int s) {
	while(dat.size() > s) {
		int a = dat.back().ff.ff, b = dat.back().ff.ss, parb = dat.back().ss;
		par[b] = parb;
		siz[a] -= siz[b];
		dat.pop_back();
	}
}

void combine(int a, int b) {
	a = findpar(a), b = findpar(b);
	if(a != b) {
		if(siz[a] < siz[b]) swap(a, b);
		dat.pb({{a, b}, par[b]});
		par[b] = a;
		siz[a] += siz[b];
	}
}

void Baizho() {
	cin>>n>>m;
	for(int i = 1; i <= m; i ++) cin>>u[i]>>v[i]>>d[i];
	cin>>q;
	for(int i = 1; i <= q; i ++) {
		cin>>t[i]>>x[i]>>y[i];
	}
	for(int l = 1; l <= q; l += B) {
		unchanged.clear(), ask.clear(), change.clear();
		for(int i = 1; i <= n; i ++) {
			par[i] = i;
			siz[i] = 1;
		}
		for(int i = 1; i <= m; i ++) changed[i] = 0;
		int r = min(q, l + B - 1);
		for(int i = l; i <= r; i ++) {
			if(t[i] == 1) changed[x[i]] = 1;
			else ask.pb(i);
		}
		for(int i = 1; i <= m; i ++) {
			if(!changed[i]) unchanged.pb(i);
			else change.pb(i);
		}
		for(int i = l; i <= r; i ++) {
			if(t[i] == 2) {
				for(auto p : change) if(d[p] >= y[i]) ex[i].pb(p);
			} else d[x[i]] = y[i];
		}
		sort(all(unchanged), [](int i, int j) {
			return d[i] < d[j];
		});
		sort(all(ask), [](int i, int j) {
			return y[i] > y[j];
		});
		for(auto i : ask) {
			while(!unchanged.empty() && d[unchanged.back()] >= y[i]) {
//				cout<<i<<" "<<unchanged.back()<<"\n";
				combine(u[unchanged.back()], v[unchanged.back()]);
				unchanged.pop_back();
			}
			// add the extra bridges which we changed in this block so they are a special case
			int prev = dat.size();
			for(auto p : ex[i]) {
//				cout<<i<<" "<<p<<" yolo \n";
				combine(u[p], v[p]);
			}
			ans[i] = siz[findpar(x[i])];
//			cout<<i<<" "<<y[i]<<" "<<findpar(x[i])<<"\n";
			rollback(prev);
		}
	}
	for(int i = 1; i <= q; i ++) if(t[i] == 2) cout<<ans[i]<<"\n";
}
 
signed main() {		
// 	Freopen("nondec");
    ios_base::sync_with_stdio(false);   
    cin.tie(0);cout.tie(0); 
//   	precalc();
   	
    int ttt = 1;
//    cin>>ttt;
 
    for(int i=1; i<=ttt; i++) {Baizho(); }
}

Compilation message (stderr)

bridges.cpp: In function 'void rollback(int)':
bridges.cpp:50:19: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |  while(dat.size() > s) {
      |        ~~~~~~~~~~~^~~
bridges.cpp: In function 'void Freopen(std::string)':
bridges.cpp:35:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:35:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".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...