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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib>
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
//#define int long long
#define double long double
const int mod=998244353,mxn=1e7+5,lg=60,inf=1e9,minf=-1e9,Mxn=1e6+50000;
int sz[mxn+10];
int st[mxn+10],ed[mxn+10],type[mxn+10];
pii child[mxn+10];
string a;
pii solve(int id){
if(a[id]=='?'){
sz[id]=1;
return {1,1};
}
pii vl,vr;
vl=solve(child[id].f),vr=solve(child[id].s);
sz[id]=sz[child[id].s]+sz[child[id].f];
if(type[id])return {min(vl.f,vr.f),vr.s+vl.s-1};
else return {vl.f+vr.f,max(vl.s+sz[child[id].s],vr.s+sz[child[id].f])};
}
int32_t main(){
fastio
cin>>a;
if(a=="?")return cout<<1,0;
stack<int>st;
for(int i=0;i<a.size();i++)child[i]={-1,-1};
for(int i=0;i<a.size();i++){
sz[i]=0;
if(a[i]=='('){
type[i]=(a[i-1]=='n');
st.push(i);
}
else if(a[i]==')'){
int x=st.top();
ed[x]=i;
st.pop();
if(st.empty())continue;
if(child[st.top()].f==-1)child[st.top()].f=x;
else child[st.top()].s=x;
}
else if(a[i]=='?'){
if(child[st.top()].f==-1)child[st.top()].f=i;
else child[st.top()].s=i;
}
}
pii ans=solve(3);
cout<<ans.s-ans.f+1;
return 0;
}
/*
keep range l,r->value thats obtainable in subtree
l being the min number of number < x needed to be used
and r being the max
merging-{
min node{
nl=min(l1,l2)-> push value of every node of the other subtree up
nr=(r1+r2-1)->push 1 at the bottom of the other except 1
}
max node{
nl=l1+l2->push 1 at bottom
nr=r1+subsz2 or r2+subsz1 ->taking everything and shift it
}
}
*/
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:60:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i=0;i<a.size();i++)child[i]={-1,-1};
| ~^~~~~~~~~
Main.cpp:61:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=0;i<a.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... |