Submission #768831

#TimeUsernameProblemLanguageResultExecution timeMemory
768831fanwenSynchronization (JOI13_synchronization)C++17
100 / 100
251 ms24956 KiB
/** * author : pham van sam * created : 28 June 2023 (Wednesday) **/ #include <bits/stdc++.h> using namespace std; #define MASK(x) (1LL << (x)) #define BIT(x, i) (((x) >> (i)) & 1) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0, _n = n; i < _n; ++i) #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 FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it) template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; } template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; } void you_make_it(void); signed main() { #define TASK "synchronization" if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); auto start_time = chrono::steady_clock::now(); int test = 1; // cin >> test; for (int i = 1; i <= test; ++i) { you_make_it(); // cout << '\n'; } auto end_time = chrono::steady_clock::now(); cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl; return (0 ^ 0); } template <class T> struct Fenwick_Tree { vector<T> bit; int n; Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){} void clear() { fill(bit.begin(), bit.end(), T(0)); } void update(int u, T val) { for (; u <= n; u += u & -u) bit[u] += val; } T get(int u) { T ans = 0; for (; u; u -= u & -u) ans += bit[u]; return ans; } T get(int l, int r) { return get(r) - get(l - 1); } }; const int MAXN = 1e5 + 2; Fenwick_Tree <int> bit; int N, M, Q, anc[MAXN][21], active[MAXN]; vector <int> adj[MAXN]; int infor[MAXN], last_sync[MAXN]; pair <int, int> edges[MAXN]; int time_in[MAXN], time_out[MAXN]; void dfs(int u, int p) { anc[u][0] = p; FOR(i, 1, 20) anc[u][i] = anc[anc[u][i - 1]][i - 1]; static int run = 0; infor[u] = 1; time_in[u] = ++run; for (auto v : adj[u]) if(v != p) dfs(v, u); time_out[u] = run; } int find_anc(int u) { int lca = u; FORD(i, 20, 0) if(anc[lca][i] != 0 && bit.get(time_in[anc[lca][i]]) == bit.get(time_in[u])) { lca = anc[lca][i]; } return lca; } void you_make_it() { cin >> N >> M >> Q; FOR(i, 1, N - 1) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); edges[i] = make_pair(u, v); } dfs(1, 0); bit = Fenwick_Tree <int> (N); FOR(i, 1, N) { bit.update(time_in[i], -1); bit.update(time_out[i] + 1, 1); } while(M--) { int x; cin >> x; auto [u, v] = edges[x]; if(anc[u][0] == v) swap(u, v); if(active[x]) { infor[v] = last_sync[v] = infor[find_anc(u)]; bit.update(time_in[v], -1); bit.update(time_out[v] + 1, 1); } else { infor[find_anc(u)] += infor[v] - last_sync[v]; bit.update(time_in[v], 1); bit.update(time_out[v] + 1, -1); } active[x] = !active[x]; } while(Q--) { int x; cin >> x; cout << infor[find_anc(x)] << '\n'; } } // Dream it. Wish it. Do it.

Compilation message (stderr)

synchronization.cpp: In instantiation of 'Fenwick_Tree<T>::Fenwick_Tree(int) [with T = int]':
synchronization.cpp:73:20:   required from here
synchronization.cpp:51:9: warning: 'Fenwick_Tree<int>::n' will be initialized after [-Wreorder]
   51 |     int n;
      |         ^
synchronization.cpp:50:15: warning:   'std::vector<int> Fenwick_Tree<int>::bit' [-Wreorder]
   50 |     vector<T> bit;
      |               ^~~
synchronization.cpp:52:5: warning:   when initialized here [-Wreorder]
   52 |     Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){}
      |     ^~~~~~~~~~~~
synchronization.cpp: In function 'int main()':
synchronization.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         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...