#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <stack>
typedef long long llong;
const int MAXLEN = 10000000;
const int MAXN = 1000000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
char s[MAXN];
int type[MAXN];
int dpMIN[MAXN][3];
int dpMAX[MAXN][3];
bool blMIN[MAXN][3];
bool blMAX[MAXN][3];
std::vector <int> g[MAXN];
std::stack <int> st;
int next[MAXN];
int end[MAXN];
int cnt;
int n;
int buildTree(int l, int r)
{
if (l == r)
{
return ++cnt;
}
int node = ++cnt;
if (s[l + 1] == 'i') type[node] = 1;
else if (s[l + 1] == 'a') type[node] = 2;
else assert(false);
g[node].push_back(buildTree(l + 4, next[l + 3] - 1));
g[node].push_back(buildTree(next[l + 3] + 1, end[l + 3] - 1));
return node;
}
int fMIN(int node, int asked)
{
if (g[node].empty())
{
if (asked == 0) return 1;
return 0;
}
if (blMIN[node][asked])
{
return dpMIN[node][asked];
}
blMIN[node][asked] = true;
dpMIN[node][asked] = INTINF;
if (asked == 0)
{
if (type[node] == 1)
{
dpMIN[node][asked] = std::min(fMIN(g[node][0], 0), fMIN(g[node][1], 0));
} else
{
dpMIN[node][asked] = fMIN(g[node][0], 0) + fMIN(g[node][1], 0);
}
} else if (asked == 1)
{
if (type[node] == 1)
{
dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 1) + fMIN(g[node][1], 2));
dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 2) + fMIN(g[node][1], 1));
} else
{
dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 1) + fMIN(g[node][1], 0));
dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 0) + fMIN(g[node][1], 1));
}
} else
{
if (type[node] == 2)
{
dpMIN[node][asked] = std::min(fMIN(g[node][0], 2), fMIN(g[node][1], 2));
} else
{
dpMIN[node][asked] = fMIN(g[node][0], 2) + fMIN(g[node][1], 2);
}
}
return dpMIN[node][asked];
}
int fMAX(int node, int asked)
{
if (g[node].empty())
{
if (asked == 0) return 1;
return 0;
}
if (blMAX[node][asked])
{
return dpMAX[node][asked];
}
blMAX[node][asked] = true;
dpMAX[node][asked] = -INTINF;
if (asked == 0)
{
if (type[node] == 1)
{
dpMAX[node][asked] = std::max(fMAX(g[node][0], 0) + std::max(fMAX(g[node][1], 0), fMAX(g[node][1], 2)), std::max(fMAX(g[node][0], 0), fMAX(g[node][0], 2)) + fMAX(g[node][1], 0));
} else
{
dpMAX[node][asked] = fMAX(g[node][0], 0) + fMAX(g[node][1], 0);
}
} else if (asked == 1)
{
if (type[node] == 1)
{
dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 1) + fMAX(g[node][1], 2));
dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 2) + fMAX(g[node][1], 1));
} else
{
dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 1) + fMAX(g[node][1], 0));
dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 0) + fMAX(g[node][1], 1));
}
} else
{
if (type[node] == 2)
{
dpMAX[node][asked] = std::max(fMAX(g[node][0], 2) + std::max(fMAX(g[node][1], 0), fMAX(g[node][1], 2)), std::max(fMAX(g[node][0], 0), fMAX(g[node][0], 2)) + fMAX(g[node][1], 2));
} else
{
dpMAX[node][asked] = fMAX(g[node][0], 2) + fMAX(g[node][1], 2);
}
}
return dpMAX[node][asked];
}
void solve()
{
int len = strlen(s + 1);
for (int i = 1 ; i <= len ; ++i)
{
if (s[i] == '(') st.push(i);
if (s[i] == ',')
{
next[st.top()] = i;
st.pop();
}
}
while (!st.empty())
{
st.pop();
}
for (int i = 1 ; i <= len ; ++i)
{
if (s[i] == '(') st.push(i);
if (s[i] == ')')
{
end[st.top()] = i;
st.pop();
}
}
buildTree(1, len);
n = cnt;
// std::cout << "type\n";
// for (int i = 1 ; i <= n ; ++i)
// {
// std::cout << type[i] << '\n';
// }
// std::cout << "tree\n";
// for (int i = 1 ; i <= n ; ++i)
// {
// for (const int &u : g[i])
// {
// std::cout << i << ' ' << u << '\n';
// }
// }
std::cout << fMAX(1, 1) - fMIN(1, 1) + 1 << '\n';
}
void input()
{
std::cin >> s + 1;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void input()':
Main.cpp:194:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
194 | std::cin >> s + 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23820 KB |
Output is correct |
6 |
Correct |
12 ms |
23744 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23852 KB |
Output is correct |
18 |
Correct |
12 ms |
23824 KB |
Output is correct |
19 |
Correct |
13 ms |
23852 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23772 KB |
Output is correct |
22 |
Correct |
12 ms |
23816 KB |
Output is correct |
23 |
Correct |
13 ms |
23860 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23836 KB |
Output is correct |
26 |
Correct |
12 ms |
23764 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23820 KB |
Output is correct |
6 |
Correct |
12 ms |
23744 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23852 KB |
Output is correct |
18 |
Correct |
12 ms |
23824 KB |
Output is correct |
19 |
Correct |
13 ms |
23852 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23772 KB |
Output is correct |
22 |
Correct |
12 ms |
23816 KB |
Output is correct |
23 |
Correct |
13 ms |
23860 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23836 KB |
Output is correct |
26 |
Correct |
12 ms |
23764 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
14 ms |
23944 KB |
Output is correct |
30 |
Correct |
12 ms |
23820 KB |
Output is correct |
31 |
Correct |
12 ms |
23816 KB |
Output is correct |
32 |
Correct |
13 ms |
23764 KB |
Output is correct |
33 |
Correct |
15 ms |
23860 KB |
Output is correct |
34 |
Correct |
13 ms |
23764 KB |
Output is correct |
35 |
Correct |
12 ms |
23864 KB |
Output is correct |
36 |
Correct |
13 ms |
23828 KB |
Output is correct |
37 |
Correct |
12 ms |
23808 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
13 ms |
23828 KB |
Output is correct |
40 |
Correct |
12 ms |
23824 KB |
Output is correct |
41 |
Correct |
16 ms |
23764 KB |
Output is correct |
42 |
Correct |
12 ms |
23848 KB |
Output is correct |
43 |
Correct |
14 ms |
23812 KB |
Output is correct |
44 |
Correct |
12 ms |
23820 KB |
Output is correct |
45 |
Correct |
12 ms |
23820 KB |
Output is correct |
46 |
Correct |
14 ms |
23764 KB |
Output is correct |
47 |
Correct |
13 ms |
23764 KB |
Output is correct |
48 |
Correct |
12 ms |
23820 KB |
Output is correct |
49 |
Correct |
14 ms |
23820 KB |
Output is correct |
50 |
Correct |
13 ms |
23768 KB |
Output is correct |
51 |
Correct |
12 ms |
23820 KB |
Output is correct |
52 |
Correct |
12 ms |
23848 KB |
Output is correct |
53 |
Correct |
12 ms |
23764 KB |
Output is correct |
54 |
Correct |
12 ms |
23808 KB |
Output is correct |
55 |
Correct |
13 ms |
23772 KB |
Output is correct |
56 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
35 ms |
51132 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23820 KB |
Output is correct |
6 |
Correct |
12 ms |
23744 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23852 KB |
Output is correct |
18 |
Correct |
12 ms |
23824 KB |
Output is correct |
19 |
Correct |
13 ms |
23852 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23772 KB |
Output is correct |
22 |
Correct |
12 ms |
23816 KB |
Output is correct |
23 |
Correct |
13 ms |
23860 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23836 KB |
Output is correct |
26 |
Correct |
12 ms |
23764 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
14 ms |
23944 KB |
Output is correct |
30 |
Correct |
12 ms |
23820 KB |
Output is correct |
31 |
Correct |
12 ms |
23816 KB |
Output is correct |
32 |
Correct |
13 ms |
23764 KB |
Output is correct |
33 |
Correct |
15 ms |
23860 KB |
Output is correct |
34 |
Correct |
13 ms |
23764 KB |
Output is correct |
35 |
Correct |
12 ms |
23864 KB |
Output is correct |
36 |
Correct |
13 ms |
23828 KB |
Output is correct |
37 |
Correct |
12 ms |
23808 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
13 ms |
23828 KB |
Output is correct |
40 |
Correct |
12 ms |
23824 KB |
Output is correct |
41 |
Correct |
16 ms |
23764 KB |
Output is correct |
42 |
Correct |
12 ms |
23848 KB |
Output is correct |
43 |
Correct |
14 ms |
23812 KB |
Output is correct |
44 |
Correct |
12 ms |
23820 KB |
Output is correct |
45 |
Correct |
12 ms |
23820 KB |
Output is correct |
46 |
Correct |
14 ms |
23764 KB |
Output is correct |
47 |
Correct |
13 ms |
23764 KB |
Output is correct |
48 |
Correct |
12 ms |
23820 KB |
Output is correct |
49 |
Correct |
14 ms |
23820 KB |
Output is correct |
50 |
Correct |
13 ms |
23768 KB |
Output is correct |
51 |
Correct |
12 ms |
23820 KB |
Output is correct |
52 |
Correct |
12 ms |
23848 KB |
Output is correct |
53 |
Correct |
12 ms |
23764 KB |
Output is correct |
54 |
Correct |
12 ms |
23808 KB |
Output is correct |
55 |
Correct |
13 ms |
23772 KB |
Output is correct |
56 |
Correct |
12 ms |
23764 KB |
Output is correct |
57 |
Correct |
12 ms |
24008 KB |
Output is correct |
58 |
Correct |
12 ms |
23976 KB |
Output is correct |
59 |
Correct |
12 ms |
23952 KB |
Output is correct |
60 |
Correct |
13 ms |
24012 KB |
Output is correct |
61 |
Correct |
13 ms |
24020 KB |
Output is correct |
62 |
Correct |
14 ms |
24040 KB |
Output is correct |
63 |
Correct |
15 ms |
24020 KB |
Output is correct |
64 |
Correct |
15 ms |
24056 KB |
Output is correct |
65 |
Correct |
14 ms |
24056 KB |
Output is correct |
66 |
Correct |
13 ms |
24028 KB |
Output is correct |
67 |
Correct |
12 ms |
23952 KB |
Output is correct |
68 |
Correct |
14 ms |
24076 KB |
Output is correct |
69 |
Correct |
15 ms |
23928 KB |
Output is correct |
70 |
Correct |
12 ms |
24020 KB |
Output is correct |
71 |
Correct |
12 ms |
24032 KB |
Output is correct |
72 |
Correct |
13 ms |
24084 KB |
Output is correct |
73 |
Correct |
12 ms |
24020 KB |
Output is correct |
74 |
Correct |
13 ms |
24076 KB |
Output is correct |
75 |
Correct |
12 ms |
24064 KB |
Output is correct |
76 |
Correct |
14 ms |
24020 KB |
Output is correct |
77 |
Correct |
12 ms |
24000 KB |
Output is correct |
78 |
Correct |
12 ms |
24020 KB |
Output is correct |
79 |
Correct |
11 ms |
23952 KB |
Output is correct |
80 |
Correct |
12 ms |
23972 KB |
Output is correct |
81 |
Correct |
13 ms |
23948 KB |
Output is correct |
82 |
Correct |
15 ms |
24020 KB |
Output is correct |
83 |
Correct |
12 ms |
23892 KB |
Output is correct |
84 |
Correct |
11 ms |
23912 KB |
Output is correct |
85 |
Correct |
11 ms |
23920 KB |
Output is correct |
86 |
Correct |
14 ms |
23868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23820 KB |
Output is correct |
6 |
Correct |
12 ms |
23744 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23820 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23852 KB |
Output is correct |
18 |
Correct |
12 ms |
23824 KB |
Output is correct |
19 |
Correct |
13 ms |
23852 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23772 KB |
Output is correct |
22 |
Correct |
12 ms |
23816 KB |
Output is correct |
23 |
Correct |
13 ms |
23860 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23836 KB |
Output is correct |
26 |
Correct |
12 ms |
23764 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
14 ms |
23944 KB |
Output is correct |
30 |
Correct |
12 ms |
23820 KB |
Output is correct |
31 |
Correct |
12 ms |
23816 KB |
Output is correct |
32 |
Correct |
13 ms |
23764 KB |
Output is correct |
33 |
Correct |
15 ms |
23860 KB |
Output is correct |
34 |
Correct |
13 ms |
23764 KB |
Output is correct |
35 |
Correct |
12 ms |
23864 KB |
Output is correct |
36 |
Correct |
13 ms |
23828 KB |
Output is correct |
37 |
Correct |
12 ms |
23808 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
13 ms |
23828 KB |
Output is correct |
40 |
Correct |
12 ms |
23824 KB |
Output is correct |
41 |
Correct |
16 ms |
23764 KB |
Output is correct |
42 |
Correct |
12 ms |
23848 KB |
Output is correct |
43 |
Correct |
14 ms |
23812 KB |
Output is correct |
44 |
Correct |
12 ms |
23820 KB |
Output is correct |
45 |
Correct |
12 ms |
23820 KB |
Output is correct |
46 |
Correct |
14 ms |
23764 KB |
Output is correct |
47 |
Correct |
13 ms |
23764 KB |
Output is correct |
48 |
Correct |
12 ms |
23820 KB |
Output is correct |
49 |
Correct |
14 ms |
23820 KB |
Output is correct |
50 |
Correct |
13 ms |
23768 KB |
Output is correct |
51 |
Correct |
12 ms |
23820 KB |
Output is correct |
52 |
Correct |
12 ms |
23848 KB |
Output is correct |
53 |
Correct |
12 ms |
23764 KB |
Output is correct |
54 |
Correct |
12 ms |
23808 KB |
Output is correct |
55 |
Correct |
13 ms |
23772 KB |
Output is correct |
56 |
Correct |
12 ms |
23764 KB |
Output is correct |
57 |
Runtime error |
35 ms |
51132 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |