QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#875176#2008. 括号树Physics212303100 ✓130ms112112kbC++17750b2025-01-29 11:56:232025-01-29 11:56:24

Judging History

This is the latest submission verdict.

  • [2025-01-29 11:56:24]
  • Judged
  • Verdict: 100
  • Time: 130ms
  • Memory: 112112kb
  • [2025-01-29 11:56:23]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
main(){
  ios::sync_with_stdio(false);
  int n,w=0; string s; cin>>n>>s;
  vector<vector<int> > g(n),q(n+1<<1);
  vector<int> r(n),c(n+1<<1),t(n+1<<1,-1);
  for(int i=1;i<n;i++){
    int f; cin>>f;
    g[f-1].emplace_back(i);
  }
  function<void(int,int)> dfs=[&](int u,int p){
    r[u]=c[p]++; int o=t[p];
    if(t[p]=u;~t[p-1])q[t[p-1]].emplace_back(u);
    for(int i:g[u])dfs(i,p+(s[i]=='('?1:-1));
    for(int i:q[u])r[i]-=c[p+1];
    c[p]--,t[p]=o;
  };
  function<void(int)> dfs2=[&](int u){
    for(int i:g[u])r[i]+=r[u],dfs2(i);
  };
  c[n+1]=1,dfs(0,n+(s[0]=='('?1:-1)+1),dfs2(0);
  for(int i=0;i<n;i++)w^=(i+1)*r[i];
  cout<<w<<endl;
  return 0;
}

详细


Pretests


Final Tests

Test #1:

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

input:

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

output:

15

result:

ok "15"

Test #2:

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

input:

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

output:

19

result:

ok "19"

Test #3:

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

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: 0ms
memory: 3584kb

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: 0ms
memory: 4096kb

input:

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

output:

1098479

result:

ok "1098479"

Test #6:

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

input:

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

output:

531389

result:

ok "531389"

Test #7:

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

input:

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

output:

63861

result:

ok "63861"

Test #8:

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

input:

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

output:

1416

result:

ok "1416"

Test #9:

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

input:

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

output:

27926

result:

ok "27926"

Test #10:

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

input:

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

output:

10983

result:

ok "10983"

Test #11:

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

input:

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

output:

3818846184119

result:

ok "3818846184119"

Test #12:

score: 5
Accepted
time: 19ms
memory: 30176kb

input:

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

output:

6201171476965

result:

ok "6201171476965"

Test #13:

score: 5
Accepted
time: 20ms
memory: 30176kb

input:

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

output:

242124759663

result:

ok "242124759663"

Test #14:

score: 5
Accepted
time: 19ms
memory: 30180kb

input:

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

output:

5113850219609

result:

ok "5113850219609"

Test #15:

score: 5
Accepted
time: 18ms
memory: 25184kb

input:

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

output:

2085082970032

result:

ok "2085082970032"

Test #16:

score: 5
Accepted
time: 21ms
memory: 25572kb

input:

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

output:

2112798541955

result:

ok "2112798541955"

Test #17:

score: 5
Accepted
time: 122ms
memory: 111980kb

input:

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

output:

487466589970316

result:

ok "487466589970316"

Test #18:

score: 5
Accepted
time: 120ms
memory: 111596kb

input:

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

output:

1047922056418682

result:

ok "1047922056418682"

Test #19:

score: 5
Accepted
time: 124ms
memory: 112112kb

input:

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

output:

1616950739745800

result:

ok "1616950739745800"

Test #20:

score: 5
Accepted
time: 130ms
memory: 112108kb

input:

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

output:

1464802790801058

result:

ok "1464802790801058"

Extra Test:

score: 0
Extra Test Passed