Submission #442422

# Submission time Handle Problem Language Result Execution time Memory
442422 2021-07-07T17:47:30 Z koioi.org-koosaga Counterspells (CPSPC17_counterspells) C++17
100 / 100
773 ms 41068 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], rev[MAXN];
int din[MAXN], dout[MAXN], chn[MAXN], piv;

void hld(int x){
	din[x] = ++piv;
	rev[din[x]] = x;
	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;

set<int> cnd;

pi forward(int x){
	int y = x;
	while(true){
		int nxt = chn[x];
		auto ll = cnd.lower_bound(din[x]);
		while(ll != cnd.begin()){
			ll--;
			if(*ll < din[nxt]) break;
			x = rev[*ll + 1];
			if(bit.query(din[x]) != bit.query(din[p[x]])){
				return pi(x, dep[y] - dep[x]);
			}
			else ll = cnd.erase(ll);
		}
		x = chn[x];
		if(x == 0) break;
		if(bit.query(din[x]) == bit.query(din[p[x]])){
			x = p[x];
		}
		else break;
	}
	return pi(x, dep[y] - dep[x]);
}

void witness(int v){
	if(v == -1) return;
	if(cnd.find(din[v]) != cnd.end()) cnd.erase(din[v]);
	if(bit.query(din[v]) != bit.query(din[v] + 1)) cnd.insert(din[v]);
}

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);
		witness(s);
		s = p[chn[s]];
	}
	bit.add(din[e], +x);
	bit.add(din[s] + 1, -x);
	witness(p[e]);
	witness(s);
}

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);
		}
		if(i) cnd.insert(din[i]);
	}
	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:92:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
Main.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |   scanf("%d",&p[i]);
      |   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5068 KB Output is correct
2 Correct 4 ms 5196 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5068 KB Output is correct
2 Correct 4 ms 5196 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 19 ms 5964 KB Output is correct
9 Correct 12 ms 6732 KB Output is correct
10 Correct 16 ms 5892 KB Output is correct
11 Correct 11 ms 5836 KB Output is correct
12 Correct 12 ms 6032 KB Output is correct
13 Correct 18 ms 6172 KB Output is correct
14 Correct 13 ms 5964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5068 KB Output is correct
2 Correct 4 ms 5196 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 19 ms 5964 KB Output is correct
9 Correct 12 ms 6732 KB Output is correct
10 Correct 16 ms 5892 KB Output is correct
11 Correct 11 ms 5836 KB Output is correct
12 Correct 12 ms 6032 KB Output is correct
13 Correct 18 ms 6172 KB Output is correct
14 Correct 13 ms 5964 KB Output is correct
15 Correct 319 ms 14288 KB Output is correct
16 Correct 106 ms 21952 KB Output is correct
17 Correct 130 ms 14020 KB Output is correct
18 Correct 89 ms 13712 KB Output is correct
19 Correct 123 ms 15176 KB Output is correct
20 Correct 216 ms 16500 KB Output is correct
21 Correct 113 ms 16792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 739 ms 24188 KB Output is correct
2 Correct 290 ms 23780 KB Output is correct
3 Correct 140 ms 18864 KB Output is correct
4 Correct 184 ms 24380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5068 KB Output is correct
2 Correct 4 ms 5196 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 19 ms 5964 KB Output is correct
9 Correct 12 ms 6732 KB Output is correct
10 Correct 16 ms 5892 KB Output is correct
11 Correct 11 ms 5836 KB Output is correct
12 Correct 12 ms 6032 KB Output is correct
13 Correct 18 ms 6172 KB Output is correct
14 Correct 13 ms 5964 KB Output is correct
15 Correct 319 ms 14288 KB Output is correct
16 Correct 106 ms 21952 KB Output is correct
17 Correct 130 ms 14020 KB Output is correct
18 Correct 89 ms 13712 KB Output is correct
19 Correct 123 ms 15176 KB Output is correct
20 Correct 216 ms 16500 KB Output is correct
21 Correct 113 ms 16792 KB Output is correct
22 Correct 739 ms 24188 KB Output is correct
23 Correct 290 ms 23780 KB Output is correct
24 Correct 140 ms 18864 KB Output is correct
25 Correct 184 ms 24380 KB Output is correct
26 Correct 773 ms 25504 KB Output is correct
27 Correct 204 ms 41068 KB Output is correct
28 Correct 291 ms 25304 KB Output is correct
29 Correct 172 ms 22220 KB Output is correct
30 Correct 190 ms 25388 KB Output is correct
31 Correct 412 ms 28984 KB Output is correct
32 Correct 255 ms 31204 KB Output is correct