This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
string s;
vector<vector<int>>v;
struct subarbore {
int st, dr, cnt;
};
subarbore rec (int st, int dr, int l) {
if (st + 1 == dr) {
return {1, 1, 1};
}
int mid = *lower_bound(v[l - 1].begin(), v[l - 1].end(), st);
subarbore a = rec(st + 4, mid, l + 1);
subarbore b = rec(mid + 1, dr - 1, l + 1);
if (s[st + 1] == 'a') {
return {a.st + b.st, max(a.dr + b.cnt, b.dr + a.cnt), a.cnt + b.cnt};
}
else {
a.st = a.cnt - a.st + 1;
a.dr = a.cnt - a.dr + 1;
b.st = b.cnt - b.st + 1;
b.dr = b.cnt - b.dr + 1;
swap(a.st, a.dr);
swap(b.st, b.dr);
return {a.cnt + b.cnt - max(a.dr + b.cnt, b.dr + a.cnt) + 1, a.cnt + b.cnt - (a.st + b.st) + 1, a.cnt + b.cnt};
}
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> s;
s = '#' + s;
int cnt = 0;
for (int i = 1; i < s.size(); i++) {
if (s[i] == '(')
cnt++;
if (s[i] == ')')
cnt--;
if (s[i] == ',') {
while (v.size() < cnt)
v.push_back({});
v[cnt - 1].push_back(i);
}
}
subarbore ans = rec(1, s.size(), 1);
cout << ans.dr - ans.st + 1;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:46:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i = 1; i < s.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp:52:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | while (v.size() < cnt)
| ~~~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |