#include <bits/stdc++.h>
using namespace std;
string a;
int poz;
int curnod = 1;
int l[4000005],r[4000005],s[4000005];
int fs[4000005],fd[4000005],tip[4000005];
void build_tree(int nod)
{
if (a[poz] != '?')
{
if (a[poz + 1] == 'i')
tip[nod] = 0;
else
tip[nod] = 1;
poz += 4;
curnod++;
fs[nod] = curnod;
build_tree(curnod);
poz++;
curnod++;
fd[nod] = curnod;
build_tree(curnod);
poz++;
}
else
{
tip[nod] = -1;
fs[nod] = fd[nod] = -1;
poz++;
}
}
void dfs(int nod)
{
if (fs[nod] == -1)
l[nod] = r[nod] = s[nod] = 1;
else
{
dfs(fs[nod]);
dfs(fd[nod]);
s[nod] = s[fs[nod]] + s[fd[nod]];
if (tip[nod] == 0)
{
l[nod] = min(l[fs[nod]],l[fd[nod]]);
r[nod] = r[fs[nod]] + r[fd[nod]] - 1;
}
else
{
l[nod] = l[fs[nod]] + l[fd[nod]];
r[nod] = max(r[fs[nod]] + s[fd[nod]],r[fd[nod]] + s[fs[nod]]);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> a;
build_tree(1);
dfs(1);
cout << r[1] - l[1] + 1;
return 0;
}
/**
Construiesc arborele ala
Fiecare nod poate avea un interval
Daca e frunza, e L = R = S = 1
Fie L1 R1 S1 si L2 R2 S2 L,R si size-ul pentru fii
S = S1 + S2 oricum
Daca e nod de min, L = min(L1,L2), R = R1 + R2 - 1
Daca e nod de max, L = L1 + L2 + 1, R = max(L1 + S2,L2 + S1)
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10708 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10704 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10712 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10708 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10704 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10712 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
1 ms |
10588 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
1 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
1 ms |
10588 KB |
Output is correct |
39 |
Correct |
1 ms |
10588 KB |
Output is correct |
40 |
Correct |
1 ms |
10588 KB |
Output is correct |
41 |
Correct |
1 ms |
10588 KB |
Output is correct |
42 |
Correct |
1 ms |
10588 KB |
Output is correct |
43 |
Correct |
1 ms |
10588 KB |
Output is correct |
44 |
Correct |
1 ms |
10588 KB |
Output is correct |
45 |
Correct |
1 ms |
10720 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10716 KB |
Output is correct |
48 |
Correct |
1 ms |
10588 KB |
Output is correct |
49 |
Correct |
1 ms |
10588 KB |
Output is correct |
50 |
Correct |
1 ms |
10588 KB |
Output is correct |
51 |
Correct |
1 ms |
10588 KB |
Output is correct |
52 |
Correct |
1 ms |
10588 KB |
Output is correct |
53 |
Correct |
1 ms |
10584 KB |
Output is correct |
54 |
Correct |
1 ms |
10588 KB |
Output is correct |
55 |
Correct |
1 ms |
10588 KB |
Output is correct |
56 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
94932 KB |
Output is correct |
2 |
Correct |
68 ms |
94156 KB |
Output is correct |
3 |
Correct |
68 ms |
94160 KB |
Output is correct |
4 |
Correct |
68 ms |
95144 KB |
Output is correct |
5 |
Correct |
67 ms |
94260 KB |
Output is correct |
6 |
Correct |
68 ms |
94160 KB |
Output is correct |
7 |
Correct |
67 ms |
94416 KB |
Output is correct |
8 |
Correct |
67 ms |
94156 KB |
Output is correct |
9 |
Correct |
69 ms |
94156 KB |
Output is correct |
10 |
Correct |
67 ms |
94924 KB |
Output is correct |
11 |
Correct |
68 ms |
94812 KB |
Output is correct |
12 |
Correct |
68 ms |
95444 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10708 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10704 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10712 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
1 ms |
10588 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
1 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
1 ms |
10588 KB |
Output is correct |
39 |
Correct |
1 ms |
10588 KB |
Output is correct |
40 |
Correct |
1 ms |
10588 KB |
Output is correct |
41 |
Correct |
1 ms |
10588 KB |
Output is correct |
42 |
Correct |
1 ms |
10588 KB |
Output is correct |
43 |
Correct |
1 ms |
10588 KB |
Output is correct |
44 |
Correct |
1 ms |
10588 KB |
Output is correct |
45 |
Correct |
1 ms |
10720 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10716 KB |
Output is correct |
48 |
Correct |
1 ms |
10588 KB |
Output is correct |
49 |
Correct |
1 ms |
10588 KB |
Output is correct |
50 |
Correct |
1 ms |
10588 KB |
Output is correct |
51 |
Correct |
1 ms |
10588 KB |
Output is correct |
52 |
Correct |
1 ms |
10588 KB |
Output is correct |
53 |
Correct |
1 ms |
10584 KB |
Output is correct |
54 |
Correct |
1 ms |
10588 KB |
Output is correct |
55 |
Correct |
1 ms |
10588 KB |
Output is correct |
56 |
Correct |
1 ms |
10588 KB |
Output is correct |
57 |
Correct |
1 ms |
10588 KB |
Output is correct |
58 |
Correct |
1 ms |
10584 KB |
Output is correct |
59 |
Correct |
2 ms |
10844 KB |
Output is correct |
60 |
Correct |
1 ms |
10588 KB |
Output is correct |
61 |
Correct |
1 ms |
10588 KB |
Output is correct |
62 |
Correct |
1 ms |
10588 KB |
Output is correct |
63 |
Correct |
1 ms |
10588 KB |
Output is correct |
64 |
Correct |
1 ms |
10588 KB |
Output is correct |
65 |
Correct |
1 ms |
10588 KB |
Output is correct |
66 |
Correct |
1 ms |
10588 KB |
Output is correct |
67 |
Correct |
1 ms |
10588 KB |
Output is correct |
68 |
Correct |
1 ms |
10588 KB |
Output is correct |
69 |
Correct |
1 ms |
10588 KB |
Output is correct |
70 |
Correct |
1 ms |
10588 KB |
Output is correct |
71 |
Correct |
1 ms |
10588 KB |
Output is correct |
72 |
Correct |
1 ms |
10588 KB |
Output is correct |
73 |
Correct |
1 ms |
10588 KB |
Output is correct |
74 |
Correct |
1 ms |
10588 KB |
Output is correct |
75 |
Correct |
1 ms |
10588 KB |
Output is correct |
76 |
Correct |
1 ms |
10588 KB |
Output is correct |
77 |
Correct |
1 ms |
10588 KB |
Output is correct |
78 |
Correct |
1 ms |
10588 KB |
Output is correct |
79 |
Correct |
1 ms |
10588 KB |
Output is correct |
80 |
Correct |
1 ms |
10588 KB |
Output is correct |
81 |
Correct |
2 ms |
10844 KB |
Output is correct |
82 |
Correct |
1 ms |
10588 KB |
Output is correct |
83 |
Correct |
1 ms |
10588 KB |
Output is correct |
84 |
Correct |
1 ms |
10588 KB |
Output is correct |
85 |
Correct |
1 ms |
10588 KB |
Output is correct |
86 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10708 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10704 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10712 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
1 ms |
10588 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
1 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
1 ms |
10588 KB |
Output is correct |
39 |
Correct |
1 ms |
10588 KB |
Output is correct |
40 |
Correct |
1 ms |
10588 KB |
Output is correct |
41 |
Correct |
1 ms |
10588 KB |
Output is correct |
42 |
Correct |
1 ms |
10588 KB |
Output is correct |
43 |
Correct |
1 ms |
10588 KB |
Output is correct |
44 |
Correct |
1 ms |
10588 KB |
Output is correct |
45 |
Correct |
1 ms |
10720 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10716 KB |
Output is correct |
48 |
Correct |
1 ms |
10588 KB |
Output is correct |
49 |
Correct |
1 ms |
10588 KB |
Output is correct |
50 |
Correct |
1 ms |
10588 KB |
Output is correct |
51 |
Correct |
1 ms |
10588 KB |
Output is correct |
52 |
Correct |
1 ms |
10588 KB |
Output is correct |
53 |
Correct |
1 ms |
10584 KB |
Output is correct |
54 |
Correct |
1 ms |
10588 KB |
Output is correct |
55 |
Correct |
1 ms |
10588 KB |
Output is correct |
56 |
Correct |
1 ms |
10588 KB |
Output is correct |
57 |
Correct |
67 ms |
94932 KB |
Output is correct |
58 |
Correct |
68 ms |
94156 KB |
Output is correct |
59 |
Correct |
68 ms |
94160 KB |
Output is correct |
60 |
Correct |
68 ms |
95144 KB |
Output is correct |
61 |
Correct |
67 ms |
94260 KB |
Output is correct |
62 |
Correct |
68 ms |
94160 KB |
Output is correct |
63 |
Correct |
67 ms |
94416 KB |
Output is correct |
64 |
Correct |
67 ms |
94156 KB |
Output is correct |
65 |
Correct |
69 ms |
94156 KB |
Output is correct |
66 |
Correct |
67 ms |
94924 KB |
Output is correct |
67 |
Correct |
68 ms |
94812 KB |
Output is correct |
68 |
Correct |
68 ms |
95444 KB |
Output is correct |
69 |
Correct |
1 ms |
10588 KB |
Output is correct |
70 |
Correct |
1 ms |
10588 KB |
Output is correct |
71 |
Correct |
1 ms |
10584 KB |
Output is correct |
72 |
Correct |
2 ms |
10844 KB |
Output is correct |
73 |
Correct |
1 ms |
10588 KB |
Output is correct |
74 |
Correct |
1 ms |
10588 KB |
Output is correct |
75 |
Correct |
1 ms |
10588 KB |
Output is correct |
76 |
Correct |
1 ms |
10588 KB |
Output is correct |
77 |
Correct |
1 ms |
10588 KB |
Output is correct |
78 |
Correct |
1 ms |
10588 KB |
Output is correct |
79 |
Correct |
1 ms |
10588 KB |
Output is correct |
80 |
Correct |
1 ms |
10588 KB |
Output is correct |
81 |
Correct |
1 ms |
10588 KB |
Output is correct |
82 |
Correct |
1 ms |
10588 KB |
Output is correct |
83 |
Correct |
1 ms |
10588 KB |
Output is correct |
84 |
Correct |
1 ms |
10588 KB |
Output is correct |
85 |
Correct |
1 ms |
10588 KB |
Output is correct |
86 |
Correct |
1 ms |
10588 KB |
Output is correct |
87 |
Correct |
1 ms |
10588 KB |
Output is correct |
88 |
Correct |
1 ms |
10588 KB |
Output is correct |
89 |
Correct |
1 ms |
10588 KB |
Output is correct |
90 |
Correct |
1 ms |
10588 KB |
Output is correct |
91 |
Correct |
1 ms |
10588 KB |
Output is correct |
92 |
Correct |
1 ms |
10588 KB |
Output is correct |
93 |
Correct |
1 ms |
10588 KB |
Output is correct |
94 |
Correct |
2 ms |
10844 KB |
Output is correct |
95 |
Correct |
1 ms |
10588 KB |
Output is correct |
96 |
Correct |
1 ms |
10588 KB |
Output is correct |
97 |
Correct |
1 ms |
10588 KB |
Output is correct |
98 |
Correct |
1 ms |
10588 KB |
Output is correct |
99 |
Correct |
1 ms |
10588 KB |
Output is correct |
100 |
Correct |
46 ms |
70868 KB |
Output is correct |
101 |
Correct |
44 ms |
69932 KB |
Output is correct |
102 |
Correct |
40 ms |
71008 KB |
Output is correct |
103 |
Correct |
40 ms |
69840 KB |
Output is correct |
104 |
Correct |
40 ms |
69840 KB |
Output is correct |
105 |
Correct |
56 ms |
80176 KB |
Output is correct |
106 |
Correct |
52 ms |
80296 KB |
Output is correct |
107 |
Correct |
51 ms |
81084 KB |
Output is correct |
108 |
Correct |
50 ms |
80336 KB |
Output is correct |
109 |
Correct |
52 ms |
80172 KB |
Output is correct |
110 |
Correct |
48 ms |
69712 KB |
Output is correct |
111 |
Correct |
47 ms |
69840 KB |
Output is correct |
112 |
Correct |
45 ms |
70608 KB |
Output is correct |
113 |
Correct |
44 ms |
69836 KB |
Output is correct |
114 |
Correct |
43 ms |
69836 KB |
Output is correct |
115 |
Correct |
71 ms |
102096 KB |
Output is correct |
116 |
Correct |
69 ms |
100924 KB |
Output is correct |
117 |
Correct |
68 ms |
101068 KB |
Output is correct |
118 |
Correct |
69 ms |
101064 KB |
Output is correct |
119 |
Correct |
68 ms |
102064 KB |
Output is correct |
120 |
Correct |
43 ms |
70348 KB |
Output is correct |
121 |
Correct |
45 ms |
71124 KB |
Output is correct |
122 |
Correct |
52 ms |
69988 KB |
Output is correct |
123 |
Correct |
50 ms |
69840 KB |
Output is correct |
124 |
Correct |
48 ms |
85964 KB |
Output is correct |
125 |
Correct |
53 ms |
85456 KB |
Output is correct |
126 |
Correct |
48 ms |
85300 KB |
Output is correct |
127 |
Correct |
53 ms |
86164 KB |
Output is correct |
128 |
Correct |
20 ms |
42704 KB |
Output is correct |
129 |
Correct |
18 ms |
42720 KB |
Output is correct |
130 |
Correct |
17 ms |
42704 KB |
Output is correct |
131 |
Correct |
1 ms |
10588 KB |
Output is correct |