Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#934#208. 「CSP-S2019」括号树ryp100431ms44116kbC++141.9kb2025-07-04 17:10:462025-07-04 17:10:47

answer

#include<cstdio>
#include<iostream>
#include<cstring>
#include<utility>
#include<algorithm>
#include<stack>
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

const int maxn=1111111;
int tot,fa[maxn],fir[maxn],nxt[maxn],to[maxn],f[maxn],A[maxn],g[maxn];
LL sum[maxn];
char ss[maxn];
int N;

void add_edge(int x,int y)
{
	nxt[++tot]=fir[x],to[fir[x]=tot]=y;
}
void work(){
	stack<int>s;
	int fro;
	for(int i=1;i<=N;i++){
		if(A[i]==1)s.push(i);
		else 
			if(A[i]==-1){
				if(s.size()){
					s.pop();
					if(s.size())fro=s.top();
					else fro=0;
					f[i]=g[fro]+1;
					g[fro]++;
				}
				else {
					//s.clear();
					while(s.size())s.pop();
					g[0]=0;
				}
				
			}
	} 
	for(int i=1;i<=N;i++){
	//	cout<<f[i]<<" ";
		sum[i]=sum[i-1]+f[i];
	}
//	cout<<endl;		
}

void dfs1(int x)
{
	int y=fa[x];
	if(A[x]==-1&&A[y]==1)
	{   int yy=fa[y];
		f[x]=f[yy]+1;//...()一对括号 x与y匹配 
		if(f[yy]) g[x]=g[yy];//记录x前并列的括号的位置 
		else g[x]=yy;
	}
	if(A[x]==-1&&A[y]==-1&&A[g[y]]==1)//(...)) x与g[y]匹配 
	{
		int yy=fa[g[y]]; 
		f[x]=1+f[yy];
		if(f[yy]) g[x]=g[yy];
		else g[x]=yy;
	}
	
	for(int i=fir[x],v=to[i];i;i=nxt[i],v=to[i]) 
		dfs1(v);
//	sum[x]=sum[fa[x]]+f[x];
}
void dfs2(int x)
{
	sum[x]=sum[fa[x]]+f[x];
	for(int i=fir[x],v=to[i];i;i=nxt[i],v=to[i]) 
		dfs2(v);
}

int main()
{
//	freopen("brackets.in","r",stdin);
//	freopen("brackets.out","w",stdout);
	int flag=1;
	scanf("%d",&N) ;
	scanf("%s",ss+1);
	for(int i=1;i<=N;++i)
	{
		if(ss[i]=='(') A[i]=1;
		else A[i]=-1;
	}
	for(int i=2;i<=N;++i) {
		scanf("%d",&fa[i]);
		add_edge(fa[i],i);
		if(fa[i]!=i-1)flag=0;
	}
	if(flag){
		work();
	} 
	else {
		dfs1(1);
		dfs2(1);
	}
	
	LL ans=0;
	for(int i=1;i<=N;++i) ans^=sum[i]*i;
	printf("%lld",ans);
	return 0;
	
}

详细

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

Test #1:

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

input:

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

output:

15

result:

ok "15"

Test #2:

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

input:

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

output:

19

result:

ok "19"

Test #3:

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

input:

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

output:

3273

result:

ok "3273"

Test #4:

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

input:

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

output:

21364

result:

ok "21364"

Test #5:

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

input:

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

output:

1098479

result:

ok "1098479"

Test #6:

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

input:

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

output:

531389

result:

ok "531389"

Test #7:

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

input:

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

output:

63861

result:

ok "63861"

Test #8:

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

input:

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

output:

1416

result:

ok "1416"

Test #9:

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

input:

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

output:

27926

result:

ok "27926"

Test #10:

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

input:

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

output:

10983

result:

ok "10983"

Test #11:

score: 5
Accepted
time: 12ms
memory: 20936kb

input:

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

output:

3818846184119

result:

ok "3818846184119"

Test #12:

score: 5
Accepted
time: 10ms
memory: 20688kb

input:

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

output:

6201171476965

result:

ok "6201171476965"

Test #13:

score: 5
Accepted
time: 13ms
memory: 20732kb

input:

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

output:

242124759663

result:

ok "242124759663"

Test #14:

score: 5
Accepted
time: 14ms
memory: 20784kb

input:

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

output:

5113850219609

result:

ok "5113850219609"

Test #15:

score: 5
Accepted
time: 16ms
memory: 22288kb

input:

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

output:

2085082970032

result:

ok "2085082970032"

Test #16:

score: 5
Accepted
time: 6ms
memory: 22460kb

input:

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

output:

2112798541955

result:

ok "2112798541955"

Test #17:

score: 5
Accepted
time: 90ms
memory: 44004kb

input:

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

output:

487466589970316

result:

ok "487466589970316"

Test #18:

score: 5
Accepted
time: 85ms
memory: 43880kb

input:

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

output:

1047922056418682

result:

ok "1047922056418682"

Test #19:

score: 5
Accepted
time: 85ms
memory: 43928kb

input:

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

output:

1616950739745800

result:

ok "1616950739745800"

Test #20:

score: 5
Accepted
time: 90ms
memory: 44116kb

input:

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

output:

1464802790801058

result:

ok "1464802790801058"