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 ll long long
const int N = 100'005, B = 500;
int par[N], sz[N], par2[N], sz2[N];
int find(int u) {
if (u == par[u]) {
return u;
}
return par[u] = find(par[u]);
}
void join(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
}
vector<int> in;
void add(int u) {
in.push_back(u);
par2[u] = par[u];
sz2[u] = sz[u];
}
int find2(int u) {
if (par2[u] == -1) {
add(u);
}
if (par2[u] == u) {
return u;
}
return par2[u] = find2(par2[u]);
}
void join2(int u, int v) {
u = find2(u), v = find2(v);
if (u == v) return;
if (sz2[u] < sz2[v]) swap(u, v);
par2[v] = u;
sz2[u] += sz2[v];
}
array<int, 3> qq[N];
array<int, 3> e[N];
int w[N], ans[N];
vector<int> vals[N];
vector<array<int, 3>> ask[N];
set<int> gud[N];
bool bad[N];
vector<int> vs;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> e[i][1] >> e[i][2] >> e[i][0];
}
bad[m] = 1;
int q;
cin >> q;
for (int k = 0; k < q; k++) {
cin >> qq[k][0] >> qq[k][1] >> qq[k][2];
if (qq[k][0] == 1) {
qq[k][1]--;
} else {
vs.push_back(qq[k][2]);
}
}
vs.push_back(0);
vs.push_back(1e9 + 1);
sort(vs.begin(), vs.end());
vs.resize(unique(vs.begin(), vs.end()) - vs.begin());
for (int i = 0; i < m; i++) {
e[i][0] = lower_bound(vs.begin(), vs.end(), e[i][0] + 1) - vs.begin() - 1;
w[i] = e[i][0];
gud[e[i][0]].insert(i);
}
for (int k = 0; k < q; k++) {
if (qq[k][0] == 1) {
qq[k][2] = lower_bound(vs.begin(), vs.end(), qq[k][2] + 1) - vs.begin() - 1;
} else {
qq[k][2] = lower_bound(vs.begin(), vs.end(), qq[k][2]) - vs.begin();
}
}
memset(ans, -1, sizeof ans);
vector<int> v;
int prv = 0;
auto process = [&] (int L, int R) {
for (int k = L; k <= R; k++) {
int t = qq[k][0];
if (t == 1) {
int b = qq[k][1], r = qq[k][2];
w[b] = r;
} else {
int x = qq[k][1], mx = qq[k][2];
ask[mx].push_back({x, mx, k});
for (auto i : v) {
if (w[i] >= mx) {
vals[k].push_back(i);
}
}
}
}
for (int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
par2[i] = sz2[i] = -1;
}
for (int i = vs.size() - 1; i >= 0; i--) {
for (auto idx : gud[i]) {
if (!bad[idx]) {
join(e[idx][1], e[idx][2]);
}
}
for (auto [x, y, idx] : ask[i]) {
for (auto idx : vals[idx]) {
join2(e[idx][1], e[idx][2]);
}
ans[idx] = sz2[find2(x)];
for (auto j : in) {
par2[j] = sz2[j] = -1;
}
in.clear();
}
ask[i].clear();
}
for (int k = L; k <= R; k++) {
vals[k].clear();
int t = qq[k][0];
if (t == 1) {
int b = qq[k][1], r = qq[k][2];
gud[e[b][0]].erase(b);
e[b][0] = r;
gud[e[b][0]].insert(b);
}
}
};
for (int k = 0; k < q; k++) {
if (k - prv == B) {
process(prv, k - 1);
for (auto i : v) {
bad[i] = 0;
}
v.clear();
prv = k;
}
int t = qq[k][0];
if (t == 1) {
int b = qq[k][1];
if (!bad[b]) {
bad[b] = 1;
v.push_back(b);
}
} else {
ans[k] = 1;
}
}
process(prv, q - 1);
for (auto i : ans) {
if (i == -1) continue;
cout << 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... |