-
Notifications
You must be signed in to change notification settings - Fork 0
/
d.cpp
158 lines (119 loc) · 3.39 KB
/
d.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
#include <bits/stdc++.h>
using namespace std;
// clang-format off
// trace template and ostream overloads
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << (H);
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
template<typename C,
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename std::enable_if<!std::is_same<C, std::string>::value>::type* = nullptr
>
std::ostream &operator<<(std::ostream &os, const C &container){
bool first = true;
std::stringstream ss;
ss << '[';
for(const auto &x : container){
if (!first){
ss << ", ";
}
first = false;
ss << x;
}
ss << ']';
return os << ss.str();
}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << '{' << p.first << ", " << p.second << '}'; return os;}
// stdout
#define db(x) cout << '>' << #x << ':' << x << endl;
// stderr
#ifndef ONLINE_JUDGE
#define tr(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define tr(...) ;
#endif
#define f first
#define s second
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define sz(x) ((int)(x).size())
#define nl cout << "\n"
#define all(v) v.begin(), v.end()
#define ll long long int
#define ld long double
#define vll vector<long long>
#define vvll vector<vll>
#define pll pair<long long, long long>
#define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// input overloads
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
// clang-format on
ll tc, n, m, k;
const ll mxN = 5e4 + 7;
const ll mxM = 5e4 + 7;
const ll mod = 998244353;
const ll inf = 1e18;
const ll mxK = 500+7;
int main() {
fast_io();
#ifndef ONLINE_JUDGE
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
// freopen("../output.txt", "w", stderr);
#endif
string str; cin>>str;
n = sz(str);
vll sop(n+1), scp(n+1);
stack<ll> st;
rep(i, 1, n+1) {
if(str[i-1] == '(') st.push(i);
else {
if(!st.empty()) {
sop[st.top()] = 1;
scp[i] = 1;
st.pop();
}
}
}
vll prefsop(n+1), prefscp(n+1);
rep(i, 1, n+1) {
prefsop[i] = prefsop[i-1]+sop[i];A
prefscp[i] = prefscp[i-1]+scp[i];
}
// db(prefscp);
// db(prefsop);
auto qry = [&](ll L, ll R) {
ll ans = 2*(max(0LL, prefscp[R]-prefsop[L-1]));
return ans;
};
cin>>tc;
while(tc--) {
ll L, R;
cin>>L>>R;
cout<<qry(L, R);
nl;
}
#ifndef ONLINE_JUDGE
cerr << "\nExecution time: " << 1.0 * clock() / CLOCKS_PER_SEC
<< " seconds.\n";
#endif
return 0;
}