This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Arayi
#include <bits/stdc++.h>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
char dc[] = {'R', 'D', 'L', 'U'};
const int N = 1e7 + 20;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = 1e-13;
const int T = 200;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1)
ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream &operator<<(ostream &c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template <class T>
void maxi(T &a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T &a, T b)
{
a = min(a, b);
}
int n = 1;
str s;
vii a[N];
int mc[N], mn[N];
pir l[N];
void rec(int i)
{
if (s[i] == '?')
{
mc[i] = mn[i] = 1;
return;
}
rec(a[i][0]), rec(a[i][1]);
if (s[i + 1] == 'i')
{
mn[i] = min(mn[a[i][0]], mn[a[i][1]]);
mc[i] = mc[a[i][0]] + mc[a[i][1]];
}
else
{
mn[i] = mn[a[i][0]] + mn[a[i][1]];
mc[i] = min(mc[a[i][0]], mc[a[i][1]]);
}
}
void rc(int i1, int i)
{
l[i] = l[i1];
if (s[i1 + 1] == 'i')
{
l[i].sc += mc[a[i1][0] + a[i1][1] - i];
}
else
{
l[i].fr += mn[a[i1][0] + a[i1][1] - i];
}
if (s[i] != '?')
rc(i, a[i][0]), rc(i, a[i][1]);
}
int pr[N], ans;
int main()
{
fastio;
cin >> s;
stack<int> st;
for (int i = 0; i < (int)s.length(); i++)
{
if (s[i] == 'm')
{
n++;
if (st.size())
{
a[st.top()].ad(i);
}
st.push(i);
}
else if (s[i] == '?')
{
a[st.top()].ad(i);
}
else if (s[i] == ')')
st.pop();
}
rec(0);
rc(0, a[0][0]), rc(0, a[0][1]);
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '?')
{
pr[l[i].fr + 1]++;
pr[n - l[i].sc + 1]--;
}
}
for (int i = 1; i <= n; i++)
{
pr[i] += pr[i - 1];
if (pr[i])
ans++;
}
cout << ans << endl;
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:142:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for (int i = 0; i < s.length(); 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... |