-
Notifications
You must be signed in to change notification settings - Fork 9
/
BRCKTS - Brackets.cpp
104 lines (103 loc) · 2.38 KB
/
BRCKTS - Brackets.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<bits/stdc++.h>
using namespace std;
#define mx 30005
char str[mx];
struct node
{
int match;
int unopen;
int unclose;
}tree[mx*4];
node zero;
node merge(node l, node r)
{
if(l.match == -1) return r;
if(r.match == -1) return l;
node ret;
int add = min(l.unopen, r.unclose);
ret.match = l.match + r.match + add*2;
ret.unclose = l.unclose + r.unclose - add;
ret.unopen = l.unopen + r.unopen - add;
return ret;
}
void build(int pos, int b, int e)
{
if(b>e) return;
if(b==e){
if(str[b]=='('){
tree[pos].match = 0;
tree[pos].unopen = 1;
tree[pos].unclose = 0;
}
else{
tree[pos].match = 0;
tree[pos].unopen = 0;
tree[pos].unclose = 1;
}
return;
}
int l = pos*2 +1;
int r = l+1;
int mid =(b+e)/2;
build(l,b,mid);
build(r,mid+1,e);
tree[pos] = merge(tree[l], tree[r]);
}
node query(int pos, int b, int e, int i, int j)
{
if(b>j || e<i) return zero;
if(b>=i && e<=j) return tree[pos];
int l = pos*2 +1;
int r = l+1;
int mid =(b+e)/2;
node n1 = query(l,b,mid,i,j);
node n2 = query(r,mid+1,e,i,j);
return merge(n1,n2);
}
void update(int pos, int b, int e, int i, int j)
{
if(b>e || b>j || e<i) return;
if(b>=i && e<=j){
if(str[b]==')'){
tree[pos].match = 0;
tree[pos].unopen = 1;
tree[pos].unclose = 0;
str[i] = '(';
}
else{
tree[pos].match = 0;
tree[pos].unopen = 0;
tree[pos].unclose = 1;
str[i] = ')';
}
return;
}
int l = pos*2 +1;
int r = l+1;
int mid =(b+e)/2;
update(l,b,mid,i,j);
update(r,mid+1,e,i,j);
tree[pos] = merge(tree[l], tree[r]);
}
int main()
{
zero.match = zero.unclose = zero.unopen = -1;
int len;
int n,a,cs=1;
while(scanf("%d",&len)!=EOF){
scanf("%s",str);
build(0,0,len-1);
scanf("%d",&n);
printf("Test %d:\n",cs++);
while(n--){
scanf("%d",&a);
if(a==0){
printf("%s\n",tree[0].match == len ? "YES" : "NO");
}
else{
update(0,0,len-1,a-1,a-1);
}
}
}
return 0;
}