Submission #442416

# Submission time Handle Problem Language Result Execution time Memory
442416 2021-07-07T17:39:03 Z koioi.org-koosaga Counterspells (CPSPC17_counterspells) C++17
60 / 100
1000 ms 16668 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
const int mod = 1e9 + 7;
 
int n, p[MAXN], dep[MAXN], cnt[MAXN];
vector<int> gph[MAXN];

int sz[MAXN];
int din[MAXN], dout[MAXN], chn[MAXN], piv;

void hld(int x){
	din[x] = ++piv;
	if(sz(gph[x])){
		chn[gph[x][0]] = chn[x];
		hld(gph[x][0]);
		for(int i = 1; i < sz(gph[x]); i++){
			chn[gph[x][i]] = gph[x][i];
			hld(gph[x][i]);
		}
	}
	dout[x] = piv;
}

struct bit{
	int tree[MAXN];
	void add(int x, int v){
		x += 2;
		for(int i = x; i < MAXN; i += i & -i) tree[i] += v;
	}
	int query(int x){
		x += 2;
		int ret = 0;
		for(int i = x; i; i -= i & -i) ret += tree[i];
		return ret;
	}
}bit;

pi forward(int x){
	int ret = 0;
	while(~p[x] && bit.query(din[x]) == bit.query(din[p[x]])){
		x = p[x];
		ret++;
	}
	return pi(x, ret);
}

void add_until(int s, int e, int x){
	if(s == -1) return;
	if(e == -1) e = 0;
	while(chn[s] != chn[e]){
		bit.add(din[chn[s]], +x);
		bit.add(din[s] + 1, -x);
		s = p[chn[s]];
	}
	bit.add(din[e], +x);
	bit.add(din[s] + 1, -x);
}

int main(){
	scanf("%d",&n);
	p[0] = -1;
	for(int i = 1; i <= n; i++){
		scanf("%d",&p[i]);
		dep[i] = dep[p[i]] + 1;
		gph[p[i]].push_back(i);
	}
	for(int i = n; i; i--){
		sz[i]++;
		if(~p[i]) sz[p[i]] += sz[i];
		sort(all(gph[i]), [&](const int &a, const int &b){
			return sz[a] > sz[b];
		});
	}
	hld(0);
	for(int i = 0; i <= n; i++){
		if(dep[i] % 2 == 1){
			bit.add(din[i], +1);
			bit.add(din[i] + 1, -1);
		}
	}
	for(int i = 1; i <= n; i++){
		int ans = 0;
		if(dep[i] % 2){
			int j = i;
			if(bit.query(din[p[j]]) == 0){
				tie(j, ans) = forward(p[j]);
				ans++;
			}
			add_until(p[i], p[j], 1);
		}
		else{
			int j = i;
			if(bit.query(din[p[j]]) == 1){
				tie(j, ans) = forward(p[j]);
				ans++;
			}
			add_until(p[i], p[j], -1);
		}
		printf("%d\n", ans);
	}
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
Main.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |   scanf("%d",&p[i]);
      |   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 6 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5096 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 6 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5096 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 11 ms 5452 KB Output is correct
9 Correct 496 ms 6344 KB Output is correct
10 Correct 7 ms 5452 KB Output is correct
11 Correct 7 ms 5324 KB Output is correct
12 Correct 9 ms 5440 KB Output is correct
13 Correct 22 ms 5672 KB Output is correct
14 Correct 60 ms 5556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 6 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5096 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 11 ms 5452 KB Output is correct
9 Correct 496 ms 6344 KB Output is correct
10 Correct 7 ms 5452 KB Output is correct
11 Correct 7 ms 5324 KB Output is correct
12 Correct 9 ms 5440 KB Output is correct
13 Correct 22 ms 5672 KB Output is correct
14 Correct 60 ms 5556 KB Output is correct
15 Correct 63 ms 9316 KB Output is correct
16 Execution timed out 1081 ms 16668 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 149 ms 14056 KB Output is correct
2 Correct 94 ms 13820 KB Output is correct
3 Correct 58 ms 10608 KB Output is correct
4 Correct 91 ms 14268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 6 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5096 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 11 ms 5452 KB Output is correct
9 Correct 496 ms 6344 KB Output is correct
10 Correct 7 ms 5452 KB Output is correct
11 Correct 7 ms 5324 KB Output is correct
12 Correct 9 ms 5440 KB Output is correct
13 Correct 22 ms 5672 KB Output is correct
14 Correct 60 ms 5556 KB Output is correct
15 Correct 63 ms 9316 KB Output is correct
16 Execution timed out 1081 ms 16668 KB Time limit exceeded
17 Halted 0 ms 0 KB -