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;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
template <typename T>
void pr(vector<T> &v) {
FOR(i, 0, sz(v)) cout << v[i] << " ";
cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
cout << first << " ";
pr(rest...);
cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
const ll MOD = 1000000007;
#define inf 1e18;
#define INF INT_MAX;
long double PI = 4*atan(1);
long double eps = 1e-8;
struct node {
int a=0,b=0,m=0,t=1;
};
node tree[3000000];
int lft[3000000], rgt[3000000] = {};
string s;
void parse() {
int n = s.length();
stack<int> stk;
int curr = 1;
for(int cp = 0; cp < n;) {
if(s[cp] == 'm') {
int t = -1;
if(s[cp+1] == 'a') t = 1;
if(!stk.empty()) {
if(lft[stk.top()] == 0)
lft[stk.top()] = curr;
else
rgt[stk.top()] = curr;
}
tree[curr].t = t;
stk.push(curr);
curr++;
cp += 4;
}
else if(s[cp] == ')') {
stk.pop(); cp++;
}
else if(s[cp] == '?') {
if(lft[stk.top()] == 0)
lft[stk.top()] = curr;
else
rgt[stk.top()] = curr;
curr++;
cp++;
} else {
cp++;
}
}
}
void dfs(int idx) {
int L = lft[idx];
int R = rgt[idx];
if(L == 0 && R == 0) return;
dfs(L); dfs(R);
tree[idx].m = tree[L].m + tree[R].m + 1;
if(tree[idx].t == 1) {
tree[idx].a = tree[L].a + tree[R].a + 1;
tree[idx].b = max(tree[L].m + tree[R].b + 1, tree[R].m + tree[L].b + 1);
} else {
tree[idx].a = min(tree[L].a, tree[R].a);
tree[idx].b = tree[L].b + tree[R].b;
}
}
int main() {
//auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);cin.tie(0);
// freopen("promote.in", "r", stdin);
// freopen("promote.out", "w", stdout);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> s;
parse();
dfs(1);
cout << tree[1].b - tree[1].a + 1 << endl;
// auto stop = chrono::high_resolution_clock::now();
// auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
// cout << duration.count() << endl;
//cin.close();
//cout.close();
}
# | 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... |