-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRCKTS.cpp
67 lines (60 loc) · 1.75 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
#include<bits/stdc++.h>
using namespace std;
void build(pair<int, int> *segment, int start, int end, int index, string str) {
if (start == end) {
segment[index].first = ((str[start] == '(') ? 1 : -1);
segment[index].second = segment[index].first;
return;
}
int mid = (start + end) / 2;
build(segment, start, mid, 2 * index, str);
build(segment, mid + 1, end, 2 * index + 1, str);
segment[index].first = segment[2 * index].first + segment[2 * index + 1].first;
segment[index].second = min(segment[2 * index].second, segment[2 * index].first + segment[2 * index + 1].second);
}
void update(pair<int, int> *segment, int start, int end, int index, int uIndex) {
if (start == end) {
segment[index].first = -1 * segment[index].first;
segment[index].second = segment[index].first;
return;
}
int mid = (start + end) / 2;
if (uIndex <= mid) {
update(segment, start, mid, 2 * index, uIndex);
} else {
update(segment, mid + 1, end, 2 * index + 1, uIndex);
}
segment[index].first = segment[2 * index].first + segment[2 * index + 1].first;
segment[index].second = min(segment[2 * index].second, segment[2 * index].first + segment[2 * index + 1].second);
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = 1;
while (T <= 10) {
cout << "Test " << T << ":" << endl;
int N;
cin >> N;
string str;
cin >> str;
int M;
cin >> M;
pair<int, int> *segment = new pair<int, int>[4 * N + 1];
build(segment, 0, N - 1, 1, str);
for (int i = 0; i < M; i++) {
int x;
cin >> x;
if (x == 0) {
if (segment[1].first == 0 && segment[1].second == 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
} else {
update(segment, 0, N - 1, 1, x - 1);
}
}
T++;
}
return 0;
}