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;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = (int) 5e4 + 5;
const int M = (int) 1e5 + 5;
const int S = 320;
int n, m;
int lab[N];
int t[M];
int b[M];
int R[M];
int res[M];
int w[M];
bool has[M];
pair<int, int> e[M];
bool modify[M];
stack<pair<int, int>> st;
int get(int u) {
return lab[u] < 0 ? u : get(lab[u]);
}
void unite(int u, int v, bool roll_back) {
u = get(u);
v = get(v);
if (u != v) {
if (lab[u] > lab[v]) {
swap(u, v);
}
lab[u] += lab[v];
if (roll_back) {
st.emplace(v, -lab[v]);
}
lab[v] = u;
}
}
void rollback() {
while ((int) st.size()) {
auto [v, sz] = st.top();
st.pop();
int u = lab[v];
lab[v] = -sz;
lab[u] -= lab[v];
}
}
int sz(int u) {
return -lab[get(u)];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int u, v, d;
cin >> u >> v >> d;
e[i] = {u, v};
w[i] = d;
}
int q;
cin >> q;
for (int i = 1; i <= q; ++i) {
cin >> t[i] >> b[i] >> R[i];
t[i]--;
}
for (int B = 0; B <= q / S; ++B) {
int l = max(1, B * S);
int r = min(q, (B + 1) * S - 1);
fill(modify + 1, modify + m + 1, false);
fill(lab + 1, lab + n + 1, -1);
while ((int) st.size()) {
st.pop();
}
vector<int> qry;
for (int i = l; i <= r; ++i) {
if (t[i] == 0) {
modify[b[i]] = true;
} else {
qry.emplace_back(i);
}
}
vector<int> ord, change;
for (int i = 1; i <= m; ++i) {
if (!modify[i]) {
ord.emplace_back(i);
} else {
change.emplace_back(i);
}
}
sort(ord.begin(), ord.end(), [&](int u, int v) {
return w[u] > w[v];
});
sort(qry.begin(), qry.end(), [&](int u, int v) {
return R[u] > R[v];
});
int j = -1;
for (int i : qry) {
while (j + 1 < (int) ord.size() && w[ord[j + 1]] >= R[i]) {
j++;
auto [u, v] = e[ord[j]];
unite(u, v, false);
}
for (int k = i; k >= l; --k) {
if (t[k] == 0) {
if (R[k] >= R[i] && !has[b[k]]) {
auto [u, v] = e[b[k]];
unite(u, v, true);
}
has[b[k]] = true;
}
}
for (int k : change) {
if (!has[k] && w[k] >= R[i]) {
auto [u, v] = e[k];
unite(u, v, true);
}
}
res[i] = sz(b[i]);
rollback();
for (int k = l; k <= i; ++k) {
if (!t[k]) {
has[b[k]] = false;
}
}
}
for (int i = l; i <= r; ++i) {
if (!t[i]) {
w[b[i]] = R[i];
}
}
}
for (int i = 1; i <= q; ++i) {
if (t[i] == 1) {
cout << res[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... |