Submission #566025

#TimeUsernameProblemLanguageResultExecution timeMemory
566025BungmintBridges (APIO19_bridges)C++17
73 / 100
3070 ms20256 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 struct DSU { vector<int> e; vector<int> mem; vector<pair<pii, pii>> ev; DSU(int N) { e = vector<int>(N, -1); } // get representive component (uses path compression) // To use rollback, disable path compression 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(); } } }; struct Edge { int u, v, w; }; struct Query { int type, id, w, time; bool operator<(const Query& other) const { return w > other.w; } }; ostream& operator<<(ostream& os, const Query& A) { os << "(" << A.type << ", " << A.id << ", " << A.w << ", " << A.time << ")"; return os; } bitset<101000> isupd; constexpr int SQ = 450, NN = 1e5 + 100; int tmp[NN]; void solve() { int n, m; cin >> n >> m; vector<Edge> edges(m); vector<vector<Query>> queries(SQ); vector<vi> sorted_edges; vi ind, ans; for (int i : rep(m)) { int u, v, w; cin >> u >> v >> w; u--, v--; edges[i] = {u, v, w}; ind.pb(w); } int q; cin >> q; ans.resize(q); for (int i : rep(q)) { int t, id, w; cin >> t >> id >> w; id--; queries[i / SQ].pb({t, id, w, i}); ind.pb(w); } sort(all(ind)), ind.resize(unique(all(ind)) - ind.begin()); sorted_edges.resize(sz(ind)); for (int i : rep(m)) { auto &[u, v, w] = edges[i]; w = lb(all(ind), w) - ind.begin(); sorted_edges[w].pb(i); } dbg(sorted_edges); for (int i : rep(SQ)) { if (queries[i].empty()) break; vector<Query> upd, calc; DSU dsu(n); for (auto &e : queries[i]) { e.w = lb(all(ind), e.w) - ind.begin(); if (e.type == 1) { upd.pb(e); isupd[e.id] = 1; }else{ calc.pb(e); } } sort(all(calc)); dbg(upd, calc, ind); int ptr = sz(ind); for (auto &e : calc) { while (ptr && ptr - 1 >= e.w) { ptr--; for (auto &id : sorted_edges[ptr]) { if (!isupd[id] && edges[id].w == ptr) { dsu.unite(edges[id].u, edges[id].v); } } } for (auto &f : upd) { tmp[f.id] = -1; } for (auto &f : upd) { if (f.time > e.time) continue; tmp[f.id] = f.w; } dsu.snapshot(); for (auto &f : upd) { if (f.time < e.time && tmp[f.id] >= e.w) { dsu.unite(edges[f.id].u, edges[f.id].v); }else if (tmp[f.id] == -1 && edges[f.id].w >= e.w) { dsu.unite(edges[f.id].u, edges[f.id].v); } } ans[e.time] = dsu.size(e.id); dsu.rollback(); } for (auto &e : upd) isupd[e.id] = 0, edges[e.id].w = e.w, sorted_edges[e.w].pb(e.id); } 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 }

Compilation message (stderr)

bridges.cpp: In function 'void solve()':
bridges.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
bridges.cpp:224:2: note: in expansion of macro 'dbg'
  224 |  dbg(sorted_edges);
      |  ^~~
bridges.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
bridges.cpp:239:3: note: in expansion of macro 'dbg'
  239 |   dbg(upd, calc, ind);
      |   ^~~
#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...