Logo Wy Online Judge

WyOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#991#208. 「CSP-S2019」括号树protractor1001231ms65336kbC++14886b2025-07-05 10:09:042025-07-05 11:19:05

answer

#include<iostream>
#include<vector>
using namespace std;
int n;
int f[500050];
vector<int> v[500050];
long long dp[500050];
int d[500050];
string s;
char c[500050];
int f_[500050];
int k[500050];
int cnt;
int e[500050];
long long ans;
void dfs(int x,int l,int p)
{
	d[x]=d[f[x]]+1;
	//cout<<x<<' '<<d[x]<<' '<<l<<' ';
	if(s[x]==')'&&p>0&&c[p]=='(')
	{
		k[x]=k[f[e[p]]]+1;
		p=f_[p];
		l=d[x];
		dp[x]=dp[f[x]]+k[x];
	}
	else
	{
		cnt++;
		c[cnt]=s[x];
		e[cnt]=x;
		f_[cnt]=p;
		p=cnt;
		dp[x]=dp[f[x]];
	}
	ans^=dp[x]*x;
	//cout<<k[x]<<' '<<dp[x]<<' '<<ans<<'\n';
	//for(int i=1;i<=cnt;i++) cout<<c[tp];
	//cout<<'\n';
	for(auto i : v[x])
	{
		dfs(i,l,p);
	}
	while(e[p]>=d[x]) p=f_[p];
}
int main()
{
	cin>>n>>s;
	s=" "+s;
	for(int i=2;i<=n;i++)
	{
		cin>>f[i];
		v[f[i]].push_back(i);
	}
	dfs(1,0,0);
	cout<<ans;
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

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

input:

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

output:

15

result:

ok "15"

Test #2:

score: 5
Accepted
time: 2ms
memory: 25992kb

input:

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

output:

19

result:

ok "19"

Test #3:

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

input:

198
)))())(())())(()))(()))))()(()(()()))())(()()(()()())()()))(())(())())((()((()((()(((())((()()))...

output:

3273

result:

ok "3273"

Test #4:

score: 5
Accepted
time: 2ms
memory: 25768kb

input:

200
))((())())())(())))()))())(((()()((((()((()(((((())())(()))())()))((((())((()()(())()()()())(()(...

output:

21364

result:

ok "21364"

Test #5:

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

input:

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

output:

1098479

result:

ok "1098479"

Test #6:

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

input:

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

output:

531389

result:

ok "531389"

Test #7:

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

input:

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

output:

63861

result:

ok "63861"

Test #8:

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

input:

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

output:

1416

result:

ok "1416"

Test #9:

score: 5
Accepted
time: 5ms
memory: 25920kb

input:

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

output:

27926

result:

ok "27926"

Test #10:

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

input:

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

output:

10983

result:

ok "10983"

Test #11:

score: 5
Accepted
time: 39ms
memory: 37216kb

input:

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

output:

3818846184119

result:

ok "3818846184119"

Test #12:

score: 5
Accepted
time: 44ms
memory: 37200kb

input:

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

output:

6201171476965

result:

ok "6201171476965"

Test #13:

score: 5
Accepted
time: 41ms
memory: 37172kb

input:

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

output:

242124759663

result:

ok "242124759663"

Test #14:

score: 5
Accepted
time: 38ms
memory: 37212kb

input:

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

output:

5113850219609

result:

ok "5113850219609"

Test #15:

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

input:

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

output:

2085082970032

result:

ok "2085082970032"

Test #16:

score: 5
Accepted
time: 44ms
memory: 33824kb

input:

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

output:

2112798541955

result:

ok "2112798541955"

Test #17:

score: 5
Accepted
time: 236ms
memory: 65120kb

input:

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

output:

487466589970316

result:

ok "487466589970316"

Test #18:

score: 5
Accepted
time: 238ms
memory: 64832kb

input:

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

output:

1047922056418682

result:

ok "1047922056418682"

Test #19:

score: 5
Accepted
time: 234ms
memory: 65336kb

input:

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

output:

1616950739745800

result:

ok "1616950739745800"

Test #20:

score: 5
Accepted
time: 237ms
memory: 65300kb

input:

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

output:

1464802790801058

result:

ok "1464802790801058"