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 = 800;
const int B = 500;
//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;
int ans[MX_Q];
struct UnionFind {
vector<int> sz, p;
int history[MX_N], hptr = 0;
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[hptr++] = y;
} else {
sz[y] += sz[x], p[x] = y;
history[hptr++] = x;
}
return true;
} else return false;
}
inline int getLoc() { return hptr; }
inline void undo(int loc) {
while (hptr > loc) {
int u = history[--hptr];
//cout << "undo " << u << " " << p[u] << endl;
sz[p[u]] -= sz[u], p[u] = u;
}
}
inline 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 });
queries.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; });
sort(queries.begin(),queries.end(),[](Query a,Query b) { if (a.t/B == b.t/B) return a.w > b.w; return a.t < b.t; });
//for(auto& a : edges) cout << a.s << " " << a.e << " :: " << bridge[a.b].first << " " << bridge[a.b].second << " " << a.w << endl;
int qi = 0;
while (qi < SZ(queries)) {
int b = queries[qi].t/B;
UnionFind UF(N+1);
vector<Edge> partial;
partial.reserve(2*B);
for (auto& e : edges) {
if ((e.s >= b*B && e.s <= (b+1)*B-1) || (e.e >= b*B && e.e <= (b+1)*B-1))
partial.push_back(e);
}
//sort(queries[b].begin(),queries[b].end(),[](Query a, Query b){ return a.w > b.w; });
int fi = 0;
for (; qi < SZ(queries) && queries[qi].t/B == b; ++qi) {
Query& q = queries[qi];
//cout << "BUCKET " << b << " :: " << q.t << " " << q.w << " " << q.s << endl;
while (fi < SZ(edges) && edges[fi].w >= q.w) {
//cout << "\tconsider edge " << edges[fi].s << " " << edges[fi].e << " " << bridge[edges[fi].b].first << " " << bridge[edges[fi].b].second << " " << edges[fi].w << endl;
if (edges[fi].s <= b*B && edges[fi].e >= (b+1)*B-1)
UF.unionset(bridge[edges[fi].b].first, bridge[edges[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... |