답안 #74287

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
74287 2018-08-30T22:31:24 Z Bruteforceman Regions (IOI09_regions) C++11
70 / 100
8000 ms 80560 KB
#include "bits/stdc++.h"
using namespace std;
int N, R, Q;
int a[200010], p[200010];
const int magic = 106;
int ans[1880][25001];
vector <int> g[200010];

int idx[25001];
int current;
int tot[25001];

void dfs(int x, int cnt) {
	cnt += (current == a[x]);
	if(a[x] != current) {
		ans[idx[current]][a[x]] += cnt;
	}
	for(auto i : g[x]) {
		dfs(i, cnt);
	}
} 

int in[200010];
int out[200010];
vector <int> v[25001];
vector <int> nodes[25001];
int cur;

void go(int x) {
	in[x] = ++cur;
	v[a[x]].emplace_back(in[x]);
	for(auto i : g[x]) {
		go(i);
	}
	out[x] = cur;
}
int count(int l, int r, int val) {
	return upper_bound(v[val].begin(), v[val].end(), r) - lower_bound(v[val].begin(), v[val].end(), l);
}

int main(int argc, char const *argv[])
{
	scanf("%d %d %d", &N, &R, &Q);
	for(int i = 1; i <= N; i++) {
		if(i == 1) scanf("%d", &a[i]);
		else scanf("%d %d", &p[i], &a[i]);
	}
	for(int i = 2; i <= N; i++) {
		g[p[i]].emplace_back(i);
	}
	for(int i = 1; i <= N; i++) {
		tot[a[i]] += 1;
		nodes[a[i]].emplace_back(i);
	}
	int now = 0;
	for(int i = 1; i <= R; i++) {
		if(tot[i] > magic) {
			current = i;
			idx[i] = now++; 
			dfs(1, 0);
		}
	}
	go(1);
	while(Q--) {
		int p, q;
		scanf("%d %d", &p, &q);
		if(tot[p] > magic) {
			printf("%d\n", ans[idx[p]][q]);
		} else {
			int res = 0;
			for(int i : nodes[p]) {
				res += count(in[i], out[i], q);
			}
			printf("%d\n", res);
		}
		fflush(stdout);
	}
	return 0;
}

Compilation message

regions.cpp: In function 'int main(int, const char**)':
regions.cpp:43: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:45:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   if(i == 1) scanf("%d", &a[i]);
              ~~~~~^~~~~~~~~~~~~
regions.cpp:46:13: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   else scanf("%d %d", &p[i], &a[i]);
        ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &q);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6136 KB Output is correct
2 Correct 6 ms 6344 KB Output is correct
3 Correct 8 ms 6344 KB Output is correct
4 Correct 10 ms 6372 KB Output is correct
5 Correct 12 ms 6392 KB Output is correct
6 Correct 23 ms 6392 KB Output is correct
7 Correct 31 ms 6420 KB Output is correct
8 Correct 38 ms 6548 KB Output is correct
9 Correct 44 ms 6932 KB Output is correct
10 Correct 49 ms 6972 KB Output is correct
11 Correct 125 ms 7228 KB Output is correct
12 Correct 146 ms 7740 KB Output is correct
13 Correct 190 ms 7740 KB Output is correct
14 Correct 337 ms 9168 KB Output is correct
15 Correct 220 ms 11340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 921 ms 12112 KB Output is correct
2 Correct 906 ms 12112 KB Output is correct
3 Correct 1279 ms 14032 KB Output is correct
4 Correct 302 ms 14032 KB Output is correct
5 Correct 358 ms 14032 KB Output is correct
6 Correct 625 ms 14032 KB Output is correct
7 Correct 1093 ms 14796 KB Output is correct
8 Correct 1081 ms 21708 KB Output is correct
9 Correct 4637 ms 54172 KB Output is correct
10 Correct 2689 ms 79184 KB Output is correct
11 Correct 5566 ms 79184 KB Output is correct
12 Execution timed out 8009 ms 79184 KB Time limit exceeded
13 Incorrect 4839 ms 79184 KB Unexpected end of file - int32 expected
14 Execution timed out 8015 ms 79184 KB Time limit exceeded
15 Incorrect 3692 ms 80560 KB Unexpected end of file - int32 expected
16 Incorrect 2707 ms 80560 KB Unexpected end of file - int32 expected
17 Incorrect 2627 ms 80560 KB Unexpected end of file - int32 expected