#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[203];
ll magic=500,co=0; ll other=203;
deque<spi>Q;
stack<ll>stk;
struct ufds_ {
int p[MAXN],sz[MAXN];
ufds_() { FOR(i,0,n+1)p[i]=i,sz[i]=1; }
void merge_b4(ll x, ll y){
x=find_real(x),y=find_real(y);
if(x==y)return;
p[x]=y;
sz[y]+=sz[x];
}
ll find_real(ll x) { 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) { return (p[x]==x)?x:find_fake(p[x]);}
void rollback(){
while(stk.size()){
sz[p[stk.top()]]-=sz[stk.top()];
p[stk.top()]=stk.top();
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
*/
Compilation message
bridges.cpp: In function 'int main()':
bridges.cpp:101:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(bkts[co].size()>magic)++co;
~~~~~~~~~~~~~~~^~~~~~
bridges.cpp:105:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(bkts[co].size()>magic)++co;
~~~~~~~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
25 ms |
1912 KB |
Output is correct |
4 |
Correct |
9 ms |
1912 KB |
Output is correct |
5 |
Correct |
24 ms |
1912 KB |
Output is correct |
6 |
Correct |
20 ms |
2004 KB |
Output is correct |
7 |
Correct |
23 ms |
1816 KB |
Output is correct |
8 |
Correct |
25 ms |
1912 KB |
Output is correct |
9 |
Correct |
23 ms |
1912 KB |
Output is correct |
10 |
Correct |
26 ms |
2040 KB |
Output is correct |
11 |
Correct |
25 ms |
1912 KB |
Output is correct |
12 |
Correct |
26 ms |
1912 KB |
Output is correct |
13 |
Correct |
32 ms |
1912 KB |
Output is correct |
14 |
Correct |
29 ms |
2016 KB |
Output is correct |
15 |
Correct |
27 ms |
1912 KB |
Output is correct |
16 |
Correct |
23 ms |
1812 KB |
Output is correct |
17 |
Correct |
24 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1455 ms |
8080 KB |
Output is correct |
2 |
Correct |
1414 ms |
7928 KB |
Output is correct |
3 |
Correct |
1444 ms |
7916 KB |
Output is correct |
4 |
Correct |
1467 ms |
8148 KB |
Output is correct |
5 |
Correct |
1506 ms |
8056 KB |
Output is correct |
6 |
Correct |
1892 ms |
7996 KB |
Output is correct |
7 |
Correct |
1889 ms |
7992 KB |
Output is correct |
8 |
Correct |
1893 ms |
8024 KB |
Output is correct |
9 |
Correct |
63 ms |
5172 KB |
Output is correct |
10 |
Correct |
1523 ms |
6936 KB |
Output is correct |
11 |
Correct |
1403 ms |
6940 KB |
Output is correct |
12 |
Correct |
1239 ms |
8032 KB |
Output is correct |
13 |
Correct |
1298 ms |
7896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1299 ms |
7040 KB |
Output is correct |
2 |
Correct |
943 ms |
5572 KB |
Output is correct |
3 |
Correct |
1832 ms |
6884 KB |
Output is correct |
4 |
Correct |
1454 ms |
7284 KB |
Output is correct |
5 |
Correct |
62 ms |
5112 KB |
Output is correct |
6 |
Correct |
1696 ms |
7060 KB |
Output is correct |
7 |
Correct |
1403 ms |
7060 KB |
Output is correct |
8 |
Correct |
1214 ms |
7032 KB |
Output is correct |
9 |
Correct |
1085 ms |
7296 KB |
Output is correct |
10 |
Correct |
1012 ms |
7200 KB |
Output is correct |
11 |
Correct |
1116 ms |
7032 KB |
Output is correct |
12 |
Correct |
983 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1740 ms |
10548 KB |
Output is correct |
2 |
Correct |
61 ms |
5112 KB |
Output is correct |
3 |
Correct |
118 ms |
5928 KB |
Output is correct |
4 |
Correct |
190 ms |
6136 KB |
Output is correct |
5 |
Correct |
2374 ms |
9152 KB |
Output is correct |
6 |
Correct |
1716 ms |
10588 KB |
Output is correct |
7 |
Correct |
2353 ms |
9052 KB |
Output is correct |
8 |
Correct |
731 ms |
8168 KB |
Output is correct |
9 |
Correct |
707 ms |
8184 KB |
Output is correct |
10 |
Correct |
735 ms |
7964 KB |
Output is correct |
11 |
Correct |
1114 ms |
9224 KB |
Output is correct |
12 |
Correct |
1111 ms |
9368 KB |
Output is correct |
13 |
Correct |
1136 ms |
9356 KB |
Output is correct |
14 |
Correct |
2512 ms |
9180 KB |
Output is correct |
15 |
Correct |
2368 ms |
9140 KB |
Output is correct |
16 |
Correct |
1680 ms |
10460 KB |
Output is correct |
17 |
Correct |
1660 ms |
10428 KB |
Output is correct |
18 |
Correct |
1614 ms |
10672 KB |
Output is correct |
19 |
Correct |
1633 ms |
10544 KB |
Output is correct |
20 |
Correct |
1281 ms |
9752 KB |
Output is correct |
21 |
Correct |
1279 ms |
9620 KB |
Output is correct |
22 |
Correct |
1615 ms |
10296 KB |
Output is correct |
23 |
Correct |
1628 ms |
8424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1455 ms |
8080 KB |
Output is correct |
2 |
Correct |
1414 ms |
7928 KB |
Output is correct |
3 |
Correct |
1444 ms |
7916 KB |
Output is correct |
4 |
Correct |
1467 ms |
8148 KB |
Output is correct |
5 |
Correct |
1506 ms |
8056 KB |
Output is correct |
6 |
Correct |
1892 ms |
7996 KB |
Output is correct |
7 |
Correct |
1889 ms |
7992 KB |
Output is correct |
8 |
Correct |
1893 ms |
8024 KB |
Output is correct |
9 |
Correct |
63 ms |
5172 KB |
Output is correct |
10 |
Correct |
1523 ms |
6936 KB |
Output is correct |
11 |
Correct |
1403 ms |
6940 KB |
Output is correct |
12 |
Correct |
1239 ms |
8032 KB |
Output is correct |
13 |
Correct |
1298 ms |
7896 KB |
Output is correct |
14 |
Correct |
1299 ms |
7040 KB |
Output is correct |
15 |
Correct |
943 ms |
5572 KB |
Output is correct |
16 |
Correct |
1832 ms |
6884 KB |
Output is correct |
17 |
Correct |
1454 ms |
7284 KB |
Output is correct |
18 |
Correct |
62 ms |
5112 KB |
Output is correct |
19 |
Correct |
1696 ms |
7060 KB |
Output is correct |
20 |
Correct |
1403 ms |
7060 KB |
Output is correct |
21 |
Correct |
1214 ms |
7032 KB |
Output is correct |
22 |
Correct |
1085 ms |
7296 KB |
Output is correct |
23 |
Correct |
1012 ms |
7200 KB |
Output is correct |
24 |
Correct |
1116 ms |
7032 KB |
Output is correct |
25 |
Correct |
983 ms |
7032 KB |
Output is correct |
26 |
Correct |
2059 ms |
8192 KB |
Output is correct |
27 |
Correct |
2231 ms |
7832 KB |
Output is correct |
28 |
Correct |
1945 ms |
8100 KB |
Output is correct |
29 |
Correct |
1531 ms |
8016 KB |
Output is correct |
30 |
Correct |
1909 ms |
7936 KB |
Output is correct |
31 |
Correct |
1914 ms |
7908 KB |
Output is correct |
32 |
Correct |
2050 ms |
7924 KB |
Output is correct |
33 |
Correct |
1677 ms |
8056 KB |
Output is correct |
34 |
Correct |
1747 ms |
7984 KB |
Output is correct |
35 |
Correct |
1694 ms |
8056 KB |
Output is correct |
36 |
Correct |
1456 ms |
7940 KB |
Output is correct |
37 |
Correct |
1431 ms |
7928 KB |
Output is correct |
38 |
Correct |
1435 ms |
7928 KB |
Output is correct |
39 |
Correct |
1209 ms |
8168 KB |
Output is correct |
40 |
Correct |
1207 ms |
8040 KB |
Output is correct |
41 |
Correct |
1213 ms |
8048 KB |
Output is correct |
42 |
Correct |
1180 ms |
7896 KB |
Output is correct |
43 |
Correct |
1217 ms |
7880 KB |
Output is correct |
44 |
Correct |
1191 ms |
7800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
25 ms |
1912 KB |
Output is correct |
4 |
Correct |
9 ms |
1912 KB |
Output is correct |
5 |
Correct |
24 ms |
1912 KB |
Output is correct |
6 |
Correct |
20 ms |
2004 KB |
Output is correct |
7 |
Correct |
23 ms |
1816 KB |
Output is correct |
8 |
Correct |
25 ms |
1912 KB |
Output is correct |
9 |
Correct |
23 ms |
1912 KB |
Output is correct |
10 |
Correct |
26 ms |
2040 KB |
Output is correct |
11 |
Correct |
25 ms |
1912 KB |
Output is correct |
12 |
Correct |
26 ms |
1912 KB |
Output is correct |
13 |
Correct |
32 ms |
1912 KB |
Output is correct |
14 |
Correct |
29 ms |
2016 KB |
Output is correct |
15 |
Correct |
27 ms |
1912 KB |
Output is correct |
16 |
Correct |
23 ms |
1812 KB |
Output is correct |
17 |
Correct |
24 ms |
1912 KB |
Output is correct |
18 |
Correct |
1455 ms |
8080 KB |
Output is correct |
19 |
Correct |
1414 ms |
7928 KB |
Output is correct |
20 |
Correct |
1444 ms |
7916 KB |
Output is correct |
21 |
Correct |
1467 ms |
8148 KB |
Output is correct |
22 |
Correct |
1506 ms |
8056 KB |
Output is correct |
23 |
Correct |
1892 ms |
7996 KB |
Output is correct |
24 |
Correct |
1889 ms |
7992 KB |
Output is correct |
25 |
Correct |
1893 ms |
8024 KB |
Output is correct |
26 |
Correct |
63 ms |
5172 KB |
Output is correct |
27 |
Correct |
1523 ms |
6936 KB |
Output is correct |
28 |
Correct |
1403 ms |
6940 KB |
Output is correct |
29 |
Correct |
1239 ms |
8032 KB |
Output is correct |
30 |
Correct |
1298 ms |
7896 KB |
Output is correct |
31 |
Correct |
1299 ms |
7040 KB |
Output is correct |
32 |
Correct |
943 ms |
5572 KB |
Output is correct |
33 |
Correct |
1832 ms |
6884 KB |
Output is correct |
34 |
Correct |
1454 ms |
7284 KB |
Output is correct |
35 |
Correct |
62 ms |
5112 KB |
Output is correct |
36 |
Correct |
1696 ms |
7060 KB |
Output is correct |
37 |
Correct |
1403 ms |
7060 KB |
Output is correct |
38 |
Correct |
1214 ms |
7032 KB |
Output is correct |
39 |
Correct |
1085 ms |
7296 KB |
Output is correct |
40 |
Correct |
1012 ms |
7200 KB |
Output is correct |
41 |
Correct |
1116 ms |
7032 KB |
Output is correct |
42 |
Correct |
983 ms |
7032 KB |
Output is correct |
43 |
Correct |
1740 ms |
10548 KB |
Output is correct |
44 |
Correct |
61 ms |
5112 KB |
Output is correct |
45 |
Correct |
118 ms |
5928 KB |
Output is correct |
46 |
Correct |
190 ms |
6136 KB |
Output is correct |
47 |
Correct |
2374 ms |
9152 KB |
Output is correct |
48 |
Correct |
1716 ms |
10588 KB |
Output is correct |
49 |
Correct |
2353 ms |
9052 KB |
Output is correct |
50 |
Correct |
731 ms |
8168 KB |
Output is correct |
51 |
Correct |
707 ms |
8184 KB |
Output is correct |
52 |
Correct |
735 ms |
7964 KB |
Output is correct |
53 |
Correct |
1114 ms |
9224 KB |
Output is correct |
54 |
Correct |
1111 ms |
9368 KB |
Output is correct |
55 |
Correct |
1136 ms |
9356 KB |
Output is correct |
56 |
Correct |
2512 ms |
9180 KB |
Output is correct |
57 |
Correct |
2368 ms |
9140 KB |
Output is correct |
58 |
Correct |
1680 ms |
10460 KB |
Output is correct |
59 |
Correct |
1660 ms |
10428 KB |
Output is correct |
60 |
Correct |
1614 ms |
10672 KB |
Output is correct |
61 |
Correct |
1633 ms |
10544 KB |
Output is correct |
62 |
Correct |
1281 ms |
9752 KB |
Output is correct |
63 |
Correct |
1279 ms |
9620 KB |
Output is correct |
64 |
Correct |
1615 ms |
10296 KB |
Output is correct |
65 |
Correct |
1628 ms |
8424 KB |
Output is correct |
66 |
Correct |
2059 ms |
8192 KB |
Output is correct |
67 |
Correct |
2231 ms |
7832 KB |
Output is correct |
68 |
Correct |
1945 ms |
8100 KB |
Output is correct |
69 |
Correct |
1531 ms |
8016 KB |
Output is correct |
70 |
Correct |
1909 ms |
7936 KB |
Output is correct |
71 |
Correct |
1914 ms |
7908 KB |
Output is correct |
72 |
Correct |
2050 ms |
7924 KB |
Output is correct |
73 |
Correct |
1677 ms |
8056 KB |
Output is correct |
74 |
Correct |
1747 ms |
7984 KB |
Output is correct |
75 |
Correct |
1694 ms |
8056 KB |
Output is correct |
76 |
Correct |
1456 ms |
7940 KB |
Output is correct |
77 |
Correct |
1431 ms |
7928 KB |
Output is correct |
78 |
Correct |
1435 ms |
7928 KB |
Output is correct |
79 |
Correct |
1209 ms |
8168 KB |
Output is correct |
80 |
Correct |
1207 ms |
8040 KB |
Output is correct |
81 |
Correct |
1213 ms |
8048 KB |
Output is correct |
82 |
Correct |
1180 ms |
7896 KB |
Output is correct |
83 |
Correct |
1217 ms |
7880 KB |
Output is correct |
84 |
Correct |
1191 ms |
7800 KB |
Output is correct |
85 |
Execution timed out |
3092 ms |
10084 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |