forked from love1024/spoj-solution-with-explanation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBRCKTS.cpp
105 lines (88 loc) · 2.45 KB
/
BRCKTS.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
105
#include<bits/stdc++.h>
using namespace std;
//Node of segment tree
class Node {
public:
int left,right;
Node(int left,int right) {
this->left = left;
this->right = right;
}
Node() {}
};
//Get size of segment tree
int getSize(int n) {
int sze = 1;
while(sze < n) {
sze <<= 1;
}
return sze<<1;
}
//Make segment tree
void makeTree(Node seg[],string &str,int p,int i,int j) {
if(i >= j) {
if(str[i] == '(')
seg[p] = Node(1,0);
else
seg[p] = Node(0,1);
return;
}
int mid = (i+j)/2;
//Call left and right subtree
makeTree(seg,str,2*p+1,i,mid);
makeTree(seg,str,2*p+2,mid+1,j);
//Find minimum of bracket left from left and bracket right from right
//As these will form closed pair
int mn = min(seg[2*p+1].left,seg[2*p+2].right);
//Add new node to left and right brackets and subtract those
//which will form pair
seg[p] = Node(seg[2*p+1].left + seg[2*p+2].left - mn,seg[2*p+2].right + seg[2*p+1].right - mn);
}
//Update the index or reveres and update its parents
void update(Node seg[],int p,int i,int j,int s,int e) {
if(i == s && j == e) {
seg[p].left = seg[p].left?0:1;
seg[p].right = seg[p].right?0:1;
return ;
}
int mid = (i+j)/2;
if(e <= mid)
update(seg,2*p+1,i,mid,s,e);
else if(s > mid)
update(seg,2*p+2,mid+1,j,s,e);
else {
update(seg,2*p+1,i,mid,s,mid);
update(seg,2*p+2,mid+1,j,mid+1,j);
}
int mn = min(seg[2*p+1].left,seg[2*p+2].right);
seg[p] = Node(seg[2*p+1].left + seg[2*p+2].left - mn,seg[2*p+2].right + seg[2*p+1].right - mn);
}
int main(){
//Loop over all test cases
int t = 10,n,m,q,k=1;
string str;
while(t--) {
//Get number of nodes,string and number of queries
cin>>n>>str>>m;
cout<<"Test "<<k<<":"<<endl;
//Get size of segment tree and make segment tree
int sze = getSize(n);
Node seg[sze+1];
makeTree(seg,str,0,0,n-1);
//Loop over all queries
int q;
for(int i=0;i<m;i++) {
//Take query and act accordingly
cin>>q;
if(q == 0) {
if(seg[0].left == 0 && seg[0].right == 0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
} else {
update(seg,0,0,n-1,q-1,q-1);
}
}
k++;
}
}