Submission #53628

#TimeUsernameProblemLanguageResultExecution timeMemory
53628baactreeRegions (IOI09_regions)C++17
3 / 100
8069 ms75436 KiB
#include <bits/stdc++.h> using namespace std; int n, r, q; vector<int> adj[200005]; int arr[200005]; const int sz = 467; int dp[sz][25005]; pair<int,int> cnt[25005]; int inv[25005]; int le[200005], ri[200005]; vector<int> idx[25005]; vector<pair<int, int> > vec[25005]; int vn; void dfs(int now, int par) { idx[arr[now]].push_back(vn); le[now] = vn++; for (auto &there : adj[now]) { if (there == par)continue; dfs(there, now); } ri[now] = vn++; vec[arr[now]].emplace_back(le[now], ri[now]); } int main() { scanf("%d%d%d", &n, &r, &q); scanf("%d", &arr[1]); cnt[arr[1]].first++; for (int i = 2; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); adj[a].push_back(i); adj[i].push_back(a); arr[i] = b; cnt[arr[i]].first++; } for (int i = 0; i < 25005; i++) cnt[i].second = i; sort(cnt, cnt + 25005, [&](pair<int, int> &a, pair<int, int> &b) { return a > b; }); for (int i = 0; i < 25005; i++) inv[cnt[i].second] = i; dfs(1, 0); for (int i = 0; i < 25005; i++) sort(vec[i].begin(), vec[i].end()); for (int i = 0; i < sz; i++) { int a = cnt[i].second; for (int b = 1; b <= r; b++) { int ans = 0; int lidx = 0, ridx = 0; for (auto x : vec[a]) { int le = x.first; int ri = x.second; while (lidx < idx[b].size() && idx[b][lidx] < le)lidx++; while (ridx < idx[b].size() && idx[b][ridx] <= ri)ridx++; ans += ridx - lidx; } dp[i][b] = ans; } } while (q--) { int a, b; scanf("%d%d", &a, &b); if (inv[a] < sz) { printf("%d\n", dp[inv[a]][b]); fflush(stdout); } else { int ans = 0; for (auto x : vec[a]) { int le = x.first; int ri = x.second; ans += upper_bound(idx[b].begin(), idx[b].end(), ri) - lower_bound(idx[b].begin(), idx[b].end(), le); } printf("%d\n", ans); fflush(stdout); } } return 0; }

Compilation message (stderr)

regions.cpp: In function 'int main()':
regions.cpp:54:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (lidx < idx[b].size() && idx[b][lidx] < le)lidx++;
            ~~~~~^~~~~~~~~~~~~~~
regions.cpp:55:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (ridx < idx[b].size() && idx[b][ridx] <= ri)ridx++;
            ~~~~~^~~~~~~~~~~~~~~
regions.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &n, &r, &q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
regions.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &arr[1]);
  ~~~~~^~~~~~~~~~~~~~~
regions.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
regions.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...