Submission #566054

#TimeUsernameProblemLanguageResultExecution timeMemory
566054BungmintBridges (APIO19_bridges)C++17
59 / 100
3032 ms5572 KiB
// Copyright © 2022 Youngmin Park. All rights reserved. #pragma GCC optimize("O3") // #pragma GCC target("avx2") #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using vpi = vector<pii>; using pll = pair<ll, ll>; using vl = vector<ll>; using vpl = vector<pll>; using ld = long double; template <typename T, size_t SZ> using ar = array<T, SZ>; template <typename T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define all(v) (v).begin(), (v).end() #define pb push_back #define sz(x) (int)(x).size() #define fi first #define se second #define lb lower_bound #define ub upper_bound constexpr int INF = 1e9; constexpr ll LINF = 1e18; const ld PI = acos((ld)-1.0); constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> constexpr bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> constexpr bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream &operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { os << vector<T>(all(v)); return os; } template <typename T, typename S, typename C> ostream &operator<<(ostream &os, priority_queue<T, S, C> pq) { vector<T> v; while (sz(pq)) { v.pb(pq.top()); pq.pop(); } os << v; return os; } void dbg_out() { cerr << "\033[0m" << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "\033[1;35m(" << #__VA_ARGS__ << "):\033[33m", dbg_out(__VA_ARGS__) #else #define dbg(...) 42 #endif inline namespace RecursiveLambda { template <typename Fun> struct y_combinator_result { Fun fun_; template <typename T> explicit y_combinator_result(T &&fun) : fun_(forward<T>(fun)) {} template <typename... Args> decltype(auto) operator()(Args &&...args) { return fun_(ref(*this), forward<Args>(args)...); } }; template <typename Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun)); } }; inline namespace Range { class ForwardRange { int src, dst; public: explicit constexpr ForwardRange(const int l, const int r) : src(l), dst(r) {} explicit constexpr ForwardRange(const int n) : src(0), dst(n) {} constexpr ForwardRange begin() const { return *this; } constexpr monostate end() const { return {}; } constexpr bool operator!=(monostate) const { return src < dst; } constexpr void operator++() const {} constexpr int operator*() { return src++; } }; class BackwardRange { int src, dst; public: explicit constexpr BackwardRange(const int l, const int r) : src(r), dst(l) {} explicit constexpr BackwardRange(const int n) : src(n), dst(0) {} constexpr BackwardRange begin() const { return *this; } constexpr monostate end() const { return {}; } constexpr bool operator!=(monostate) const { return src > dst; } constexpr void operator++() const {} constexpr int operator*() { return --src; } }; using rep = ForwardRange; using per = BackwardRange; }; // From the USACO tutorial lol template <int N> struct DSU { int e[N]; vector<int> mem; vector<pair<pii, pii>> ev; DSU() { memset(e, -1, sizeof(e)); } // get representive component (uses path compression) // To use rollback, disable path compression inline int get(int x) { return e[x] < 0 ? x : get(e[x]); } bool same_set(int a, int b) { return get(a) == get(b); } int size(int x) { return -e[get(x)]; } bool unite(int x, int y) { // union by size x = get(x), y = get(y); if (x == y) return false; ev.pb({{x, e[x]}, {y, e[y]}}); if (e[x] > e[y]) swap(x, y); e[x] += e[y]; e[y] = x; return true; } void snapshot(){ mem.pb(sz(ev)); } void rollback(){ if (mem.empty()) return; int SZ = mem.back(); mem.pop_back(); while(sz(ev) != SZ){ pair<pii, pii> p = ev.back(); e[p.fi.fi] = p.fi.se; e[p.se.fi] = p.se.se; ev.pop_back(); } } void clear() { memset(e, -1, sizeof(e)); mem.clear(); ev.clear(); } }; struct Edge { int u, v, w; }; struct Query { int type, id, w, time; }; constexpr int SQ = 350, NN = 1e5 + 100; bitset<NN> used; int tmp[NN], ans[NN]; Query queries[SQ][SQ]; int cur[SQ]; vi toJoin[SQ + 100]; Edge edges[NN]; DSU<NN> dsu{}; void solve() { int n, m; cin >> n >> m; for (int i : rep(m)) { int u, v, w; cin >> u >> v >> w; u--, v--; edges[i] = {u, v, w}; } int q; cin >> q; for (int i : rep(q)) { int t, id, w; cin >> t >> id >> w; id--; queries[i / SQ][cur[i / SQ]++] = {t, id, w, i}; } for (int i : rep(SQ)) { if (cur[i] == 0) break; vi upd, calc, unchanged; dsu.clear(); for (int j : rep(cur[i])) { auto &e = queries[i][j]; if (e.type == 1) { upd.pb(j); used[e.id] = 1; }else{ calc.pb(j); } } for (int j : rep(m)) { if (!used[j]) unchanged.pb(j); } for (int j : rep(cur[i])) { auto [ty, id, w, time] = queries[i][j]; if (ty == 1) { edges[id].w = w; }else{ toJoin[j].clear(); for (auto &e : upd) { if (edges[queries[i][e].id].w >= w) toJoin[j].pb(e); } } } sort(all(calc), [&](auto x, auto y) { return queries[i][x].w > queries[i][y].w; }); sort(all(unchanged), [&](auto x, auto y) { return edges[x].w > edges[y].w; }); int ptr = 0; for (auto &e : calc) { auto [ty, id, ww, time] = queries[i][e]; while (ptr < sz(unchanged)) { auto [u, v, w] = edges[unchanged[ptr]]; if (w < ww) break; dsu.unite(u, v); ptr++; } dsu.snapshot(); for (auto &f : toJoin[e]) { auto [u, v, w] = edges[queries[i][f].id]; dsu.unite(u, v); } ans[time] = dsu.size(id); dsu.rollback(); } for (auto &e : upd) used[queries[i][e].id] = 0, edges[queries[i][e].id].w = queries[i][e].w; } for (auto &e : ans) { if (e) cout << e << '\n'; } } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int testcase = 1; // cin >> testcase; while (testcase--) { solve(); } #ifdef LOCAL cerr << "Time elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n"; #endif }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...