-
Notifications
You must be signed in to change notification settings - Fork 0
/
223C.cpp
202 lines (176 loc) · 3.25 KB
/
223C.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#define MOD 1000000007
#define MN(a,b)a<b?a:b
#define N 10100
#define PS system("pause")
#define exit return 0
//#define inf 1000000000000000000LL
//#define left (v<<1)
//#define right (left|1)
#define tm ((tl+tr)>>1)
typedef long long ll;
//#define S 525231
#define R0(i,n) for(int i=0;i<(int)n;i++)
#define R1(i,n) for(int i=1;i<=(int)n;i++)
using namespace std;
struct sg{
int pasL = 0, pasR = 0, pas = 0;
int X, Y, SZ;
sg *left, *right;
sg(){
/*pasL = 0, pasR = 0, pas = 0;
left = right = NULL;*/
}
};
sg *F = new sg;
sg *root;
int getSZ(sg *T){
if (T)return T->SZ;
else return 0;
}
void upSZ(sg *&T){
if (T)T->SZ = getSZ(T->left) + getSZ(T->right) + 1;
}
void UpdateInfo(sg *&T, sg *L, sg *R){
T->pas = L->pas + R->pas + min(L->pasL, R->pasR);
T->pasL = L->pasL + R->pasL - min(L->pasL, R->pasR);
T->pasR = L->pasR + R->pasR - min(L->pasL, R->pasR);
}
void UP(sg *&T){
if (T){
sg *tmp = F;
sg *T1 = F;
T1->pas = T1->pasL = T1->pasR = 0;
if (T->X == 1)T1->pasL = 1;
else T1->pasR = 1;
if (!T->left&&!T->right){
T->pasL = T1->pasL;
T->pasR = T1->pasR;
T->pas = T1->pas;
return;
}
if (!T->left){
UpdateInfo(T, T1, T->right);
return;
}
if (!T->right){
UpdateInfo(T, T->left, T1);
return;
}
UpdateInfo(tmp, T->left, T1);
UpdateInfo(T, tmp, T->right);
}
}
void split(sg *t, sg *&L, sg *&R, int K, int add){
if (!t){
L = R = NULL;
return;
}
int cur_Key = add + getSZ(t->left);
if (cur_Key < K){
split(t->right, t->right, R, K, cur_Key + 1);
L = t;
}
else {
split(t->left, L, t->left, K, add);
R = t;
}
upSZ(t);
UP(t);
}
void merge(sg *&t, sg *L, sg *R){
if (!L)t = R;
else if (!R)t = L;
else if (L->Y > R->Y){
merge(L->right, L->right, R);
t = L;
}
else{
merge(R->left, L, R->left);
t = R;
}
upSZ(t);
UP(t);
}
//void insertIt(sg *&T, sg *it){
// if (!T)T = it;
//
// if (it->Y > T->Y){
// split(T, it->left, it->right, it->X, 0);
// T = it;
// }
// else{
// if (T->X <= it->X)insertIt(T->right, it);
// else insertIt(T->left, it);
// }
//}
//
//void eraseIt(sg *&T, int K){
// if (!T)return;
// if (T->X == K){
// sg *tmp = T;
// merge(T, T->left, T->right);
// free(tmp);
// }
// else if (T->X < K){
// eraseIt(T->right, K);
// }
// else eraseIt(T->left, K);
//}
sg *goit(int K){
sg *on = new sg;
on->X = K;
on->Y = rand();
on->SZ = 1;
on->pas = on->pasL = on->pasR = 0;
if (K == 1)on->pasL = 1;
else on->pasR = 1;
on->left = on->right = NULL;
return on;
}
char in[1000005];
int Q, l, r;
sg *tmp;
int main(){
root = NULL;
scanf("%s", in);
int siz = strlen(in);
for (int i = 0; i < siz; i++){
tmp = goit(((in[i] == '(') ? 1 : 2));
if (!root){
root = tmp;
}
else{
merge(root, root, tmp);
}
}
scanf("%d", &Q);
sg *T1, *T2, *T3, *T4;
while (Q--){
scanf("%d %d", &l, &r);
l--, r--;
split(root, T1, T2, l, 0);
split(T2, T3, T4, r - l + 1, 0);
printf("%d\n", T3->pas * 2);
merge(T2, T3, T4);
merge(root, T1, T2);
}
exit;
//PS;
}