Submission #736218

#TimeUsernameProblemLanguageResultExecution timeMemory
736218marvinthangRegions (IOI09_regions)C++17
100 / 100
2216 ms40956 KiB
/************************************* * author: marvinthang * * created: 05.05.2023 16:50:15 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } // end of template const int MAX = 2e5 + 5; const int MAX_R = 25e3 + 3; const int BLOCK_SIZE = 500; int N, R, Q, H[MAX]; vector <int> adj[MAX], List[MAX_R]; vector <pair <int, int>> List_f[MAX_R]; int id[MAX_R], res1[MAX / BLOCK_SIZE + 5][MAX_R], cnt[MAX_R], res2[MAX / BLOCK_SIZE + 5][MAX_R], numBig; int tin[MAX], tout[MAX], treeSize[MAX]; void init(void) { cin >> N >> R >> Q; FORE(i, 1, N) { if (i > 1) { int p; cin >> p; adj[p].push_back(i); } cin >> H[i]; ++cnt[H[i]]; } } void update(int u, int add) { if (~id[H[u]]) cnt[id[H[u]]] += add; for (int v: adj[u]) update(v, add); } void dfs(int u) { static int timeDfs = 0; tin[u] = ++timeDfs; if (~id[H[u]]) ++cnt[id[H[u]]]; List_f[H[u]].emplace_back(tin[u] - 1, -1); List[H[u]].push_back(tin[u]); REP(i, numBig) res1[i][H[u]] += cnt[i]; treeSize[u] = 1; for (int v: adj[u]) { dfs(v); treeSize[u] += treeSize[v]; } if (~id[H[u]]) --cnt[id[H[u]]]; tout[u] = timeDfs; List_f[H[u]].emplace_back(tout[u], 1); } void dfs2(int u) { int heavy = -1; for (int v: adj[u]) if (heavy == -1 || treeSize[heavy] < treeSize[v]) heavy = v; for (int v: adj[u]) if (v != heavy) { dfs2(v); update(v, -1); } if (heavy != -1) dfs2(heavy); for (int v: adj[u]) if (v != heavy) update(v, 1); if (~id[H[u]]) ++cnt[id[H[u]]]; REP(i, numBig) res2[i][H[u]] += cnt[i]; } void process(void) { memset(id, -1, sizeof(id)); FORE(i, 1, R) { if (cnt[i] > BLOCK_SIZE) id[i] = numBig++; cnt[i] = 0; } dfs(1); dfs2(1); while (Q--) { int a, b; cin >> a >> b; if (~id[a]) cout << res1[id[a]][b] << endl; else if (~id[b]) cout << res2[id[b]][a] << endl; else { int res = 0, i = 0; for (auto [u, f]: List_f[a]) { while (i < (int) List[b].size() && List[b][i] <= u) ++i; res += f * i; } cout << res << endl; } } } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); file("regions"); init(); process(); // cerr << "Time elapsed: " << TIME << " s.\n"; return (0^0); }

Compilation message (stderr)

regions.cpp: In function 'int main()':
regions.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:127:2: note: in expansion of macro 'file'
  127 |  file("regions");
      |  ^~~~
regions.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:127:2: note: in expansion of macro 'file'
  127 |  file("regions");
      |  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...