Submission #1087934

#TimeUsernameProblemLanguageResultExecution timeMemory
1087934Zero_OPRegions (IOI09_regions)C++14
0 / 100
223 ms78988 KiB
#include<bits/stdc++.h> using namespace std; #define sz(v) (int)v.size() #define all(v) begin(v), end(v) #define compact(v) v.erase(unique(all(v)), end(v)) #define dbg(v) "[" #v " = " << (v) << "]" #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define rep(i, l, r) for(int i = (l); i < (r); ++i) using ll = long long; using vi = vector<int>; using vl = vector<long long>; using ld = long double; template<typename T> bool minimize(T& a, const T& b){ if(a > b){ return a = b, true; } return false; } template<typename T> bool maximize(T& a, const T& b){ if(a < b){ return a = b, true; } return false; } template<int dimension, typename T> struct tensor : public vector<tensor<dimension - 1, T>> { static_assert(dimension > 0, "Dimension must be positive !\n"); template<typename... Args> tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {} }; template<typename T> struct tensor<1, T> : public vector<T> { tensor(int n = 0, T val = T()) : vector<T>(n, val) {} }; const int MAXN = 2e5 + 5; const int MAXR = 500 * 505; const int B = 500; int N, R, Q, H[MAXN], r1[MAXN], r2[MAXN]; vector<int> adj[MAXN]; bool heavy[MAXR]; vector<int> inRegion[MAXR]; int sum[MAXN], target; ll total_sum[MAXR]; void dfs(int u, vector<ll>& total_sum){ total_sum[H[u]] += sum[u]; sum[u] += (H[u] == target); for(int v : adj[u]){ sum[v] = sum[u]; dfs(v, total_sum); } } int timerDFS, tin[MAXN], tout[MAXN]; vector<int> pos[MAXR]; vector<ll> save[MAXR]; void init_dfs(int u){ tin[u] = ++timerDFS; pos[H[u]].push_back(tin[u]); for(int v : adj[u]){ init_dfs(v); } tout[u] = timerDFS; } int count(int i, int l, int r){ return upper_bound(all(pos[i]), r) - lower_bound(all(pos[i]), l); } void testcase(){ cin >> N >> R >> Q; for(int i = 1; i <= N; ++i){ if(i > 1){ int p; cin >> p; adj[p].push_back(i); } cin >> H[i]; inRegion[H[i]].push_back(i); } for(int i = 1; i <= R; ++i){ if(sz(inRegion[i]) > B){ heavy[i] = true; sum[1] = 0; save[i].resize(N + 1); target = i; dfs(1, save[i]); } else{ heavy[i] = false; } } init_dfs(1); for(int i = 1; i <= Q; ++i){ int r1, r2; cin >> r1 >> r2; if(heavy[r1]){ cout << save[r1][r2] << '\n'; } else{ ll ans = 0; for(int u : inRegion[r1]){ ans += count(r2, tin[u], tout[u]); } if(r1 == r2){ //2x times ans -= sz(inRegion[r1]); } cout << ans << '\n'; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); file("task"); int T = 1; // cin >> T; while(T--){ testcase(); } return 0; }

Compilation message (stderr)

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