This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <cstddef>
#include <vector>
#include <array>
#include <stack>
#include <tuple>
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
constexpr size_t BUCKET = 800;
constexpr size_t VERTICES = 50000;
constexpr size_t EDGES = 100000;
constexpr size_t QUERIES = 100000;
size_t N, M, Q, Step;
std::array<i32, VERTICES> parent;
std::array<i32, EDGES> U, V, C;
std::array<bool, EDGES> Static;
struct Query { i32 type, idx, weight; };
std::array<Query, QUERIES> Qs;
std::vector<i32> static_query, dynamic_query;
std::array<i32, QUERIES> answer;
i32 find(i32 u) {
while (parent[u] >= 0) {
u = parent[u];
}
return u;
}
void merge(i32 u, i32 v) {
u = find(u);
v = find(v);
if (u == v) {
return;
}
if (parent[u] > parent[v]) {
std::swap(u, v);
}
parent[u] += parent[v];
parent[v] = u;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> N >> M;
for (size_t i = 0; i < M; ++i) {
std::cin >> U[i] >> V[i] >> C[i];
--U[i]; --V[i];
}
std::cin >> Q;
Step = (Q + BUCKET - 1) / BUCKET;
for (size_t i = 0; i < Q; ++i) {
std::cin >> Qs[i].type >> Qs[i].idx >> Qs[i].weight;
--Qs[i].idx;
}
std::fill(Static.begin(), Static.end(), true);
static_query.reserve(M + BUCKET);
dynamic_query.reserve(BUCKET);
for (size_t step = 0; step < Step; ++step) {
std::fill(parent.begin(), parent.begin() + N, -1);
const size_t first = step * BUCKET;
const size_t last = std::min((step + 1) * BUCKET, Q);
for (size_t i = first; i < last; ++i) {
if (Qs[i].type == 1) {
Static[Qs[i].idx] = false;
dynamic_query.push_back((i32) i);
}
else {
static_query.push_back(-((i32) i + 1));
}
}
for (size_t i = 0; i < M; ++i) {
if (Static[i]) {
static_query.push_back((i32) i);
}
}
std::sort(static_query.begin(), static_query.end(), [](i32 i, i32 j) {
if (i >= 0 && j >= 0) {
return C[i] > C[j];
}
else if (i >= 0) {
if (C[i] == Qs[-(j + 1)].weight) {
return true;
}
return C[i] > Qs[-(j + 1)].weight;
}
else if (j >= 0) {
if (Qs[-(i + 1)].weight == C[j]) {
return false;
}
return Qs[-(i + 1)].weight > C[j];
}
else {
return Qs[-(i + 1)].weight > Qs[-(j + 1)].weight;
}
});
for (auto i: static_query) {
if (i >= 0) {
merge(U[i], V[i]);
}
else {
i = -(i + 1);
std::stack<std::pair<i32, i32>> fix_edges;
for (auto j: dynamic_query) {
if (j > i) {
break;
}
fix_edges.emplace(Qs[j].idx, C[Qs[j].idx]);
C[Qs[j].idx] = Qs[j].weight;
}
std::stack<std::tuple<i32, i32, i32, i32>> fix_nodes;
for (auto j: dynamic_query) {
if (C[Qs[j].idx] < Qs[i].weight) {
continue;
}
i32 u = find(U[Qs[j].idx]);
i32 v = find(V[Qs[j].idx]);
if (u == v) {
continue;
}
if (parent[u] > parent[v]) {
std::swap(u, v);
}
fix_nodes.emplace(u, parent[u], v, parent[v]);
parent[u] += parent[v];
parent[v] = u;
}
answer[i] = -parent[find(Qs[i].idx)];
while (!fix_edges.empty()) {
C[fix_edges.top().first] = fix_edges.top().second;
fix_edges.pop();
}
while (!fix_nodes.empty()) {
const auto &top = fix_nodes.top();
parent[std::get<0>(top)] = std::get<1>(top);
parent[std::get<2>(top)] = std::get<3>(top);
fix_nodes.pop();
}
}
}
for (size_t i = first; i < last; ++i) {
if (Qs[i].type == 1) {
C[Qs[i].idx] = Qs[i].weight;
Static[Qs[i].idx] = true;
}
}
static_query.clear();
dynamic_query.clear();
}
for (auto x: answer) {
if (x != 0) {
std::cout << x << '\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... |