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>
using namespace std;
#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
#define SZ(x) ((int)(x).size())
typedef pair<int,int> ii;
const int MX_N = 5e4+5;
const int MX_M = 1e5+5;
const int MX_Q = 1e5+5;
const int B = 230;
//const int B = 1;
struct Edge {
int s, e, b, w;
};
struct Query {
int t, s, w;
};
int N, M;
ii bridge[MX_M];
int weight[MX_M];
int time_[MX_M];
int Q;
vector<Edge> edges;
vector<Query> queries[MX_Q/B+1];
int ans[MX_Q];
struct UnionFind {
vector<int> sz, p;
stack<int> history;
UnionFind(int n) { sz.assign(n,1); p.resize(n); iota(p.begin(),p.end(),0); }
int findset(int i) { return (p[i] == i) ? i : findset(p[i]); }
bool unionset(int i, int j) {
//cout << "union " << i << " " << j << endl;
int x = findset(i), y = findset(j);
if (x != y) {
if (sz[x] > sz[y]) {
sz[x] += sz[y], p[y] = x;
history.push(y);
} else {
sz[y] += sz[x], p[x] = y;
history.push(x);
}
return true;
} else return false;
}
int getLoc() { return SZ(history); }
void undo(int loc) {
while (SZ(history) > loc) {
int u = history.top(); history.pop();
//cout << "undo " << u << " " << p[u] << endl;
sz[p[u]] -= sz[u], p[u] = u;
}
}
int getSize(int i) { return sz[findset(i)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
FOR(i,1,M){
int U, V, D; cin >> U >> V >> D;
bridge[i] = {U,V};
weight[i] = D;
time_[i] = 0;
}
cin >> Q;
FOR(i,1,Q){
int T; cin >> T;
if (T == 1) {
int B, R; cin >> B >> R;
edges.emplace_back((Edge){ time_[B],i-1,B,weight[B] });
time_[B] = i;
weight[B] = R;
ans[i] = -1;
} else {
int S, W; cin >> S >> W;
queries[i/B].emplace_back((Query){ i,S,W });
}
}
FOR(B,1,M){
edges.emplace_back((Edge){ time_[B],Q+1,B,weight[B] });
}
sort(edges.begin(),edges.end(),[](Edge a,Edge b){ return a.w > b.w; });
FOR(b,0,Q/B){
UnionFind UF(N+1);
vector<Edge> full;
vector<Edge> partial;
for (auto& e : edges) {
if (e.s <= b*B && e.e >= (b+1)*B-1) {
full.push_back(e);
} else if (e.e < b*B || e.s > (b+1)*B-1) continue;
else {
partial.push_back(e);
}
}
//cout << "BUCKET " << b << " :: full " << SZ(full) << " partial " << SZ(partial) << " queries " << SZ(queries[b]) << endl;
sort(queries[b].begin(),queries[b].end(),[](Query a, Query b){ return a.w > b.w; });
int fi = 0;
for (auto& q : queries[b]) {
while (fi < SZ(full) && full[fi].w >= q.w) UF.unionset(bridge[full[fi].b].first, bridge[full[fi].b].second), ++fi;
int loc = UF.getLoc();
for (auto& e : partial) {
if (e.s <= q.t && q.t <= e.e && e.w >= q.w) {
UF.unionset(bridge[e.b].first, bridge[e.b].second);
}
}
ans[q.t] = UF.getSize(q.s);
UF.undo(loc);
}
}
FOR(i,1,Q){
if (ans[i] != -1) cout << ans[i] << '\n';
}
}
# | 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... |