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 target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
#define uid(a, b) uniform_int_distribution<int>(a, b)(mt)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e18;
const int mod = 1e9+7;
const int maxn = 1e5+5;
const int sq = 317;
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int u[maxn], v[maxn], d[maxn], D[maxn], E[maxn], p[maxn], sz[maxn], a[maxn], b[maxn], wt[maxn];
bool ok[maxn];
bool c(int x, int y){
return d[x] < d[y];
}
bool cmp(int x, int y){
return b[x] < b[y];
}
int fnd(int x){
if(p[x] == x)return x;
return fnd(p[x]);
}
void solve() {
int n, m, q;
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> u[i] >> v[i] >> d[i];
--u[i], --v[i];
E[i] = i;
D[i] = d[i];
}
cin >> q;
for(int j = 0; j < q; j++){
cin >> wt[j] >> a[j] >> b[j];
--a[j];
}
vt<int> ch, qu;
int out[q];
for(int i = 0; i < q; i += sq){
ch.clear(), qu.clear();
for(int j = i; j < q && j / sq == i / sq; j++){
if(wt[j] == 2){
qu.pb(j);
}
else {
ok[a[j]] = 1;
ch.pb(j);
}
}
for(int i = 0; i < n; i++)p[i] = i, sz[i] = 1;
sort(E, E + m, c);
sort(all(qu), cmp);
int sz1 = ch.size(), sz2 = qu.size(), r = m - 1;
for(int j = sz2 - 1; j >= 0; j--){
int it = qu[j];
while(r >= 0 && d[E[r]] >= b[it]){
if(!ok[E[r]]){
int U = fnd(u[E[r]]), V = fnd(v[E[r]]);
if(U != V){
if(sz[U] > sz[V])swap(U, V);
p[U] = V, sz[V] += sz[U];
}
}
r--;
}
for(int o = 0; o < sz1 && ch[o] < it; o++){
D[a[ch[o]]] = b[ch[o]];
}
stack<pii> rlx;
for(int o = 0; o < sz1; o++){
if(D[a[ch[o]]] >= b[it]){
D[a[ch[o]]] = 0;
int U = fnd(u[a[ch[o]]]), V = fnd(v[a[ch[o]]]);
if(U != V){
if(sz[U] > sz[V])swap(U, V);
p[U] = V, sz[V] += sz[U];
rlx.push({U, V});
}
}
}
out[it] = sz[fnd(a[it])];
while(!rlx.empty()){
pii j = rlx.top();
p[j.ff] = j.ff;
sz[j.ss] -= sz[j.ff];
rlx.pop();
}
for(int o = 0; o < sz1; o++)D[a[ch[o]]] = d[a[ch[o]]];
}
for(auto j : ch){
ok[a[j]] = 0;
D[a[j]] = d[a[j]] = b[j];
}
}
for(int i = 0; i < q; i++){
if(wt[i] == 2)cout << out[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int times = 1; //cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |