Submission #622768

#TimeUsernameProblemLanguageResultExecution timeMemory
622768Do_you_copyBridges (APIO19_bridges)C++17
100 / 100
2244 ms16396 KiB
#include <bits/stdc++.h> #define taskname "test" #define fi first #define se second #define pb push_back #define faster ios_base::sync_with_stdio(0); cin.tie(0); using namespace std; using ll = long long; using ull = unsigned ll; using ld = long double; using pii = pair <int, int>; using pil = pair <int, ll>; using pli = pair <ll, int>; using pll = pair <ll, ll>; mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count()); ll min(const ll &a, const ll &b){ return (a < b) ? a : b; } ll max(const ll &a, const ll &b){ return (a > b) ? a : b; } //const ll Mod = 1000000007; //const ll Mod2 = 999999999989; //only use when required const int maxN = 2e5 + 10; const int maxM = 2e5 + 10; const int maxQ = 2e5 + 10; const int SQRT = 1000; int n, m, q; struct TEdge{ int u, v, w, idx; }; TEdge e[maxM]; struct TQuery{ int t, x, y; }; TQuery query[maxQ]; struct TDSU{ vector <pii> save; vector <int> lab; TDSU(){} TDSU(int _n){ lab.resize(_n, -1); } inline void resize(int _n){ lab.resize(_n, -1); } inline int find_set(int u){ if (lab[u] < 0) return u; return lab[u] = find_set(lab[u]); } inline int find(int u){ if (lab[u] < 0) return u; return find(lab[u]); } inline void merge(int u, int v){ if (lab[u] > lab[v]) swap(u, v); save.pb({v, lab[v]}); lab[u] += lab[v]; lab[v] = u; } inline void undo(){ int v = save.back().fi; int u = lab[v]; lab[v] = save.back().se; lab[u] -= lab[v]; save.pop_back(); } inline void clear(){ lab.clear(); save.clear(); } inline void reset(int _n){ clear(); resize(_n); } inline void roll_back(int _n){ while (save.size() > _n) undo(); } }; TDSU dsu; bool changed[maxM]; int ans[maxQ]; vector <int> join[maxQ]; void Init(){ cin >> n >> m; for (int i = 1; i <= m; ++i){ cin >> e[i].u >> e[i].v >> e[i].w; e[i].idx = i; } vector <int> block_idx; ll cnt = 0; block_idx.pb(0); cin >> q; for (int i = 1; i <= q; ++i){ cin >> query[i].t >> query[i].x >> query[i].y; --query[i].t; ++cnt; if (cnt >= SQRT){ block_idx.pb(i); cnt = 0; } } block_idx.pb(q); //cerr << q << " "; for (int _ = 0; _ < block_idx.size() - 1; ++_){ int l = block_idx[_] + 1, r = block_idx[_ + 1]; dsu.reset(n + 1); vector <int> ask, unchanged, unstable; for (int i = l; i <= r; ++i){ if (query[i].t) ask.pb(i); else{ changed[query[i].x] = 1; unstable.pb(i); } } for (int i = 1; i <= m; ++i){ if (!changed[i]) unchanged.pb(i); else changed[i] = 0; } for (int i = l; i <= r; ++i){ if (!query[i].t) e[query[i].x].w = query[i].y; else{ join[i - l].clear(); for (int j: unstable){ if (e[query[j].x].w >= query[i].y) join[i - l].pb(query[j].x); } } } sort(ask.begin(), ask.end(),[&](const int &i, const int &j){ return query[i].y > query[j].y; }); sort(unchanged.begin(), unchanged.end(),[&](const int &i, const int &j){ return e[i].w > e[j].w; }); int j = 0; for (int __ = 0; __ < ask.size(); ++__){ int i = ask[__]; while (j < unchanged.size() && e[unchanged[j]].w >= query[i].y){ int x = dsu.find_set(e[unchanged[j]].u), y = dsu.find_set(e[unchanged[j]].v); if (x != y){ dsu.merge(x, y); } ++j; } int prev_sz = dsu.save.size(); for (int jj: join[i - l]){ int x = dsu.find(e[jj].u), y = dsu.find(e[jj].v); if (x != y){ dsu.merge(x, y); } } ans[i] = - dsu.lab[dsu.find(query[i].x)]; dsu.roll_back(prev_sz); } } for (int i = 1; i <= q; ++i){ if (query[i].t){ cout << ans[i] << "\n"; //cerr << ans[i] << "\n"; } } } int main(){ if (fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } faster; ll tt = 1; //cin >> tt; while (tt--){ Init(); } }

Compilation message (stderr)

bridges.cpp: In member function 'void TDSU::roll_back(int)':
bridges.cpp:85:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   85 |         while (save.size() > _n) undo();
      |                ~~~~~~~~~~~~^~~~
bridges.cpp: In function 'void Init()':
bridges.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |     for (int _ = 0; _ < block_idx.size() - 1; ++_){
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:146:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |         for (int __ = 0; __ < ask.size(); ++__){
      |                          ~~~^~~~~~~~~~~~
bridges.cpp:148:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |             while (j < unchanged.size() && e[unchanged[j]].w >= query[i].y){
      |                    ~~^~~~~~~~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:177:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  178 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...