Submission #777743

#TimeUsernameProblemLanguageResultExecution timeMemory
777743KihihihiBridges (APIO19_bridges)C++17
14 / 100
1949 ms18704 KiB
#include <iostream> #include <iomanip> #include <algorithm> #include <numeric> #include <cmath> #include <cassert> #include <ctime> #include <chrono> #include <cstdio> #include <random> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <deque> #include <queue> #include <bitset> #include <list> #include <fstream> #include <functional> #include <complex> using namespace std; mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count()); const long long INF = 1e18, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 20; const long double EPS = 1e-9, PI = acos(-1); const long long SQ = 1000; struct edge { long long a, b, w; }; struct dsu { long long n; vector<long long> p, rk, sz; vector<vector<pair<long long*, long long>>> hist; dsu(long long in) { n = in; p.resize(n); iota(p.begin(), p.end(), 0); rk.resize(n); sz.resize(n, 1); } long long rt(long long v) { return (p[v] == v ? v : rt(p[v])); } void unite(long long a, long long b) { a = rt(a); b = rt(b); hist.push_back({}); if (a == b) { return; } hist.back().reserve(4); if (rk[a] < rk[b]) { swap(a, b); } hist.back().push_back({ &p[b], p[b] }); p[b] = a; hist.back().push_back({ &sz[a], sz[a] }); sz[a] += sz[b]; hist.back().push_back({ &sz[b], sz[b] }); sz[b] = 0; hist.back().push_back({ &rk[a], rk[a] }); rk[a] += (rk[a] == rk[b]); } void ctrlz() { while (hist.back().size()) { *hist.back().back().first = hist.back().back().second; hist.back().pop_back(); } } long long get(long long v) { return sz[rt(v)]; } }; void solve() { long long n, m; cin >> n >> m; vector<edge> e(m); for (long long i = 0; i < m; i++) { cin >> e[i].a >> e[i].b >> e[i].w; e[i].a--; e[i].b--; } long long q; cin >> q; for (long long qil = 0; qil < q; qil += SQ) { long long qir = qil + SQ - 1; qir = min(qir, q - 1); long long qlen = qir - qil + 1; vector<bool> used(m); vector<tuple<long long, long long, long long>> qr(qlen); for (long long i = qil; i < qir + 1; i++) { long long qt; cin >> qt; if (qt == 1) { long long idx, nw; cin >> idx >> nw; idx--; used[idx] = true; qr[i - qil] = { qt, idx, nw }; } else { long long ver, w; cin >> ver >> w; ver--; qr[i - qil] = { qt, ver, w }; } } vector<long long> ch, rest; for (long long i = 0; i < m; i++) { if (used[i]) { ch.push_back(i); } else { rest.push_back(i); } } vector<vector<long long>> add; vector<tuple<long long, long long, long long>> ask; for (long long i = 0; i < qlen; i++) { auto [qt, qx, qy] = qr[i]; if (qt == 1) { e[qx].w = qy; continue; } ask.push_back({ qy, qx, add.size() }); add.push_back({}); for (auto& j : ch) { if (e[j].w >= qy) { add.back().push_back(j); } } } sort(ask.rbegin(), ask.rend()); sort(rest.begin(), rest.end(), [&](long long x, long long y) { return e[x].w > e[y].w; } ); dsu kek(n); vector<long long> ans(ask.size()); long long ptr = 0; for (auto& [w, ver, idx] : ask) { while (ptr < rest.size() && e[rest[ptr]].w >= w) { kek.unite(e[rest[ptr]].a, e[rest[ptr]].b); ptr++; } for (auto& i : add[idx]) { kek.unite(e[i].a, e[i].b); } ans[idx] = kek.get(ver); for (long long _ = 0; _ < add[idx].size(); _++) { kek.ctrlz(); } } for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << "\n"; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(time(NULL)); int tst = 1; //cin >> tst; while (tst--) { solve(); //cout << "----------------------------------------\n"; } return 0; } /* <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 ⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀ ⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀ ⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀ ⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀ ⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀ ⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇ ⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇ ⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁ ⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀ ⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀ ⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀ ⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀ ⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀ ⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀ ⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀ ⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀ ⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀ ⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧ ⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘ ⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀ ⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀ <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 */

Compilation message (stderr)

bridges.cpp: In function 'void solve()':
bridges.cpp:186:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  186 |             while (ptr < rest.size() && e[rest[ptr]].w >= w)
      |                    ~~~~^~~~~~~~~~~~~
bridges.cpp:197:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  197 |             for (long long _ = 0; _ < add[idx].size(); _++)
      |                                   ~~^~~~~~~~~~~~~~~~~
bridges.cpp:202:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  202 |         for (long long i = 0; i < ans.size(); i++)
      |                               ~~^~~~~~~~~~~~
#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...