#include <bits/stdc++.h>
using namespace std;
#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
void solve() {
string s;
cin >> s;
s = s + ' ';
vi par(1), type(1);
// ? 0
// max +1
// min -1
int idx = 0, cnt = 0;
stack<int> st;
st.push(0);
while(s[idx] != ' ') {
if(s[idx] == 'm') {
cnt++;
type.pb(s[idx+1] == 'a' ? 1 : -1);
par.pb(st.top());
st.push(cnt);
idx += 4;
} else if(s[idx] == ')') {
st.pop();
idx++;
} else if(s[idx] == '?') {
cnt++;
type.pb(0);
par.pb(st.top());
idx++;
} else if(s[idx] == ',') {
// do nothing
idx++;
}
}
vvi adj(cnt+1);
loop1(i, cnt) {
adj[par[i]].pb(i);
}
vi sub(cnt+1);
vii intv(cnt+1);
auto dfs = [&] (int u, auto dfs) -> void {
if(type[u] == 0) {
sub[u] = 1;
intv[u] = {1, 1};
} else {
auto v1 = adj[u][0], v2 = adj[u][1];
dfs(v1, dfs);
dfs(v2, dfs);
sub[u] = sub[v1] + sub[v2];
auto [a, b] = intv[v1];
auto [c, d] = intv[v2];
int x1 = sub[v1], x2 = sub[v2];
if(type[u] == -1) {
intv[u] = {min(a, c), b + d - 1};
} else {
intv[u] = {a + c, x1 + x2 - min(x1 - b, x2 - d)};
}
}
};
dfs(1, dfs);
cout << intv[1][1] - intv[1][0] + 1 << endl;
}
signed main() {
fast_io;
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
316 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
252 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
324 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
320 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
249072 KB |
Output is correct |
2 |
Correct |
299 ms |
249064 KB |
Output is correct |
3 |
Correct |
315 ms |
249056 KB |
Output is correct |
4 |
Correct |
298 ms |
249156 KB |
Output is correct |
5 |
Correct |
293 ms |
249184 KB |
Output is correct |
6 |
Correct |
289 ms |
249180 KB |
Output is correct |
7 |
Correct |
314 ms |
249188 KB |
Output is correct |
8 |
Correct |
286 ms |
249188 KB |
Output is correct |
9 |
Correct |
291 ms |
249124 KB |
Output is correct |
10 |
Correct |
278 ms |
249172 KB |
Output is correct |
11 |
Correct |
357 ms |
249072 KB |
Output is correct |
12 |
Correct |
302 ms |
249088 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
316 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
252 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
324 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
320 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
456 KB |
Output is correct |
59 |
Correct |
1 ms |
452 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
456 KB |
Output is correct |
63 |
Correct |
1 ms |
456 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
1 ms |
468 KB |
Output is correct |
66 |
Correct |
1 ms |
448 KB |
Output is correct |
67 |
Correct |
1 ms |
456 KB |
Output is correct |
68 |
Correct |
1 ms |
468 KB |
Output is correct |
69 |
Correct |
1 ms |
468 KB |
Output is correct |
70 |
Correct |
1 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
468 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
1 ms |
456 KB |
Output is correct |
75 |
Correct |
1 ms |
468 KB |
Output is correct |
76 |
Correct |
1 ms |
456 KB |
Output is correct |
77 |
Correct |
1 ms |
464 KB |
Output is correct |
78 |
Correct |
1 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
460 KB |
Output is correct |
81 |
Correct |
1 ms |
456 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
320 KB |
Output is correct |
86 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
316 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
252 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
324 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
320 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
340 ms |
249072 KB |
Output is correct |
58 |
Correct |
299 ms |
249064 KB |
Output is correct |
59 |
Correct |
315 ms |
249056 KB |
Output is correct |
60 |
Correct |
298 ms |
249156 KB |
Output is correct |
61 |
Correct |
293 ms |
249184 KB |
Output is correct |
62 |
Correct |
289 ms |
249180 KB |
Output is correct |
63 |
Correct |
314 ms |
249188 KB |
Output is correct |
64 |
Correct |
286 ms |
249188 KB |
Output is correct |
65 |
Correct |
291 ms |
249124 KB |
Output is correct |
66 |
Correct |
278 ms |
249172 KB |
Output is correct |
67 |
Correct |
357 ms |
249072 KB |
Output is correct |
68 |
Correct |
302 ms |
249088 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
456 KB |
Output is correct |
72 |
Correct |
1 ms |
452 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
1 ms |
468 KB |
Output is correct |
75 |
Correct |
1 ms |
456 KB |
Output is correct |
76 |
Correct |
1 ms |
456 KB |
Output is correct |
77 |
Correct |
1 ms |
468 KB |
Output is correct |
78 |
Correct |
1 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
448 KB |
Output is correct |
80 |
Correct |
1 ms |
456 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
1 ms |
468 KB |
Output is correct |
87 |
Correct |
1 ms |
456 KB |
Output is correct |
88 |
Correct |
1 ms |
468 KB |
Output is correct |
89 |
Correct |
1 ms |
456 KB |
Output is correct |
90 |
Correct |
1 ms |
464 KB |
Output is correct |
91 |
Correct |
1 ms |
468 KB |
Output is correct |
92 |
Correct |
1 ms |
468 KB |
Output is correct |
93 |
Correct |
1 ms |
460 KB |
Output is correct |
94 |
Correct |
1 ms |
456 KB |
Output is correct |
95 |
Correct |
1 ms |
468 KB |
Output is correct |
96 |
Correct |
1 ms |
340 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
1 ms |
320 KB |
Output is correct |
99 |
Correct |
1 ms |
212 KB |
Output is correct |
100 |
Correct |
281 ms |
170464 KB |
Output is correct |
101 |
Correct |
314 ms |
170580 KB |
Output is correct |
102 |
Correct |
255 ms |
170480 KB |
Output is correct |
103 |
Correct |
271 ms |
170584 KB |
Output is correct |
104 |
Correct |
247 ms |
170580 KB |
Output is correct |
105 |
Correct |
328 ms |
196728 KB |
Output is correct |
106 |
Correct |
291 ms |
196776 KB |
Output is correct |
107 |
Correct |
261 ms |
196748 KB |
Output is correct |
108 |
Correct |
287 ms |
196716 KB |
Output is correct |
109 |
Correct |
252 ms |
196708 KB |
Output is correct |
110 |
Correct |
247 ms |
170588 KB |
Output is correct |
111 |
Correct |
235 ms |
170584 KB |
Output is correct |
112 |
Correct |
240 ms |
170596 KB |
Output is correct |
113 |
Correct |
226 ms |
170580 KB |
Output is correct |
114 |
Correct |
229 ms |
170580 KB |
Output is correct |
115 |
Correct |
274 ms |
249172 KB |
Output is correct |
116 |
Correct |
293 ms |
249232 KB |
Output is correct |
117 |
Correct |
276 ms |
249180 KB |
Output is correct |
118 |
Correct |
275 ms |
249056 KB |
Output is correct |
119 |
Correct |
271 ms |
249184 KB |
Output is correct |
120 |
Correct |
234 ms |
170464 KB |
Output is correct |
121 |
Correct |
226 ms |
170704 KB |
Output is correct |
122 |
Correct |
233 ms |
170480 KB |
Output is correct |
123 |
Correct |
237 ms |
170532 KB |
Output is correct |
124 |
Correct |
228 ms |
209880 KB |
Output is correct |
125 |
Correct |
225 ms |
209768 KB |
Output is correct |
126 |
Correct |
340 ms |
210012 KB |
Output is correct |
127 |
Correct |
231 ms |
209980 KB |
Output is correct |
128 |
Correct |
121 ms |
89580 KB |
Output is correct |
129 |
Correct |
113 ms |
89584 KB |
Output is correct |
130 |
Correct |
111 ms |
89620 KB |
Output is correct |
131 |
Correct |
1 ms |
212 KB |
Output is correct |