-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathC.cpp
More file actions
90 lines (78 loc) · 2.25 KB
/
C.cpp
File metadata and controls
90 lines (78 loc) · 2.25 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <complex>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>
using namespace std;
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
string s;
int n;
const int MN = 1000111;
struct Node {
int len, open, close;
Node(char c) : len(0), open(c == '('), close(c == ')') {}
Node(int len = 0, int open = 0, int close = 0) : len(len), open(open), close(close) {}
Node operator + (Node a) {
int can = min(open, a.close);
return Node(len + a.len + can,
open - can + a.open,
close + a.close - can);
}
} it[MN * 8];
#define CT(X) ((X) << 1)
#define CP(X) (CT(X) + 1)
void build(int i, int l, int r) {
if (l == r) {
it[i] = Node(s[l]);
return ;
}
int mid = (l + r) >> 1;
build(CT(i), l, mid);
build(CP(i), mid+1, r);
it[i] = it[CT(i)] + it[CP(i)];
}
Node get(int i, int l, int r, int u, int v) {
if (v < l || r < u) return Node();
if (u <= l && r <= v) return it[i];
int mid = (l + r) >> 1;
return get(CT(i), l, mid, u, v) + get(CP(i), mid+1, r, u, v);
}
int main() {
ios :: sync_with_stdio(false); cin.tie(NULL);
cout << (fixed) << setprecision(6);
while (cin >> s) {
n = s.length();
s = " " + s;
build(1, 1, n);
int q; cin >> q;
while (q--) {
int u, v; cin >> u >> v;
Node cur = get(1, 1, n, u, v);
cout << 2 * cur.len << "\n";
}
}
return 0;
}