QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#290631#2008. 括号树MoRanSky100 ✓46ms36768kbC++23859b2023-12-25 06:12:002023-12-25 06:12:00

Judging History

你现在查看的是最新测评结果

  • [2023-12-25 06:12:00]
  • 评测
  • 测评结果:100
  • 用时:46ms
  • 内存:36768kb
  • [2023-12-25 06:12:00]
  • 提交

answer

#include <cstdio>
#include <iostream>

using namespace std;

const int N = 500005;

typedef long long LL;

int n;

int head[N], numE = 0, f[N], st[N], top, fa[N];

LL cnt[N];

char s[N];

struct E{
	int next, v;
} e[N];

void inline add(int u, int v) {
	e[++numE] = (E) { head[u], v };
	head[u] = numE;
}

void dfs(int u) {
	int tmp = 0;
	if (s[u] == '(') st[++top] = u;
	else if (top) cnt[u] += (f[u] = f[fa[tmp = st[top--]]] + 1);
	for (int i = head[u]; i; i = e[i].next) {
		int v = e[i].v;
		cnt[v] += cnt[u];
		dfs(v);
	}
	if (s[u] == '(') --top;
	else if (tmp) st[++top] = tmp;
}

int main() {
	scanf("%d%s", &n, s + 1);
	for (int i = 2; i <= n; i++)
		scanf("%d", fa + i), add(fa[i], i);
	dfs(1);
	LL ans = 0;
	for (int i = 1; i <= n; i++)
		ans ^= (LL)i * cnt[i];
	printf("%lld\n", ans);
	return 0;
}

详细

Test #1:

score: 5
Accepted
time: 1ms
memory: 7964kb

input:

7
(()()((
1 2 3 4 5 6

output:

15

result:

ok "15"

Test #2:

score: 5
Accepted
time: 1ms
memory: 7836kb

input:

8
)())((()
1 2 3 4 5 6 7

output:

19

result:

ok "19"

Test #3:

score: 5
Accepted
time: 1ms
memory: 7972kb

input:

198
)))())(())())(()))(()))))()(()(()()))())(()()(()()())()()))(())(())())((()((()((()(((())((()())))))))()((()((())(()))))(()))))())(())(())((()()())()))())())(()()((())()())(())())())))))()())(())))((
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3...

output:

3273

result:

ok "3273"

Test #4:

score: 5
Accepted
time: 1ms
memory: 7824kb

input:

200
))((())())())(())))()))())(((()()((((()((()(((((())())(()))())()))((((())((()()(())()()()())(()(((()))))())(()))()(())))(())))))(()(((()((())()((()(((())(()(()))((()((())()((()))))(()(((()))()()((()))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...

output:

21364

result:

ok "21364"

Test #5:

score: 5
Accepted
time: 1ms
memory: 8020kb

input:

1949
(())))((()))(()))()()((())()(()))())((()()))(())))((())))()()(())((())(())()(()((()((((()((())()))()(()(((()((()))((()(())))))()))((((())))()))))()()())(()((()()((())))))))))()(((())((()))()((()(()()(()())))(()(()))()(()()((())(())())())())((((()))(()))((()))()()(((((((())())()))))))()(()(())((...

output:

1098479

result:

ok "1098479"

Test #6:

score: 5
Accepted
time: 1ms
memory: 8008kb

input:

1926
())(((()()()))(()))())()))((((()(((())())())((((())()()))((())(())))))))(())))()))()(()()()))())())()((())())())()))()))())())((()((()(())()()))))(()(()()(()(((()()))(())())))))()()(()()()()())))()()()(()((()(((())())())(()))(())(()))())))((()(()((((()())((((()()((((((()))((((()))))()()()))(())...

output:

531389

result:

ok "531389"

Test #7:

score: 5
Accepted
time: 0ms
memory: 8032kb

input:

2000
))())())()())))(()()(())))))(()((((()(()())())())))(()())))(()))()()())(())()((()()(()()))(())())))()))()(()))())))()(()((()())()))))()))())()(()(()))))(())()(())()(()())()))()()))()))()()))())()()))())(())()())))))))())))))())()))))((()(((((((((((()(()((((()()(())(()))((()))))(())))((()(((()((...

output:

63861

result:

ok "63861"

Test #8:

score: 5
Accepted
time: 0ms
memory: 7872kb

input:

1998
()))((()((()(()))))()((())()())())((((()))((((()()((((((())())))((()))()()))(())()((()))))()(())(()()()(()()())()))())((((())))(()(()()(()))))())((()))(((()((((())(((()((((()()))())(()))))((())()()()()()(((((()()())))))((()())(((()()()())()))))())())((((((()))(((()())(()))))()))))()()()(((()))(...

output:

1416

result:

ok "1416"

Test #9:

score: 5
Accepted
time: 1ms
memory: 7964kb

input:

1999
)))(((()(()()()(()))())((()()()(()))(((()())()))))))))(()()))(((())((()))))())((()(())()(((())))))()(())()(((())((((())))(()())))()))))()((()(((((()(((()))())(()(()(()))((()()()()()(()(())((((()))())(())((((()()()((()(()()())()(((()(()))((()())((((((()(())))()()))))))((((((()((())()())())())(()...

output:

27926

result:

ok "27926"

Test #10:

score: 5
Accepted
time: 1ms
memory: 7888kb

input:

2000
()((()()(((()(()()))()()(((()))((()))()()()(((()()(())((()(((())(()()))()))(((((())()(()()()()))()(((()(())()()())()()))))(()()()()((())()((((((()))))()))()((((()))()))))))(((((((())()))()))(())(()()(()((())))(())))))())))()))))))(()))(()(())())())((((()(((((((()((())((((()(()))((())((()()()())...

output:

10983

result:

ok "10983"

Test #11:

score: 5
Accepted
time: 8ms
memory: 20208kb

input:

99997
()(((()()))()()(())(()))(((()(((())()((())((()))))))))()(((())((())())())())()()(()())()(((()(()))()((((())((())))))))(())((()))((()(())))()((()(()))())()()(())()()()(()()())()((()(((()))))((()())))()((()())((()(()))((()()(()))()())(()()())(()()())()((()()(())())()()(((((()()()()))))))))()()((...

output:

3818846184119

result:

ok "3818846184119"

Test #12:

score: 5
Accepted
time: 9ms
memory: 17696kb

input:

99998
(((()((((()(()))()((()))))))))(()())()()()()()()((()))(()(())()((())((((()((()())()))()((())(()))(())()))))())(()()())()(())()()()((()(((()()))()()()()))((()())))((((((()()(())(()))((())(())))())())))()(()(()))((()))()(((()()))(()((())()))())(())((())(()((((())()((())))()())))(()))((((((((()))...

output:

6201171476965

result:

ok "6201171476965"

Test #13:

score: 5
Accepted
time: 3ms
memory: 15672kb

input:

99999
()(())()(())()()()()(()())()()(())()(()()()()(()()()()(()())(())((((()((()()))((()))())))(()(())))))()(()((()()()((())))))()((()()()()()((()))))()((()))()(())(()(()())()())()(()()()()(()))()()()()((((())(()(())()())())))((()())())()()()((())())((())()(((())())()))((()()))()(())()()(())()()((()...

output:

242124759663

result:

ok "242124759663"

Test #14:

score: 5
Accepted
time: 8ms
memory: 19928kb

input:

100000
()(()())()()((((())((())()(((()))))()())()(()((())()))()()))(((()(((()(())))))))((()()()))()(())()()(((()()(()(()()(()))((((()))))((((()())())()))))))(()(()))()(()()()())(((((((())))))))(((((())))((())())))()(()()(((()(())(((()(())))))(()()))(())))()()(())()()()((())((()()(()))(())))(((((()((...

output:

5113850219609

result:

ok "5113850219609"

Test #15:

score: 5
Accepted
time: 7ms
memory: 18540kb

input:

99999
((()()()(((()())(()))()((())))))(((())())()()())(()()(()))()()()()(()((()()((((())))))))()()()()()(())((()(((((((()(()))()((())()((()(())(())(()()))))))))))))(())()()(()())(())()(())()()((()()((())()((())))))((()))()()()(())(((((()())))))()()(())()((((())()())(((()))())))()(()())()()()()()()()...

output:

2085082970032

result:

ok "2085082970032"

Test #16:

score: 5
Accepted
time: 4ms
memory: 17400kb

input:

100000
()((())(()()())(()()))(()())(())()((()()((())(()((()()(()))))((())))))()((()((()((())())((()((()))))))))()(()())(())()(())()()((((()())))(())(())(()))((())(()((((((()())(((()))))))))))(()(()())())((()()))()(())()()(()((((()))(()()(((((()((())))()())()))()(())())))))()()(()((((())))((((())))))...

output:

2112798541955

result:

ok "2112798541955"

Test #17:

score: 5
Accepted
time: 43ms
memory: 34872kb

input:

500000
(())()((()))(((())))()()()()(())()()(())()(())()(((())))(((())))(())()()()()(())((()()))(((()())))()()()((()()))()((()()))()()()()(())(()())()(()())()(())()()()()()()(())(()()())()()()(()())(((())))()()(())()()()((()))((((()))))(())(())(())()()()()()()()()()()(())()()()()(())(())(())()()()()(...

output:

487466589970316

result:

ok "487466589970316"

Test #18:

score: 5
Accepted
time: 36ms
memory: 34972kb

input:

500000
()()()()((((()))))((()))(())(()(()))()(())(())()()(()()(()))()(()())()((()(())))(())(())()(()())()(())(())((()))()(()(()))()()()(())(())()()()()()()()()(())(()())()(((())))((()))()()()()()()()()()()()()()(())()(()())()(((())))(())()()()(())()(())(((())))()()(((())))()()(())((()))(())()()()(((...

output:

1047922056418682

result:

ok "1047922056418682"

Test #19:

score: 5
Accepted
time: 46ms
memory: 36768kb

input:

500000
()()(())(())()()()(((())((()(()()(()))(()))((()())))))(()(()((((()))()()((()(()(())(((()(((((()))(()())()))))(()))))))())(((())())))(((()))(()()()))))(())(()())()(())(()())()()()()(((()())(())((()(((())))()((((())()())))))())())()()(((()())))(()((())))()()()(()())()(())((()))((()))()()()((()(...

output:

1616950739745800

result:

ok "1616950739745800"

Test #20:

score: 5
Accepted
time: 34ms
memory: 35276kb

input:

500000
()((()(())))()((()())((())()())(())())((())())()((()))(())((()()()))()(()())(()(()))()(()(((((((()()()))((())))))((()())())()((()()((()((()))))())))(())))()()((()(((((()((()((())())))(()))))))))()()()()()()(()()()(()((()))))()((()))(()()()((()(())()()))(((((((()())))()()))())))()()(())((()))(...

output:

1464802790801058

result:

ok "1464802790801058"

Extra Test:

score: 0
Extra Test Passed