Submission #1286840

#TimeUsernameProblemLanguageResultExecution timeMemory
1286840SoMotThanhXuan다리 (APIO19_bridges)C++20
73 / 100
3021 ms383020 KiB
#ifdef ONLINE_JUDGE #endif // ONLINE_JUDGE #include <bits/stdc++.h> using namespace std; #define F first #define S second #define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; --i) #define REP(i, n) for(int i = 0, _n = (n); i < _n; ++i) #define mp make_pair #define all(v) v.begin(), v.end() #define uni(v) v.erase(unique(all(v)), v.end()) #define Bit(x, i) ((x >> (i)) & 1) #define Mask(i) (1 << (i)) #define Cnt(x) __builtin_popcount(x) #define Cntll(x) __builtin_popcountll(x) #define Ctz(x) __builtin_ctz(x) #define Ctzll(x) __builtin_ctzll(x) #define Clz(x) __builtin_clz(x) #define Clzll(x) __builtin_clzll(x) #define left __left #define down __down #define right __right #define up __up inline bool maximize(int &u, int v){ return v > u ? u = v, true : false; } inline bool minimize(int &u, int v){ return v < u ? u = v, true : false; } inline bool maximizell(long long &u, long long v){ return v > u ? u = v, true : false; } inline bool minimizell(long long &u, long long v){ return v < u ? u = v, true : false; } const int mod = 1e9 + 7; inline int fastPow(int a, int n){ if(n == 0) return 1; int t = fastPow(a, n >> 1); t = 1ll * t * t % mod; if(n & 1) t = 1ll * t * a % mod; return t; } inline void add(int &u, int v){ u += v; if(u >= mod) u -= mod; } inline void sub(int &u, int v){ u -= v; if(u < 0) u += mod; } const int maxN = 1e5 + 5; const int inf = 1e9; const long long infll = 1e18; string notValid = "Impossible"; int n, m, q; pair<int, int> edge[maxN]; int cen[maxN], reqW[maxN], type[maxN], ans[maxN]; int w[maxN]; int root[maxN], sz[maxN]; int find_root(int v){ while(v != root[v]) v = root[v]; return v; } vector<pair<int, int>> save; bool unite(int u, int v){ u = find_root(u); v = find_root(v); if(u == v) return false; if(sz[u] < sz[v]) swap(u, v); save.emplace_back(v, sz[v]); sz[u] += sz[v]; root[v] = u; return true; } void rollback(){ auto [v, sv] = save.back(); save.pop_back(); int u = root[v]; sz[u] -= sv; root[v] = v; } void reset(){ FOR(i, 1, n)root[i] = i, sz[i] = 1; while(!save.empty()) save.pop_back(); } struct query{ int w, id; query(int _w, int _id){ w = _w; id = _id; } bool operator < (const query &rhs) const{ return w != rhs.w ? w > rhs.w : id > rhs.id; } }; set<query> ask[maxN]; bool change[maxN]; void process(){ cin >> n >> m; FOR(i, 1, n) root[i] = i, sz[i] = 1; set<query> rem; FOR(i, 1, m){ cin >> edge[i].first >> edge[i].second >> w[i]; rem.insert(query(w[i], i)); } cin >> q; FOR(i, 1, q){ cin >> type[i] >> cen[i] >> reqW[i]; } int BLOCK = sqrt(q); for(int l = 1; l <= q; l += BLOCK){ int r = min(q, l + BLOCK - 1); reset(); set<query> ms; set<query> need; FOR(i, l, r){ if(type[i] == 1){ if(!change[cen[i]]){ ms.insert(query(w[cen[i]], cen[i])); change[cen[i]] = true; rem.erase(rem.find(query(w[cen[i]], cen[i]))); } }else{ } } FOR(i, l, r){ if(type[i] == 1){ ms.erase(ms.find(query(w[cen[i]], cen[i]))); w[cen[i]] = reqW[i]; ms.insert(query(w[cen[i]], cen[i])); }else{ ask[i] = ms; need.insert(query(reqW[i], i)); } } auto remit = rem.begin(); auto needit = need.begin(); while(remit != rem.end() && needit != need.end()){ if(remit -> w >= needit -> w){ unite(edge[remit -> id].first, edge[remit -> id].second); ++remit; }else{ int saveSize = save.size(); int curid = needit -> id; for(auto [w, id] : ask[curid]){ if(w >= needit -> w){ unite(edge[id].first, edge[id].second); }else break; } ans[curid] = sz[find_root(cen[curid])]; while(save.size() > saveSize) rollback(); ++needit; } } while(needit != need.end()){ int saveSize = save.size(); int curid = needit -> id; for(auto [w, id] : ask[curid]){ if(w >= needit -> w){ unite(edge[id].first, edge[id].second); }else break; } ans[curid] = sz[find_root(cen[curid])]; while(save.size() > saveSize) rollback(); ++needit; } FOR(i, l, r)if(type[i] == 1){ change[cen[i]] = false; if(w[cen[i]] == reqW[i]){ rem.insert(query(w[cen[i]], cen[i])); } } } FOR(i, 1, q)if(type[i] == 2)cout << ans[i] << '\n'; } #define LOVE "code" int main(){ if(fopen(LOVE".inp", "r")){ freopen(LOVE".inp", "r", stdin); freopen(LOVE".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while(t--) process(); // cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ; return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:170:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |         freopen(LOVE".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen(LOVE".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...