-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrac.cpp
More file actions
70 lines (64 loc) · 1.69 KB
/
brac.cpp
File metadata and controls
70 lines (64 loc) · 1.69 KB
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
#include<bits/stdc++.h>
using namespace std;
vector<int> open,close,done;
string s;
void build(int x,int l, int r)
{
//cout<<x<<endl;
if(l>0 && r<=s.size())
{
if(l==r)
{
if(s[l-1] == '(') { open[x] = 1; close[x] = 0; done[x] =0;}
else if(s[l-1] == ')') {close[x] = 1;open[x] = 0; done[x] =0;}
// cout<<x<<endl;
return;
}
else
{
//cout<<"here";
int m = (l+r)/2;
build(2*x,l,m);
build(2*x+1,m+1,r);
int yo = min(open[2*x],close[2*x+1]);
open[x] = open[2*x] + open[2*x+1] - yo;
close[x] = close[2*x] + close[2*x+1] - yo;
done[x] = done[2*x] + done[2*x+1] + 2*yo;
}
}
}
tuple<int,int,int> query(int x, int start, int end, int l, int r)
{
//if(start==end) return make_tuple(done[x],open[x],close[x]);
if(r<start || end<l) return make_tuple(0,0,0);
if(l<=start && r>= end) return make_tuple(done[x],open[x],close[x]);
int mid = (start + end) / 2;
//if(mid>=r) return query(2*x,start,mid,l,r);
//if(mid<l) return query(2*x+1,mid+1,end,l,r);
tuple<int,int,int> p1 = query(2*x, start, mid, l, r);
tuple<int,int,int> p2 = query(2*x+1, mid+1, end, l, r);
int yo = min(get<1>(p1),get<2>(p2));
int d = get<0>(p1) + get<0>(p2) + 2*yo;
int o = get<1>(p1) + get<1>(p2) - yo;
int c = get<2>(p1) + get<2>(p2) - yo;
return make_tuple(d,o,c);
}
int main()
{
cin>>s;
int m;
cin>>m;
int n;
for(n=0;s[n];n++){}
for(int i=0;i<=2*n;i++) {open.push_back(0);close.push_back(0);done.push_back(0);}
build(1,1,n);
int l,r;
for(int i=0;i<m;i++)
{
cin>>l>>r;
cout<<get<0>(query(1,1,n,l,r))<<"\n";
}
//cout<<open[4]<<open[5]<<open[6]<<open[7]<<"\n";
//cout<<close[4]<<close[5]<<close[6]<<close[7]<<"\n";
//cout<<done[4]<<done[5]<<done[6]<<done[7];
}