Submission #1293435

#TimeUsernameProblemLanguageResultExecution timeMemory
1293435guanexBridges (APIO19_bridges)C++20
44 / 100
3020 ms5256 KiB
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; #define endl '\n' //data structures typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<ll> vll; typedef vector<ld> vld; typedef pair<long long, long long> pll; typedef pair<char, int> ci; typedef pair<string, int> si; typedef vector<int> vi; typedef vector<string> vs; typedef vector<vector<int>> vvi; #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define sz(a) ((int)a.size()) #define fi first #define se second #define whole(v) v.begin(), v.end() #define rwhole(v) v.rbegin(), v.rend() #define fro front #define pqueue priority_queue #define ubound upper_bound #define lbound lower_bound #define beg(v) v.begin() //bit operations int flip(int x){ return ~(x) ^ (1 << 32); } int allon(int x){ return (1LL << x) - 1; } bool bit(ll a, ll i){ return (1LL << i) & a; } #define llpc(x) __builtin_popcountll(x) #define ipc(x) __builtin_popcount(x) #define iclz(x) __builtin_clz(x) #define llclz(x) __builtin_clzll(x) #define ictz(x) __builtin_ctz(x) #define llctz(x) __builtin_ctzll(x) //answers #define cYES cout << "YES" << endl #define cYes cout << "Yes" << endl #define cyes cout << "yes" << endl #define cNO cout << "NO" << endl #define cNo cout << "No" << endl #define cno cout << "no" << endl #define ipsb cout << -1 << endl const ll mod2 = 998244353; const ll mod = 1000000007; const int inf = int(1e9); // ll inf = ll(1e18); // read arr vec matr etc #define fill(x, y) memset(x, y, sizeof(x)) void read(vector<int> &x){ for(auto &e:x) cin >> e; } void sread(vector<string> &x){ for(auto &e:x) cin >> e; } void mread(vector<vector<int>> &p, int nnn, int mmm){ for(int i = 0; i < nnn; ++i){ vector<int> pp; for(int j = 0; j < mmm; ++j){ int wq; cin >> wq; pp.pb(wq); } p.pb(pp); } } typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //high quality random number generator using time as seed //int random(int l, int r){return uniform_int_distribution<int>(l,r)(rng);} //returns a randomb number between [l, r] // Solution int sz[100005]; int fat[100005]; stack<int> stck; int ffather(int no){ if(fat[no] == no) return no; return ffather(fat[no]); } void join(int u, int v){ u = ffather(u); v = ffather(v); if(u == v)return; if(sz[u] < sz[v]) swap(u, v); stck.push(v); fat[v] = u; sz[u] += sz[v]; } void roll(int x){ while(sz(stck) > x){ int no = stck.top(); stck.pop(); sz[fat[no]] -= sz[no]; fat[no] = no; } } void tc(){ int n, m; cin >> n >> m; vector<pair<ii, int>> ed; for(int i = 0; i < m; ++i){ int u, v, w; cin >> u >> v >> w; u--; v--; ed.pb({{u, v}, w}); } int t; cin >> t; int sq = 1000; vector<pair<int, ii>> q; for(int i = 0; i < t; ++i){ int a, b, c; cin >> a >> b >> c; q.pb({a, {b, c}}); } bool changed[m+1]; fill(changed, 0); vi ans(t+1, 0); for(int j = 0; j < t; j += sq){ for(int i = 0; i <= n; ++i){ fat[i] = i; sz[i] = 1; } while(stck.size()){ stck.pop(); } int r = min(j + sq, t); vi toans; for(int i = j; i < r; ++i){ if(q[i].fi == 1){ changed[q[i].se.fi-1] = 1; }else{ toans.pb(i); } } vi unchanged; for(int i = 0; i < m; ++i){ if(changed[i] == 0){ unchanged.pb(i); } changed[i] = 0; } sort(whole(unchanged), [&](int u, int v){return ed[u].se > ed[v].se;}); sort(whole(toans), [&](int u, int v){return q[u].se.se > q[v].se.se;}); int p = 0; for(auto e:toans){ int no = q[e].se.fi-1; int w = q[e].se.se; for(; p < sz(unchanged); ++p){ //cout << p << " " << unchanged[p] << endl; if(ed[unchanged[p]].se >= w){ join(ed[unchanged[p]].fi.fi, ed[unchanged[p]].fi.se); }else{ break; } } //cout << e << " " << no << ' ' << w << " " << j << " " << r << endl; int prevsz = sz(stck); map<int, int> change; for(int i = j; i < e; ++i){ if(q[i].fi == 1){ int bridge = q[i].se.fi-1; int wei = q[i].se.se; change[bridge] = wei; } } for(int i = e+1; i < r; ++i){ if(q[i].fi == 1){ int bridge = q[i].se.fi-1; int wei = ed[bridge].se; if(!change.count(bridge)){ change[bridge] = wei; } } } for(auto o:change){ if(o.se >= w){ join(ed[o.fi].fi.fi, ed[o.fi].fi.se); } } int an = sz[ffather(no)]; ans[e] = an; roll(prevsz); } for(int i = j; i < r; ++i){ if(q[i].fi == 1){ ed[q[i].se.fi-1].se = q[i].se.se; } } } for(int i = 0; i < t; ++i){ if(q[i].fi == 2){ cout << ans[i] << endl; } } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while(t--){ tc(); } }

Compilation message (stderr)

bridges.cpp: In function 'int flip(int)':
bridges.cpp:42:22: warning: left shift count >= width of type [-Wshift-count-overflow]
   42 |     return ~(x) ^ (1 << 32);
      |                    ~~^~~~~
#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...