Submission #428644

# Submission time Handle Problem Language Result Execution time Memory
428644 2021-06-15T13:27:36 Z 송준혁(#7532) Counterspells (CPSPC17_counterspells) C++17
100 / 100
303 ms 39188 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define MOD 1000000007
#define INF (1ll<<62)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, num=-1;
int Pa[202020], P[202020];
int W[202020], I[202020], T[202020];
int X[202020], H[202020];
vector<int> adj[202020];
set<int> B[202020];

void upd(int k){k++;while(k<=N+1)T[k]++,k+=k&-k;}
int f(int k){int r=0;k++;while(k)r+=T[k],k^=k&-k;return r;}

void dfs(int u){
	W[u] = 1;
	for (int v : adj[u]) dfs(v), W[u] += W[v];
	sort(adj[u].begin(), adj[u].end(), [&](int a, int b){return W[a] > W[b];});
}

void hld(int u, int h){
	I[u] = ++num, P[I[u]] = I[Pa[u]], H[I[u]] = I[h];
	if (!adj[u].size()) return;
	hld(adj[u][0], h);
	for (int v : adj[u]) if (v != adj[u][0]) hld(v, v);
}

int main(){
	scanf("%d", &N);
	for (int i=1; i<=N; i++) scanf("%d", &Pa[i]), adj[Pa[i]].pb(i);
	dfs(0), hld(0, 0);
	upd(1);
	for (int i=1; i<=N; i++){
		int u = I[i];
		int ans = 0;
		vector<int> up;
		while (u>=0){
			auto it = B[H[u]].lb(u);
			if (it != B[H[u]].begin()){
				int v = *(--it);
				ans += u-v;
				upd(v+1), upd(u+1);
				break;
			}
			int v = H[u];
			ans += u-v+1;
			if (!(f(v)&1) && v) X[P[v]]--;
			upd(v), upd(u+1);
			if (!v || X[P[v]] || (H[P[v]] == H[P[v]+1] && !(f(P[v]+1)&1))) u = -1;
			else u = P[v];
			if (!(f(v)&1) && v) X[P[v]]++;
			up.pb(P[v]);
		}
		for (int v : up){
			if (X[v]) B[H[v]].insert(v);
			else B[H[v]].erase(v);
		}
		printf("%d\n", ans-1);
	}
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
Main.cpp:37:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |  for (int i=1; i<=N; i++) scanf("%d", &Pa[i]), adj[Pa[i]].pb(i);
      |                           ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14520 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 9 ms 14540 KB Output is correct
4 Correct 10 ms 14500 KB Output is correct
5 Correct 10 ms 14532 KB Output is correct
6 Correct 11 ms 14540 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14520 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 9 ms 14540 KB Output is correct
4 Correct 10 ms 14500 KB Output is correct
5 Correct 10 ms 14532 KB Output is correct
6 Correct 11 ms 14540 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
8 Correct 14 ms 15052 KB Output is correct
9 Correct 13 ms 15672 KB Output is correct
10 Correct 13 ms 15052 KB Output is correct
11 Correct 13 ms 14844 KB Output is correct
12 Correct 13 ms 14924 KB Output is correct
13 Correct 16 ms 15124 KB Output is correct
14 Correct 14 ms 15084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14520 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 9 ms 14540 KB Output is correct
4 Correct 10 ms 14500 KB Output is correct
5 Correct 10 ms 14532 KB Output is correct
6 Correct 11 ms 14540 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
8 Correct 14 ms 15052 KB Output is correct
9 Correct 13 ms 15672 KB Output is correct
10 Correct 13 ms 15052 KB Output is correct
11 Correct 13 ms 14844 KB Output is correct
12 Correct 13 ms 14924 KB Output is correct
13 Correct 16 ms 15124 KB Output is correct
14 Correct 14 ms 15084 KB Output is correct
15 Correct 98 ms 19832 KB Output is correct
16 Correct 60 ms 26396 KB Output is correct
17 Correct 56 ms 19792 KB Output is correct
18 Correct 51 ms 17584 KB Output is correct
19 Correct 57 ms 18836 KB Output is correct
20 Correct 74 ms 20116 KB Output is correct
21 Correct 59 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 292 ms 25744 KB Output is correct
2 Correct 120 ms 26312 KB Output is correct
3 Correct 71 ms 19468 KB Output is correct
4 Correct 111 ms 23000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14520 KB Output is correct
2 Correct 10 ms 14540 KB Output is correct
3 Correct 9 ms 14540 KB Output is correct
4 Correct 10 ms 14500 KB Output is correct
5 Correct 10 ms 14532 KB Output is correct
6 Correct 11 ms 14540 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
8 Correct 14 ms 15052 KB Output is correct
9 Correct 13 ms 15672 KB Output is correct
10 Correct 13 ms 15052 KB Output is correct
11 Correct 13 ms 14844 KB Output is correct
12 Correct 13 ms 14924 KB Output is correct
13 Correct 16 ms 15124 KB Output is correct
14 Correct 14 ms 15084 KB Output is correct
15 Correct 98 ms 19832 KB Output is correct
16 Correct 60 ms 26396 KB Output is correct
17 Correct 56 ms 19792 KB Output is correct
18 Correct 51 ms 17584 KB Output is correct
19 Correct 57 ms 18836 KB Output is correct
20 Correct 74 ms 20116 KB Output is correct
21 Correct 59 ms 21084 KB Output is correct
22 Correct 292 ms 25744 KB Output is correct
23 Correct 120 ms 26312 KB Output is correct
24 Correct 71 ms 19468 KB Output is correct
25 Correct 111 ms 23000 KB Output is correct
26 Correct 303 ms 25608 KB Output is correct
27 Correct 155 ms 39188 KB Output is correct
28 Correct 166 ms 26668 KB Output is correct
29 Correct 90 ms 20508 KB Output is correct
30 Correct 116 ms 23076 KB Output is correct
31 Correct 173 ms 26244 KB Output is correct
32 Correct 127 ms 28992 KB Output is correct