Submission #355565

#TimeUsernameProblemLanguageResultExecution timeMemory
355565parsabahramiRegions (IOI09_regions)C++17
100 / 100
5068 ms80748 KiB
// Call my Name and Save me from The Dark #include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") using namespace std; typedef long long int ll; typedef pair<int, int> pii; #define SZ(x) (int) x.size() #define F first #define S second const int N = 2e5 + 10, SQ = 1500; map<int, int> Cup[N], C[N]; vector<pii> vec[N]; int A[N], S[N], St[N], Ft[N], n, R, q, tim; vector<int> adj[N]; void preDFS(int v) { St[v] = tim++; for (int &u : adj[v]) { preDFS(u); } Ft[v] = tim; } void DFS(int v, int p = -1) { if (~p) { Cup[v] = Cup[p]; if (S[A[p]] > SQ) Cup[v][A[p]]++; } for (int &u : adj[v]) { DFS(u, v); } } int main() { scanf("%d%d%d%d", &n, &R, &q, &A[1]); for (int i = 2; i <= n; i++) { int p; scanf("%d%d", &p, &A[i]); adj[p].push_back(i); } preDFS(1); for (int i = 1; i <= n; i++) vec[A[i]].push_back({St[i], i}); for (int i = 1; i <= R; i++) sort(vec[i].begin(), vec[i].end()), S[i] = SZ(vec[i]); DFS(1); for (int i = 1; i <= n; i++) { for (pii x : Cup[i]) C[A[i]][x.F] += x.S; Cup[i].clear(); } for (; q; q--) { int r1, r2; scanf("%d%d", &r1, &r2); if (S[r1] > SQ) printf("%d\n", C[r2][r1]); else { int ret = 0; for (pii x : vec[r1]) { int v = x.S; int l = lower_bound(vec[r2].begin(), vec[r2].end(), pii(St[v], -1e9)) - vec[r2].begin(); int r = upper_bound(vec[r2].begin(), vec[r2].end(), pii(Ft[v], -1e9)) - vec[r2].begin(); ret += r - l; } printf("%d\n", ret); } fflush(stdout); } return 0; }

Compilation message (stderr)

regions.cpp: In function 'int main()':
regions.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   37 |     scanf("%d%d%d%d", &n, &R, &q, &A[1]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:39:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |         int p; scanf("%d%d", &p, &A[i]);
      |                ~~~~~^~~~~~~~~~~~~~~~~~~
regions.cpp:51:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   51 |         int r1, r2; scanf("%d%d", &r1, &r2);
      |                     ~~~~~^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...