This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pll pair<ll, ll>
#define MP make_pair
#define PB push_back
#define pii pair<int, int>
#define pi3 pair<pii, int>
#define ft first
#define sd second
#define MP3(a,b,c) MP(MP(a,b),c)
using namespace std;
typedef long long ll;
const int N = 50100;
const int M = 100100;
const int Q = 100100;
const int CO = 200100;
const int oo = 2e9;
const int B = 1300;
const int MB = M / B + 10;
stack<pi3> stk;
vector<int> g[N], vc, vec, qr, ed, vrc, cur;
int n, m, U[M], V[M], D[M], q, ans[Q], tt = 0, mrk[M], loc[M];
int vls[B][B], tp[Q], l[Q], r[Q], siz[N], pr[N], mak[M], ind[M];
bool cmp(int x, int y){
return r[x] > r[y];
}
bool comp(int x, int y){
return D[x] < D[y];
}
int Get(int x){ return (x == pr[x] ? x : Get(pr[x])); }
void Link(int x, int y){
x = Get(x);
y = Get(y);
if (x == y) return;
if (siz[x] < siz[y]) swap(x, y);
stk.push(MP3(x, y, pr[y]));
pr[y] = x;
siz[x] += siz[y];
}
int get(int x){ return (x == pr[x] ? x : pr[x] = get(pr[x]));}
void link(int x, int y){
x = get(x);
y = get(y);
if (x == y) return;
if (siz[x] < siz[y]) swap(x, y);
pr[y] = x;
siz[x] += siz[y];
}
int main(){
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i < m; i++){
cin >> U[i] >> V[i] >> D[i];
U[i]--; V[i]--;
g[U[i]].PB(i);
g[V[i]].PB(i);
vc.PB(D[i]);
}
cin >> q;
for (int i = 0; i < q; i++) {
cin >> tp[i] >> l[i] >> r[i];
l[i]--;
vc.PB(r[i]);
}
sort(all(vc));
vc.resize(unique(all(vc)) - vc.begin());
for (int i = 0; i < m; i++) {
D[i] = lower_bound(all(vc), D[i]) - vc.begin();
vrc.PB(i);
}
for (int i = 0; i < q; i++)
r[i] = lower_bound(all(vc), r[i]) - vc.begin();
sort(all(vrc), comp);
for (int i = 0; i < m; i++)
ind[vrc[i]] = i;
for (int it = 0; it < q; it += B){
int bd = min(q, it + B);
tt++;
vec.clear();
qr.clear();
for (int i = it; i < bd; i++)
if (tp[i] == 1) {
if (mrk[l[i]] < tt) {
mrk[l[i]] = tt;
loc[l[i]] = sz(vec);
vec.PB(l[i]);
}
} else qr.PB(i);
for (int i = it; i < bd; i++){
int nm = i - it;
if (i == it){
for (int j = 0; j < sz(vec); j++)
vls[nm][j] = D[vec[j]];
} else {
for (int j = 0; j < sz(vec); j++)
vls[nm][j] = vls[nm - 1][j];
}
if (tp[i] == 1)
vls[nm][loc[l[i]]] = r[i];
}
for (int i = 0; i < m; i++)
if (mrk[i] < tt) {
mak[ind[i]] = tt;
}
cur.clear();
for (int i = 0; i < m; i++)
if (mak[i] == tt)
cur.PB(vrc[i]);
for (int i = 0; i < n; i++){
siz[i] = 1;
pr[i] = i;
}
if (sz(qr)){
sort(all(qr), cmp);
}
while (sz(stk)) stk.pop();
int lst = sz(cur) - 1;
for (int nm : qr){
while (lst >= 0 && D[cur[lst]] >= r[nm]){
link(U[cur[lst]], V[cur[lst]]);
lst--;
}
int gt = nm - it;
for (int i = 0; i < sz(vec); i++)
if (vls[gt][i] >= r[nm])
Link(U[vec[i]], V[vec[i]]);
ans[nm] = siz[Get(l[nm])];
while (sz(stk) > 0){
pi3 cr = stk.top();
stk.pop();
siz[cr.ft.ft] -= siz[cr.ft.sd];
pr[cr.ft.sd] = cr.sd;
}
}
//end
for (int i = 0; i < sz(vec); i++)
D[vec[i]] = vls[bd - it - 1][i];
}
for (int i = 0; i < q; i++)
if (tp[i] == 2)
cout << ans[i] << '\n';
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... |