# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
142980 | ryansee | Bridges (APIO19_bridges) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) //0.0000001 the value
#define PI (acos(-1))
#define MAXN (300006/3)
#define ll /*long long*/ int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = ss; ii < ee; ++ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define emp emplace
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL<<(x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
ll n,m,q;
struct edge {
ll a,b,w,i,in;
bool operator < (const edge &e) const {
return w>e.w;
}
};
edge edges[MAXN];
int pos[MAXN];
struct query {
ll b,r,i,q,f;
bool operator < (const query &e) const {
if(b!=e.b)return b<e.b;
return i<e.i;
}
}; vector<query>bkts[178];
ll magic=570,co=0; ll other=178;
deque<spi>Q;
stack<ll>stk;
struct ufds_ {
int p[MAXN],sz[MAXN];bitset<MAXN>up;
ufds_() { FOR(i,0,n+1)p[i]=i,sz[i]=1; up=0; }
void merge_b4(ll x, ll y){
x=find_real(x).f,y=find_real(y).f;
if(x==y)return;
p[x]=y;up[x]=1;
sz[y]+=sz[x];
sz[x]=0;
}
pi find_real(ll x) { if(!up[x]) return pi(find_fake(x), x);
if(p[x]==x) return pi(x, x);
else {
pi res=find_real(p[x]);
p[x]=res.s;
return res;
}
/*return (p[x]==x)?x:p[x]=find_real(p[x]);*/}
void fake_merge(ll x, ll y) {
x=find_fake(x),y=find_fake(y);
if(x==y)return;
if(sz[x]>sz[y])swap(x,y);//sz[x]<=sz[y];
p[x]=y;
sz[y]+=sz[x];
stk.ph(x);
}
ll find_fake(ll x) { if(up[x]) return find_real(x).f; return (p[x]==x)?x:find_fake(p[x]);}
void rollback(){
while(stk.size()){
sz[p[stk.top()]]-=sz[stk.top()];
assert(find_fake(p[stk.top()], 1) == p[stk.top()]); p[stk.top()]=stk.top(); up[stk.top()]=0;
stk.pop();
}
return;
}
} ufds;
int ans[MAXN];
int main()
{ mmst(ans,-1);
FAST
cin>>n>>m;
FOR(i,0,m){
cin>>edges[i].a>>edges[i].b>>edges[i].w;edges[i].i=i;edges[i].in=1;
}
sort(edges,edges+m);
FOR(i,0,m)pos[edges[i].i]=i;
cin>>q;
FOR(i,0,q){
ll t;cin>>t;
if(t==1){
ll b,r;cin>>b>>r;--b;
bkts[co].pb({b,r,i,0});
if(bkts[co].size()>magic)++co;
} else {
ll p,w;cin>>p>>w;
bkts[co].pb({p,w,i,1});
if(bkts[co].size()>magic)++co;
}
}
ufds=ufds_();
FOR(bk,0,other){ if(bkts[bk].empty())break;
for(auto i:bkts[bk]){
if(!i.q)edges[pos[i.b]].in=0;
}
vector<query>hmm,handle;
for(auto i:bkts[bk]){
if(i.q)hmm.pb(i);
else handle.pb(i);
}
sort(all(hmm),[](query x, query y){return x.r>y.r;});ufds=ufds_();
sort(all(handle)); FOR(i,1,siz(handle))if(handle[i-1].b==handle[i].b)handle[i].f=1;
ll co=0;
for(auto i:hmm){
while(co<m&&edges[co].w>=i.r){
if(edges[co].in)ufds.merge_b4(edges[co].a,edges[co].b);
++co;
}
auto tmp=i;
FOR(i,0,siz(handle))handle[i].q=0;
FOR(i,0,siz(handle)-1)if(handle[i+1].b==handle[i].b&&handle[i+1].i<tmp.i)handle[i].q=1;
for(auto j:handle)if(!j.q&&((j.i<i.i&&j.r>=i.r)||(j.i>i.i&&edges[pos[j.b]].w>=i.r&&!j.f))){
ufds.fake_merge(edges[pos[j.b]].a,edges[pos[j.b]].b);
}
// cerr<<i.i<<' '<<co<<' '<<ufds.find_fake(1)<<' '<<ufds.find_fake(2)<<' '<<ufds.find_fake(3)<<' '<<ufds.sz[1]<<' '<<ufds.sz[2]<<' '<<ufds.sz[3]<<'\n';
ans[i.i]=ufds.sz[ufds.find_fake(i.b)];
ufds.rollback();
}
for(auto i:bkts[bk])if(!i.q){
edges[pos[i.b]].in=1;
edges[pos[i.b]].w=i.r;
}
sort(edges,edges+m);
FOR(i,0,m)pos[edges[i].i]=i;
}
FOR(i,0,q)if(~ans[i]){
cout<<ans[i]<<'\n';
}
}
/*
3 4
1 2 5
2 3 2
3 1 4
2 3 8
5
2 1 5
1 4 1
2 2 5
1 1 1
2 3 2
*/
/*
7 8
1 2 5
1 6 5
2 3 5
2 7 5
3 4 5
4 5 5
5 6 5
6 7 5
12
2 1 6
1 1 1
2 1 2
1 2 3
2 2 2
1 5 2
1 3 1
2 2 4
2 4 2
1 8 1
2 1 1
2 1 3
*
* 5 2
4 3 3
4 1 10
5
2 1 5
1 2 9
1 1 6
1 1 2
2 3 4
*
* Ans:
* 1
*
* 5 2
4 3 3
4 1 10
8
2 1 5
1 2 2
1 2 9
1 1 6
1 1 2
2 3 4
1 1 3
2 1 6
*/