#include <bits/stdc++.h>
using namespace std;
const int MX = 2e6 + 5;
int N;
vector<int> g[MX];
bool type[MX];
int n, m;
pair<int, int> dfs(int v) {
// cout << "traverse " << v << " type " << type[v] << '\n';
if(g[v].empty()) {
return make_pair(1, m);
}
auto [l1, r1] = dfs(g[v][0]);
auto [l2, r2] = dfs(g[v][1]);
assert(1 <= l1 && r1 <= m);
assert(1 <= l2 && r2 <= m);
if(!type[v]) {
return make_pair(min(l1, l2), min(r1, r2) - (m - max(r1, r2)) - 1);
} else {
return make_pair(max(l1, l2) + min(l1, l2), max(r1, r2));
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
string s;
cin >> s;
N = s.size();
vector<int> v;
for(int i = 0; i < N; i++) {
// cout << i << " " << s[i] << '\n';
if(s[i] == '?') {
m++;
n++;
if(n > 1)
g[v.back()].push_back(n);
continue;
}
if(s[i] == ')') {
v.pop_back();
}
if(s[i] != 'm') continue;
if(s[i + 1] == 'a') {
n++;
type[n] = 1;
if(n > 1)
g[v.back()].push_back(n);
v.push_back(n);
} else {
n++;
type[n] = 0;
if(n > 1)
g[v.back()].push_back(n);
v.push_back(n);
}
}
auto [l, r] = dfs(1);
// cout << l << " " << r << '\n';
cout << r - l + 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47252 KB |
Output is correct |
3 |
Correct |
24 ms |
47296 KB |
Output is correct |
4 |
Correct |
26 ms |
47296 KB |
Output is correct |
5 |
Correct |
24 ms |
47192 KB |
Output is correct |
6 |
Correct |
29 ms |
47288 KB |
Output is correct |
7 |
Correct |
28 ms |
47188 KB |
Output is correct |
8 |
Correct |
27 ms |
47180 KB |
Output is correct |
9 |
Correct |
23 ms |
47184 KB |
Output is correct |
10 |
Correct |
24 ms |
47272 KB |
Output is correct |
11 |
Correct |
24 ms |
47284 KB |
Output is correct |
12 |
Correct |
25 ms |
47172 KB |
Output is correct |
13 |
Correct |
25 ms |
47296 KB |
Output is correct |
14 |
Correct |
26 ms |
47272 KB |
Output is correct |
15 |
Correct |
24 ms |
47216 KB |
Output is correct |
16 |
Correct |
26 ms |
47276 KB |
Output is correct |
17 |
Correct |
29 ms |
47224 KB |
Output is correct |
18 |
Correct |
29 ms |
47184 KB |
Output is correct |
19 |
Correct |
23 ms |
47188 KB |
Output is correct |
20 |
Correct |
24 ms |
47188 KB |
Output is correct |
21 |
Correct |
25 ms |
47288 KB |
Output is correct |
22 |
Correct |
24 ms |
47220 KB |
Output is correct |
23 |
Correct |
25 ms |
47324 KB |
Output is correct |
24 |
Correct |
25 ms |
47260 KB |
Output is correct |
25 |
Correct |
24 ms |
47204 KB |
Output is correct |
26 |
Correct |
24 ms |
47188 KB |
Output is correct |
27 |
Correct |
23 ms |
47188 KB |
Output is correct |
28 |
Correct |
25 ms |
47224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47252 KB |
Output is correct |
3 |
Correct |
24 ms |
47296 KB |
Output is correct |
4 |
Correct |
26 ms |
47296 KB |
Output is correct |
5 |
Correct |
24 ms |
47192 KB |
Output is correct |
6 |
Correct |
29 ms |
47288 KB |
Output is correct |
7 |
Correct |
28 ms |
47188 KB |
Output is correct |
8 |
Correct |
27 ms |
47180 KB |
Output is correct |
9 |
Correct |
23 ms |
47184 KB |
Output is correct |
10 |
Correct |
24 ms |
47272 KB |
Output is correct |
11 |
Correct |
24 ms |
47284 KB |
Output is correct |
12 |
Correct |
25 ms |
47172 KB |
Output is correct |
13 |
Correct |
25 ms |
47296 KB |
Output is correct |
14 |
Correct |
26 ms |
47272 KB |
Output is correct |
15 |
Correct |
24 ms |
47216 KB |
Output is correct |
16 |
Correct |
26 ms |
47276 KB |
Output is correct |
17 |
Correct |
29 ms |
47224 KB |
Output is correct |
18 |
Correct |
29 ms |
47184 KB |
Output is correct |
19 |
Correct |
23 ms |
47188 KB |
Output is correct |
20 |
Correct |
24 ms |
47188 KB |
Output is correct |
21 |
Correct |
25 ms |
47288 KB |
Output is correct |
22 |
Correct |
24 ms |
47220 KB |
Output is correct |
23 |
Correct |
25 ms |
47324 KB |
Output is correct |
24 |
Correct |
25 ms |
47260 KB |
Output is correct |
25 |
Correct |
24 ms |
47204 KB |
Output is correct |
26 |
Correct |
24 ms |
47188 KB |
Output is correct |
27 |
Correct |
23 ms |
47188 KB |
Output is correct |
28 |
Correct |
25 ms |
47224 KB |
Output is correct |
29 |
Correct |
28 ms |
47200 KB |
Output is correct |
30 |
Correct |
24 ms |
47184 KB |
Output is correct |
31 |
Correct |
27 ms |
47188 KB |
Output is correct |
32 |
Correct |
24 ms |
47188 KB |
Output is correct |
33 |
Correct |
26 ms |
47284 KB |
Output is correct |
34 |
Correct |
24 ms |
47308 KB |
Output is correct |
35 |
Correct |
24 ms |
47188 KB |
Output is correct |
36 |
Correct |
28 ms |
47204 KB |
Output is correct |
37 |
Correct |
24 ms |
47300 KB |
Output is correct |
38 |
Correct |
24 ms |
47188 KB |
Output is correct |
39 |
Correct |
24 ms |
47192 KB |
Output is correct |
40 |
Correct |
25 ms |
47300 KB |
Output is correct |
41 |
Correct |
28 ms |
47188 KB |
Output is correct |
42 |
Correct |
30 ms |
47272 KB |
Output is correct |
43 |
Correct |
28 ms |
47196 KB |
Output is correct |
44 |
Correct |
28 ms |
47188 KB |
Output is correct |
45 |
Correct |
29 ms |
47208 KB |
Output is correct |
46 |
Correct |
24 ms |
47276 KB |
Output is correct |
47 |
Correct |
24 ms |
47188 KB |
Output is correct |
48 |
Correct |
26 ms |
47232 KB |
Output is correct |
49 |
Correct |
25 ms |
47280 KB |
Output is correct |
50 |
Correct |
25 ms |
47236 KB |
Output is correct |
51 |
Correct |
28 ms |
47276 KB |
Output is correct |
52 |
Correct |
25 ms |
47276 KB |
Output is correct |
53 |
Correct |
25 ms |
47188 KB |
Output is correct |
54 |
Correct |
23 ms |
47188 KB |
Output is correct |
55 |
Correct |
24 ms |
47252 KB |
Output is correct |
56 |
Correct |
23 ms |
47208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
129048 KB |
Output is correct |
2 |
Correct |
185 ms |
128972 KB |
Output is correct |
3 |
Correct |
195 ms |
128912 KB |
Output is correct |
4 |
Correct |
193 ms |
128932 KB |
Output is correct |
5 |
Correct |
162 ms |
128896 KB |
Output is correct |
6 |
Correct |
234 ms |
128952 KB |
Output is correct |
7 |
Correct |
176 ms |
128964 KB |
Output is correct |
8 |
Correct |
162 ms |
128916 KB |
Output is correct |
9 |
Correct |
157 ms |
128916 KB |
Output is correct |
10 |
Correct |
194 ms |
128892 KB |
Output is correct |
11 |
Correct |
162 ms |
128912 KB |
Output is correct |
12 |
Correct |
158 ms |
128868 KB |
Output is correct |
13 |
Correct |
23 ms |
47276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47252 KB |
Output is correct |
3 |
Correct |
24 ms |
47296 KB |
Output is correct |
4 |
Correct |
26 ms |
47296 KB |
Output is correct |
5 |
Correct |
24 ms |
47192 KB |
Output is correct |
6 |
Correct |
29 ms |
47288 KB |
Output is correct |
7 |
Correct |
28 ms |
47188 KB |
Output is correct |
8 |
Correct |
27 ms |
47180 KB |
Output is correct |
9 |
Correct |
23 ms |
47184 KB |
Output is correct |
10 |
Correct |
24 ms |
47272 KB |
Output is correct |
11 |
Correct |
24 ms |
47284 KB |
Output is correct |
12 |
Correct |
25 ms |
47172 KB |
Output is correct |
13 |
Correct |
25 ms |
47296 KB |
Output is correct |
14 |
Correct |
26 ms |
47272 KB |
Output is correct |
15 |
Correct |
24 ms |
47216 KB |
Output is correct |
16 |
Correct |
26 ms |
47276 KB |
Output is correct |
17 |
Correct |
29 ms |
47224 KB |
Output is correct |
18 |
Correct |
29 ms |
47184 KB |
Output is correct |
19 |
Correct |
23 ms |
47188 KB |
Output is correct |
20 |
Correct |
24 ms |
47188 KB |
Output is correct |
21 |
Correct |
25 ms |
47288 KB |
Output is correct |
22 |
Correct |
24 ms |
47220 KB |
Output is correct |
23 |
Correct |
25 ms |
47324 KB |
Output is correct |
24 |
Correct |
25 ms |
47260 KB |
Output is correct |
25 |
Correct |
24 ms |
47204 KB |
Output is correct |
26 |
Correct |
24 ms |
47188 KB |
Output is correct |
27 |
Correct |
23 ms |
47188 KB |
Output is correct |
28 |
Correct |
25 ms |
47224 KB |
Output is correct |
29 |
Correct |
28 ms |
47200 KB |
Output is correct |
30 |
Correct |
24 ms |
47184 KB |
Output is correct |
31 |
Correct |
27 ms |
47188 KB |
Output is correct |
32 |
Correct |
24 ms |
47188 KB |
Output is correct |
33 |
Correct |
26 ms |
47284 KB |
Output is correct |
34 |
Correct |
24 ms |
47308 KB |
Output is correct |
35 |
Correct |
24 ms |
47188 KB |
Output is correct |
36 |
Correct |
28 ms |
47204 KB |
Output is correct |
37 |
Correct |
24 ms |
47300 KB |
Output is correct |
38 |
Correct |
24 ms |
47188 KB |
Output is correct |
39 |
Correct |
24 ms |
47192 KB |
Output is correct |
40 |
Correct |
25 ms |
47300 KB |
Output is correct |
41 |
Correct |
28 ms |
47188 KB |
Output is correct |
42 |
Correct |
30 ms |
47272 KB |
Output is correct |
43 |
Correct |
28 ms |
47196 KB |
Output is correct |
44 |
Correct |
28 ms |
47188 KB |
Output is correct |
45 |
Correct |
29 ms |
47208 KB |
Output is correct |
46 |
Correct |
24 ms |
47276 KB |
Output is correct |
47 |
Correct |
24 ms |
47188 KB |
Output is correct |
48 |
Correct |
26 ms |
47232 KB |
Output is correct |
49 |
Correct |
25 ms |
47280 KB |
Output is correct |
50 |
Correct |
25 ms |
47236 KB |
Output is correct |
51 |
Correct |
28 ms |
47276 KB |
Output is correct |
52 |
Correct |
25 ms |
47276 KB |
Output is correct |
53 |
Correct |
25 ms |
47188 KB |
Output is correct |
54 |
Correct |
23 ms |
47188 KB |
Output is correct |
55 |
Correct |
24 ms |
47252 KB |
Output is correct |
56 |
Correct |
23 ms |
47208 KB |
Output is correct |
57 |
Correct |
23 ms |
47240 KB |
Output is correct |
58 |
Correct |
27 ms |
47260 KB |
Output is correct |
59 |
Correct |
27 ms |
47284 KB |
Output is correct |
60 |
Correct |
27 ms |
47340 KB |
Output is correct |
61 |
Correct |
27 ms |
47268 KB |
Output is correct |
62 |
Correct |
23 ms |
47300 KB |
Output is correct |
63 |
Correct |
23 ms |
47284 KB |
Output is correct |
64 |
Correct |
23 ms |
47316 KB |
Output is correct |
65 |
Correct |
24 ms |
47292 KB |
Output is correct |
66 |
Correct |
23 ms |
47284 KB |
Output is correct |
67 |
Correct |
24 ms |
47256 KB |
Output is correct |
68 |
Correct |
30 ms |
47252 KB |
Output is correct |
69 |
Correct |
24 ms |
47288 KB |
Output is correct |
70 |
Correct |
26 ms |
47268 KB |
Output is correct |
71 |
Correct |
26 ms |
47252 KB |
Output is correct |
72 |
Correct |
26 ms |
47372 KB |
Output is correct |
73 |
Correct |
23 ms |
47292 KB |
Output is correct |
74 |
Correct |
24 ms |
47232 KB |
Output is correct |
75 |
Correct |
25 ms |
47376 KB |
Output is correct |
76 |
Correct |
27 ms |
47360 KB |
Output is correct |
77 |
Correct |
27 ms |
47324 KB |
Output is correct |
78 |
Correct |
23 ms |
47396 KB |
Output is correct |
79 |
Correct |
26 ms |
47436 KB |
Output is correct |
80 |
Correct |
24 ms |
47296 KB |
Output is correct |
81 |
Correct |
24 ms |
47316 KB |
Output is correct |
82 |
Correct |
23 ms |
47316 KB |
Output is correct |
83 |
Correct |
23 ms |
47288 KB |
Output is correct |
84 |
Correct |
24 ms |
47244 KB |
Output is correct |
85 |
Correct |
23 ms |
47316 KB |
Output is correct |
86 |
Correct |
24 ms |
47252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47252 KB |
Output is correct |
3 |
Correct |
24 ms |
47296 KB |
Output is correct |
4 |
Correct |
26 ms |
47296 KB |
Output is correct |
5 |
Correct |
24 ms |
47192 KB |
Output is correct |
6 |
Correct |
29 ms |
47288 KB |
Output is correct |
7 |
Correct |
28 ms |
47188 KB |
Output is correct |
8 |
Correct |
27 ms |
47180 KB |
Output is correct |
9 |
Correct |
23 ms |
47184 KB |
Output is correct |
10 |
Correct |
24 ms |
47272 KB |
Output is correct |
11 |
Correct |
24 ms |
47284 KB |
Output is correct |
12 |
Correct |
25 ms |
47172 KB |
Output is correct |
13 |
Correct |
25 ms |
47296 KB |
Output is correct |
14 |
Correct |
26 ms |
47272 KB |
Output is correct |
15 |
Correct |
24 ms |
47216 KB |
Output is correct |
16 |
Correct |
26 ms |
47276 KB |
Output is correct |
17 |
Correct |
29 ms |
47224 KB |
Output is correct |
18 |
Correct |
29 ms |
47184 KB |
Output is correct |
19 |
Correct |
23 ms |
47188 KB |
Output is correct |
20 |
Correct |
24 ms |
47188 KB |
Output is correct |
21 |
Correct |
25 ms |
47288 KB |
Output is correct |
22 |
Correct |
24 ms |
47220 KB |
Output is correct |
23 |
Correct |
25 ms |
47324 KB |
Output is correct |
24 |
Correct |
25 ms |
47260 KB |
Output is correct |
25 |
Correct |
24 ms |
47204 KB |
Output is correct |
26 |
Correct |
24 ms |
47188 KB |
Output is correct |
27 |
Correct |
23 ms |
47188 KB |
Output is correct |
28 |
Correct |
25 ms |
47224 KB |
Output is correct |
29 |
Correct |
28 ms |
47200 KB |
Output is correct |
30 |
Correct |
24 ms |
47184 KB |
Output is correct |
31 |
Correct |
27 ms |
47188 KB |
Output is correct |
32 |
Correct |
24 ms |
47188 KB |
Output is correct |
33 |
Correct |
26 ms |
47284 KB |
Output is correct |
34 |
Correct |
24 ms |
47308 KB |
Output is correct |
35 |
Correct |
24 ms |
47188 KB |
Output is correct |
36 |
Correct |
28 ms |
47204 KB |
Output is correct |
37 |
Correct |
24 ms |
47300 KB |
Output is correct |
38 |
Correct |
24 ms |
47188 KB |
Output is correct |
39 |
Correct |
24 ms |
47192 KB |
Output is correct |
40 |
Correct |
25 ms |
47300 KB |
Output is correct |
41 |
Correct |
28 ms |
47188 KB |
Output is correct |
42 |
Correct |
30 ms |
47272 KB |
Output is correct |
43 |
Correct |
28 ms |
47196 KB |
Output is correct |
44 |
Correct |
28 ms |
47188 KB |
Output is correct |
45 |
Correct |
29 ms |
47208 KB |
Output is correct |
46 |
Correct |
24 ms |
47276 KB |
Output is correct |
47 |
Correct |
24 ms |
47188 KB |
Output is correct |
48 |
Correct |
26 ms |
47232 KB |
Output is correct |
49 |
Correct |
25 ms |
47280 KB |
Output is correct |
50 |
Correct |
25 ms |
47236 KB |
Output is correct |
51 |
Correct |
28 ms |
47276 KB |
Output is correct |
52 |
Correct |
25 ms |
47276 KB |
Output is correct |
53 |
Correct |
25 ms |
47188 KB |
Output is correct |
54 |
Correct |
23 ms |
47188 KB |
Output is correct |
55 |
Correct |
24 ms |
47252 KB |
Output is correct |
56 |
Correct |
23 ms |
47208 KB |
Output is correct |
57 |
Correct |
165 ms |
129048 KB |
Output is correct |
58 |
Correct |
185 ms |
128972 KB |
Output is correct |
59 |
Correct |
195 ms |
128912 KB |
Output is correct |
60 |
Correct |
193 ms |
128932 KB |
Output is correct |
61 |
Correct |
162 ms |
128896 KB |
Output is correct |
62 |
Correct |
234 ms |
128952 KB |
Output is correct |
63 |
Correct |
176 ms |
128964 KB |
Output is correct |
64 |
Correct |
162 ms |
128916 KB |
Output is correct |
65 |
Correct |
157 ms |
128916 KB |
Output is correct |
66 |
Correct |
194 ms |
128892 KB |
Output is correct |
67 |
Correct |
162 ms |
128912 KB |
Output is correct |
68 |
Correct |
158 ms |
128868 KB |
Output is correct |
69 |
Correct |
23 ms |
47276 KB |
Output is correct |
70 |
Correct |
23 ms |
47240 KB |
Output is correct |
71 |
Correct |
27 ms |
47260 KB |
Output is correct |
72 |
Correct |
27 ms |
47284 KB |
Output is correct |
73 |
Correct |
27 ms |
47340 KB |
Output is correct |
74 |
Correct |
27 ms |
47268 KB |
Output is correct |
75 |
Correct |
23 ms |
47300 KB |
Output is correct |
76 |
Correct |
23 ms |
47284 KB |
Output is correct |
77 |
Correct |
23 ms |
47316 KB |
Output is correct |
78 |
Correct |
24 ms |
47292 KB |
Output is correct |
79 |
Correct |
23 ms |
47284 KB |
Output is correct |
80 |
Correct |
24 ms |
47256 KB |
Output is correct |
81 |
Correct |
30 ms |
47252 KB |
Output is correct |
82 |
Correct |
24 ms |
47288 KB |
Output is correct |
83 |
Correct |
26 ms |
47268 KB |
Output is correct |
84 |
Correct |
26 ms |
47252 KB |
Output is correct |
85 |
Correct |
26 ms |
47372 KB |
Output is correct |
86 |
Correct |
23 ms |
47292 KB |
Output is correct |
87 |
Correct |
24 ms |
47232 KB |
Output is correct |
88 |
Correct |
25 ms |
47376 KB |
Output is correct |
89 |
Correct |
27 ms |
47360 KB |
Output is correct |
90 |
Correct |
27 ms |
47324 KB |
Output is correct |
91 |
Correct |
23 ms |
47396 KB |
Output is correct |
92 |
Correct |
26 ms |
47436 KB |
Output is correct |
93 |
Correct |
24 ms |
47296 KB |
Output is correct |
94 |
Correct |
24 ms |
47316 KB |
Output is correct |
95 |
Correct |
23 ms |
47316 KB |
Output is correct |
96 |
Correct |
23 ms |
47288 KB |
Output is correct |
97 |
Correct |
24 ms |
47244 KB |
Output is correct |
98 |
Correct |
23 ms |
47316 KB |
Output is correct |
99 |
Correct |
24 ms |
47252 KB |
Output is correct |
100 |
Correct |
154 ms |
94212 KB |
Output is correct |
101 |
Correct |
144 ms |
94224 KB |
Output is correct |
102 |
Correct |
137 ms |
94172 KB |
Output is correct |
103 |
Correct |
164 ms |
94208 KB |
Output is correct |
104 |
Correct |
141 ms |
94220 KB |
Output is correct |
105 |
Correct |
168 ms |
105944 KB |
Output is correct |
106 |
Correct |
160 ms |
105948 KB |
Output is correct |
107 |
Correct |
156 ms |
105856 KB |
Output is correct |
108 |
Correct |
157 ms |
105968 KB |
Output is correct |
109 |
Correct |
180 ms |
105928 KB |
Output is correct |
110 |
Correct |
170 ms |
94224 KB |
Output is correct |
111 |
Correct |
153 ms |
94228 KB |
Output is correct |
112 |
Correct |
149 ms |
94236 KB |
Output is correct |
113 |
Correct |
146 ms |
94116 KB |
Output is correct |
114 |
Correct |
156 ms |
94208 KB |
Output is correct |
115 |
Correct |
174 ms |
128940 KB |
Output is correct |
116 |
Correct |
174 ms |
128936 KB |
Output is correct |
117 |
Correct |
168 ms |
128964 KB |
Output is correct |
118 |
Correct |
170 ms |
128924 KB |
Output is correct |
119 |
Correct |
159 ms |
128904 KB |
Output is correct |
120 |
Correct |
138 ms |
94212 KB |
Output is correct |
121 |
Correct |
148 ms |
94228 KB |
Output is correct |
122 |
Correct |
148 ms |
94200 KB |
Output is correct |
123 |
Correct |
145 ms |
94212 KB |
Output is correct |
124 |
Correct |
149 ms |
111888 KB |
Output is correct |
125 |
Correct |
138 ms |
111832 KB |
Output is correct |
126 |
Correct |
166 ms |
111784 KB |
Output is correct |
127 |
Correct |
142 ms |
111816 KB |
Output is correct |
128 |
Correct |
81 ms |
71872 KB |
Output is correct |
129 |
Correct |
76 ms |
71848 KB |
Output is correct |
130 |
Correct |
90 ms |
71892 KB |
Output is correct |
131 |
Correct |
28 ms |
47188 KB |
Output is correct |