Submission #571235

#TimeUsernameProblemLanguageResultExecution timeMemory
571235YouAreMyGalaxyBridges (APIO19_bridges)C++17
100 / 100
2766 ms133492 KiB
//Make CSP great again //Vengeance #include <bits/stdc++.h> #define TASK "TESTCODE" #define Log2(x) 31 - __builtin_clz(x) using namespace std; const int block = 1000, M = 1e5, N = 5e4; stack<tuple<int, int, int > > st; int lab[N + 1], n, m, a[M + 1], b[M + 1], w[M + 1], t[M + 1], x[M + 1], y[M + 1], q, res[M + 1]; bitset<M + 1> changed; vector<int> upt[M + 1]; int FindSet(int u) { return lab[u] < 0 ? u : FindSet(lab[u]); } void Unite(int u, int v) { u = FindSet(u); v = FindSet(v); if (u == v) { return ; } if (lab[u] > lab[v]) { swap(u, v); } lab[u] += lab[v]; st.push(make_tuple(u, v, lab[v])); lab[v] = u; } void roll_back(int sz) { while(st.size() > sz) { int u, v, s; tie(u, v, s) = st.top(); st.pop(); lab[u] -= s; lab[v] = s; } } void read() { cin >> n >> m; for (int i = 1; i <= m; ++ i) { cin >> a[i] >> b[i] >> w[i]; } cin >> q; for (int i = 1; i <= q; ++ i) { cin >> t[i] >> x[i] >> y[i]; } } void solve() { for (int l = 1; l <= q; l+= block) { int r = min(q, l + block - 1); changed.reset(); memset(lab, -1, sizeof(lab)); while(!st.empty()) { st.pop(); } vector<int> tmp, query; for (int i = l; i <= r; ++ i) { if (t[i] == 1) { changed[x[i]] = true; tmp.push_back(x[i]); } } vector<int> unchanged; for (int i = 1; i <= m; ++ i) { if (!changed[i]) { unchanged.push_back(i); } } sort(unchanged.begin(), unchanged.end(), [&](const int &a, const int &b) { return w[a] < w[b]; }); for (int i = l; i <= r; ++ i) { if (t[i] == 1) { w[x[i]] = y[i]; //tmp.push_back(i); } else { for (int j : tmp) { if (w[j] >= y[i]) { upt[i].push_back(j); } } query.push_back(i); } } sort(query.begin(), query.end(), [&](const int & a, const int &b) { return y[a] > y[b]; }); for (int i : query) { //cerr << i << '\n'; while(!unchanged.empty() && w[unchanged.back()] >= y[i]) { //cerr << unchanged.back() << ' '; Unite(a[unchanged.back()], b[unchanged.back()]); unchanged.pop_back(); } //cerr << '\n'; int sz = st.size(); for (int j : upt[i]) { Unite(a[j], b[j]); //cerr << j << ' '; } //cerr << '\n'; res[i] = -lab[FindSet(x[i])]; roll_back(sz); //cerr << '\n'; } } for (int i = 1; i <= q; ++ i) { if (t[i] == 1) { continue; } cout << res[i] << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); //freopen(TASK".OUT", "w", stdout); } int t = 1; bool typetest = false; if (typetest) { cin >> t; } for (int __ = 1; __ <= t; ++ __) { //cout << "Case " << __ << ": "; read(); solve(); } }

Compilation message (stderr)

bridges.cpp: In function 'void roll_back(int)':
bridges.cpp:34:21: warning: comparison of integer expressions of different signedness: 'std::stack<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   34 |     while(st.size() > sz)
      |           ~~~~~~~~~~^~~~
bridges.cpp: In function 'int main()':
bridges.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...