Submission #484678

#TimeUsernameProblemLanguageResultExecution timeMemory
484678Killer2501Bridges (APIO19_bridges)C++14
14 / 100
1424 ms11492 KiB
#include <bits/stdc++.h> #define ll int #define ld long double #define ull unsigned long long #define pb push_back #define pll pair<ll, ll> #define pii pair<ll, pll> #define fi first #define se second using namespace std; const int N = 2e5+5; const int M = 405; const ll mod = 1e9; const ll base = 350; const ll inf = 1e9 ; ll n, m, t, q, k, ans, a[N], b[N], tong, in[N], out[N], res[N], x[N], y[N], w[N], id[N]; vector<ll> adj[N]; struct DSU { ll n; vector<ll> lab; vector<pair<pll, pll>> snap; DSU(ll _n) { n = _n; lab.resize(n+1, -1); } ll size() { return snap.size(); } ll findp(ll u) { return lab[u] < 0 ? u : findp(lab[u]); } void hop(ll x, ll y) { ll u = findp(x), v = findp(y); if(u == v)return; if(lab[u] > lab[v])swap(u, v); snap.pb({{u, lab[u]}, {v, lab[v]}}); lab[u] += lab[v]; lab[v] = u; } void rollback(ll sz) { while(snap.size() > sz) { auto x = snap.back(); snap.pop_back(); lab[x.fi.fi] = x.fi.se; lab[x.se.fi] = x.se.se; } } }; vector<ll> ve; bool cmp(const ll& x, const ll& y) { return w[x] > w[y]; } void sol() { cin >> n >> m; for(int i = 1; i <= m; i ++) { cin >> x[i] >> y[i] >> w[i]; id[i] = i; in[i] = i; out[i] = inf; ve.pb(i); } cin >> q; for(int i = m+1; i <= m+q; i ++) { cin >> x[i] >> y[i] >> w[i]; if(x[i] == 1) { out[id[y[i]]] = i-1; id[y[i]] = i; in[i] = i; out[i] = inf; ve.pb(i); } } //for(int i = 1; i <= m+q; i ++)cout << in[i] <<" "<<out[i] << '\n'; DSU dsu(n); sort(ve.begin(), ve.end(), cmp); for(int l = m+1; l <= m+q; l += base) { int r = min(m+q, l+base-1); vector<ll> fixed, query, kq; for(int i = l; i <= r; i ++)if(x[i] == 2)query.pb(i); sort(query.begin(), query.end(), cmp); for(int i: ve) { if(in[i] <= l && out[i] >= r)fixed.pb(i); else if((in[i] <= l && l <= out[i]) || (in[i] <= r && r <= out[i]))kq.pb(i); } int j = 0; for(int i: query) { //cout << i << " "; while(j < (int) fixed.size() && w[fixed[j]] >= w[i]) { //cout << fixed[j] << " "; if(fixed[j] <= m)dsu.hop(x[fixed[j]], y[fixed[j]]); else dsu.hop(x[y[fixed[j]]], y[y[fixed[j]]]); ++j; } k = dsu.size(); for(int p: kq) { if(w[p] < w[i])break; if(in[p] <= i && i <= out[p]) { //cout << p << " "; if(p <= m)dsu.hop(x[p], y[p]); else dsu.hop(x[y[p]], y[y[p]]); } } //cout << '\n'; res[i] = -dsu.lab[dsu.findp(y[i])]; dsu.rollback(k); } dsu.rollback(0); } for(int i = m+1; i <= m+q; i ++)if(x[i] == 2)cout << res[i] << '\n'; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); #define task "tests" if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } int test = 1; //cin >> test; while(test -- > 0)sol(); return 0; }

Compilation message (stderr)

bridges.cpp: In member function 'void DSU::rollback(int)':
bridges.cpp:48:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   48 |         while(snap.size() > sz)
      |               ~~~~~~~~~~~~^~~~
bridges.cpp: In function 'int main()':
bridges.cpp:139:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:140:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |   freopen(task".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...