Submission #683337

#TimeUsernameProblemLanguageResultExecution timeMemory
683337Tenis0206Homework (CEOI22_homework)C++11
100 / 100
114 ms119864 KiB
#include <bits/stdc++.h>

using namespace std;

string s;

int tip[2000005], w[2000005];

int st[2000005],dr[2000005];

pair<int,int> dp[2000005];

int p = 0;

void dfs(int nod)
{
    if(tip[nod]==2)
    {
        dp[nod].first = 1;
        dp[nod].second = 1;
        w[nod] = 1;
        return;
    }
    dfs(st[nod]);
    w[nod] += w[st[nod]];
    dfs(dr[nod]);
    w[nod] += w[dr[nod]];
    if(tip[nod]==0)
    {
        dp[nod].first = min(dp[st[nod]].first,dp[dr[nod]].first);
        dp[nod].second = dp[st[nod]].second + dp[dr[nod]].second - 1;
    }
    else
    {
        dp[nod].first = dp[st[nod]].first + dp[dr[nod]].first;
        dp[nod].second = max(w[st[nod]] + dp[dr[nod]].second, w[dr[nod]] + dp[st[nod]].second);
    }
}

int nod = 1;

void eval()
{
    if(s[p]=='?')
    {
        tip[nod] = 2;
        ++p;
        return;
    }
    if(s[p+1]=='i')
    {
        tip[nod] = 0;
    }
    else
    {
        tip[nod] = 1;
    }
    p += 4;
    int c_nod = nod;
    st[c_nod] = (++nod);
    eval();
    ++p;
    dr[c_nod] = (++nod);
    eval();
    ++p;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>s;
    nod = 1;
    eval();
    dfs(1);
    cout<<dp[1].second - dp[1].first + 1<<'\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...