Submission #476900

#TimeUsernameProblemLanguageResultExecution timeMemory
476900dooompyBridges (APIO19_bridges)C++17
0 / 100
1618 ms60692 KiB
#include "bits/stdc++.h" using namespace std; using ll = long long; const int B = 1000; int n, m, q; vector<pair<int, int>> adj[500005]; int u[500005], v[500005], d[500005]; int t[100005], x[100005], y[100005]; bool changed[100005]; vector<int> to_add[B]; stack<int> stck; int sz[100001], cmp[100001]; int ans[100001]; void reset() { fill(sz, sz+100001, 1); iota(cmp + 1, cmp + 1 + n, 1); } int find(int a) { if (a != cmp[a]) a = cmp[a]; return a; } void onion(int a, int b) { a = find(a), b = find(b); if(a == b) return; if (sz[a] > sz[b]) swap(a, b); stck.push(a); sz[b] += sz[a]; cmp[a] = cmp[b]; } void revert(int x) { while (stck.size() > x) { int cur = stck.top(); stck.pop(); sz[cmp[cur]] -= sz[cur]; cmp[cur] = cur; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("", "r", stdin); // freopen("", "w", stdout); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> u[i] >> v[i] >> d[i]; } cin >> q; for (int i = 1; i <= q; i++) { cin >> t[i] >> x[i] >> y[i]; } for (int l = 1; l <= q; l += B) { int r = min(q + 1, l + B); fill(changed + 1, changed + m + 1, false); reset(); vector<int> ask, upd, unchanged; for (int i = l; i < r; i++) { if (t[i] == 1) { changed[x[i]] = true; upd.push_back(i); } else { ask.push_back(i); } } for (int i = 1; i <= m; i++) { if (!changed[i]) unchanged.push_back(i); } for (int i = l; i < r; i++) { if (t[i] == 1) { d[x[i]] = y[i]; } else { to_add[i - l].clear(); for (auto a : upd) { if (d[x[a]] >= y[i]) to_add[i - l].push_back(x[a]); } } } sort(ask.begin(), ask.end(), [&](int a, int b) { return y[a] > y[b]; }); sort(unchanged.begin(), unchanged.end(), [&](int a, int b) { return d[a] > d[b]; }); int ptr = 0; for (int i : ask) { while (ptr < unchanged.size() && d[unchanged[ptr]] >= y[i]) { onion(v[unchanged[ptr]], u[unchanged[ptr]]); ptr++; } int now = stck.size(); for (auto a : to_add[i - l]) { onion(v[a], u[a]); } ans[i] = sz[find(x[i])]; revert(now); } } for (int i = 1; i <= q; i++) { if (t[i] == 2) cout << ans[i] << "\n"; } }

Compilation message (stderr)

bridges.cpp: In function 'void revert(int)':
bridges.cpp:42:24: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |     while (stck.size() > x) {
      |            ~~~~~~~~~~~~^~~
bridges.cpp: In function 'int main()':
bridges.cpp:106:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |             while (ptr < unchanged.size() && d[unchanged[ptr]] >= y[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...