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>
using namespace std;
const int N = 1e5+5;
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string s, k; cin >> s;
int n = s.size();
vector<int> type(n);
vector<int> l(n, -1), r(n, -1), ma(n), mi(n), siz(n);
for(int i=0; i<s.size(); i++)
{
if(s[i]=='m'){
if(s[i+1]=='a'){
k+="+";
}
else k+="-";
i+=2;
}
else k+=s[i];
}
// cout<<k<<"\n";
s = k;
stack<int> st;
int cnt=-1;
for(int i=0; i<s.size(); i++){
if(s[i]!=',' && s[i]!= '(' && s[i]!= ')'){
cnt++;
if(st.size()){
if(l[st.top()]==-1) l[st.top()] = cnt;
else r[st.top()] = cnt, st.pop();
}
if(s[i]=='+'||s[i]=='-'){
st.push(cnt);
if(s[i]=='+') type[cnt] = 2;
else type[cnt] = 1;
}
}
}
/*
for(int i=0; i<=cnt; i++){
cout<<i<<" "<<l[i]<<" "<<r[i]<<" "<<type[i]<<"\n";
}
*/
auto dfs=[&](int v, auto&&dfs)->void{
if(l[v]==-1 && r[v]==-1){
ma[v] = 1; mi[v] = 1; siz[v]=1;
}
else{
dfs(l[v], dfs); dfs(r[v], dfs);
siz[v]+=siz[l[v]]+siz[r[v]];
if(type[v]==2){
ma[v] = max(siz[l[v]]+ma[r[v]], siz[r[v]]+ma[l[v]]);
mi[v] = mi[l[v]]+mi[r[v]];
}
else{
ma[v] = ma[l[v]]-1+ma[r[v]];
mi[v] = min(mi[l[v]], mi[r[v]]);
}
}
};
dfs(0, dfs);
cout<<ma[0]-mi[0]+1;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:15:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i=0; i<s.size(); i++)
| ~^~~~~~~~~
Main.cpp:34:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i=0; i<s.size(); i++){
| ~^~~~~~~~~
# | 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... |