#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
using namespace std;
mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? (a = b, 1) : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? (a = b, 1) : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
#ifndef LOCAL
const int N = 1e7 + 5;
#else
const int N = 1005;
#endif
string s;
int last[N];
int p[N];
int root;
ve<int> g[N];
int n;
ve<int> pos;
int val[N];
ve<int> ver;
ve<int> order;
int sz[N];
int inc[N];
void dfs(int v) {
if (sz(g[v]) == 0) {
sz[v] = 1;
}
fe (to, g[v]) {
dfs(to);
sz[v] += sz[to];
}
order.pb(v);
}
pii dp[N][2];
int L, R;
void DFS(int v) {
if (!sz(g[v])) {
inc[L]++;
inc[R + 1]--;
return;
}
int to1 = g[v][0];
int to2 = g[v][1];
if (s[v - 1] == 'x') {
L += sz[to1] - dp[to1][0].se;
R += sz[to1] - dp[to1][0].fi;
DFS(to2);
L -= sz[to1] - dp[to1][0].se;
R -= sz[to1] - dp[to1][0].fi;
} else {
L += dp[to1][1].fi;
R += dp[to1][1].se;
DFS(to2);
L -= dp[to1][1].fi;
R -= dp[to1][1].se;
}
swap(to1, to2);
if (s[v - 1] == 'x') {
L += sz[to1] - dp[to1][0].se;
R += sz[to1] - dp[to1][0].fi;
DFS(to2);
L -= sz[to1] - dp[to1][0].se;
R -= sz[to1] - dp[to1][0].fi;
} else {
L += dp[to1][1].fi;
R += dp[to1][1].se;
DFS(to2);
L -= dp[to1][1].fi;
R -= dp[to1][1].se;
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> s;
if (sz(s) > N) {
while (1);
}
last[0] = -1;
int bal = 0;
fr (i, 0, sz(s) - 1) {
if (s[i] == '(' || s[i] == ')') {
if (s[i] == '(') {
bal++;
} else {
bal--;
}
if (s[i] == '(') {
p[i] = last[bal - 1];
last[bal] = i;
}
}
if (s[i] == '?') {
pos.pb(i);
p[i] = last[bal];
n++;
}
}
fr (i, 0, sz(s) - 1) {
if (s[i] == '(' || s[i] == '?') {
if (p[i] == -1) {
root = i;
} else {
g[p[i]].pb(i);
}
ver.pb(i);
// cout << i << " " << s[i - 1] << "\n";
}
}
dfs(root);
{
fe (v, order) {
if (sz(g[v]) == 0) {
dp[v][0] = {0, 0};
dp[v][1] = {0, 0};
continue;
}
int to1 = g[v][0];
int to2 = g[v][1];
if (s[v - 1] == 'x') {
dp[v][0] = {dp[to1][0].fi + dp[to2][0].fi, dp[to1][0].se + dp[to2][0].se};
dp[v][1] = {1e9, -1e9};
umn(dp[v][1].fi, dp[to1][1].fi + dp[to2][1].fi);
umn(dp[v][1].fi, dp[to1][1].fi + (sz[to2] - dp[to2][0].se));
umn(dp[v][1].fi, (sz[to1] - dp[to1][0].se) + dp[to2][1].fi);
umx(dp[v][1].se, dp[to1][1].se + dp[to2][1].se);
umx(dp[v][1].se, dp[to1][1].se + (sz[to2] - dp[to2][0].fi));
umx(dp[v][1].se, (sz[to1] - dp[to1][0].fi) + dp[to2][1].se);
} else {
dp[v][1] = {dp[to1][1].fi + dp[to2][1].fi, dp[to1][1].se + dp[to2][1].se};
dp[v][0] = {1e9, -1e9};
umn(dp[v][0].fi, dp[to1][0].fi + dp[to2][0].fi);
umn(dp[v][0].fi, dp[to1][0].fi + (sz[to2] - dp[to2][1].se));
umn(dp[v][0].fi, (sz[to1] - dp[to1][1].se) + dp[to2][0].fi);
umx(dp[v][0].se, dp[to1][0].se + dp[to2][0].se);
umx(dp[v][0].se, dp[to1][0].se + (sz[to2] - dp[to2][1].fi));
umx(dp[v][0].se, (sz[to1] - dp[to1][1].fi) + dp[to2][0].se);
}
}
}
// cout << dp[3][0].fi << " " << dp[3][0].se << " " << dp[3][1].fi << " " << dp[3][1].se << "\n";
{
DFS(root);
// fr (x, 1, n) {
// fr (me, 0, sz(pos) - 1) {
// int v = pos[me];
//
// int L = 0;
// int R = 0;
//
//// cout << "start " << v << "\n";
//
// int last = -1;
// while (1) {
// if (sz(g[v])) {
//
//// cout << "GO " << v << "\n";
// int to1 = g[v][0];
// int to2 = g[v][1];
//
// if (to1 == last) swap(to1, to2);
//
// if (s[v - 1] == 'x') {
// L += sz[to1] - dp[to1][0].se;
// R += sz[to1] - dp[to1][0].fi;
// } else {
// L += dp[to1][1].fi;
// R += dp[to1][1].se;
// }
//// cout << "dp: " << dp[to1][1].se << "\n";
//// cout << "to1: " << to1 << "\n";
//// cout << "l, r: " << L << " " << R << "\n";
// }
//
// if (v == root) break;
// last = v;
// v = p[v];
// }
//
//// cout << pos[me] << " " << L << " " << R << "\n";
// inc[L]++;
// inc[R + 1]--;
// }
fr (i, 1, n) inc[i] += inc[i - 1];
int ans = 0;
fr (i, 0, n - 1) {
if (inc[i]) {
ans++;
}
}
cout << ans << "\n";
}
return 0;
ve<bool> possib(n + 1);
fr (mask, 0, pw(n - 1) - 1) {
fr (me, 0, sz(pos) - 1) {
fe (x, ver) val[x] = -1;
int x = 1;
int ptr = 0;
fr (i, 0, n - 2) {
if (ptr == me) ptr++;
if (mask & pw(i)) {
val[pos[ptr++]] = 1;
} else {
val[pos[ptr++]] = 0;
x++;
}
}
val[pos[me]] = 2;
bool bad = 0;
fe (v, order) {
if (s[v] == '?') continue;
assert(val[v] == -1);
if (val[g[v][1]] == 2) swap(g[v][0], g[v][1]);
if (val[g[v][0]] == 2) {
if (s[v - 1] == 'x' && val[g[v][1]] == 1) {
bad = 1;
break;
}
if (s[v - 1] == 'n' && val[g[v][1]] == 0) {
bad = 1;
break;
}
val[v] = 2;
} else {
if (s[v - 1] == 'x') {
val[v] = max(val[g[v][0]], val[g[v][1]]);
} else {
val[v] = min(val[g[v][0]], val[g[v][1]]);
}
}
}
if (!bad) {
possib[x] = 1;
}
}
}
int ans = 0;
fr (i, 1, n) {
if (possib[i]) {
// cout << i << "\n";
ans++;
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
235084 KB |
Output is correct |
2 |
Correct |
116 ms |
235136 KB |
Output is correct |
3 |
Correct |
118 ms |
235156 KB |
Output is correct |
4 |
Correct |
108 ms |
235176 KB |
Output is correct |
5 |
Correct |
106 ms |
235084 KB |
Output is correct |
6 |
Correct |
112 ms |
235096 KB |
Output is correct |
7 |
Correct |
123 ms |
235172 KB |
Output is correct |
8 |
Correct |
107 ms |
235176 KB |
Output is correct |
9 |
Correct |
105 ms |
235104 KB |
Output is correct |
10 |
Correct |
110 ms |
235136 KB |
Output is correct |
11 |
Correct |
121 ms |
235116 KB |
Output is correct |
12 |
Correct |
106 ms |
235056 KB |
Output is correct |
13 |
Correct |
106 ms |
235084 KB |
Output is correct |
14 |
Correct |
106 ms |
235160 KB |
Output is correct |
15 |
Correct |
110 ms |
235188 KB |
Output is correct |
16 |
Correct |
104 ms |
235120 KB |
Output is correct |
17 |
Correct |
104 ms |
235140 KB |
Output is correct |
18 |
Correct |
104 ms |
235116 KB |
Output is correct |
19 |
Correct |
117 ms |
235068 KB |
Output is correct |
20 |
Correct |
105 ms |
235084 KB |
Output is correct |
21 |
Correct |
108 ms |
235376 KB |
Output is correct |
22 |
Correct |
104 ms |
235120 KB |
Output is correct |
23 |
Correct |
104 ms |
235084 KB |
Output is correct |
24 |
Correct |
106 ms |
235168 KB |
Output is correct |
25 |
Correct |
105 ms |
235084 KB |
Output is correct |
26 |
Correct |
109 ms |
235148 KB |
Output is correct |
27 |
Correct |
105 ms |
235176 KB |
Output is correct |
28 |
Correct |
107 ms |
235136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
235084 KB |
Output is correct |
2 |
Correct |
116 ms |
235136 KB |
Output is correct |
3 |
Correct |
118 ms |
235156 KB |
Output is correct |
4 |
Correct |
108 ms |
235176 KB |
Output is correct |
5 |
Correct |
106 ms |
235084 KB |
Output is correct |
6 |
Correct |
112 ms |
235096 KB |
Output is correct |
7 |
Correct |
123 ms |
235172 KB |
Output is correct |
8 |
Correct |
107 ms |
235176 KB |
Output is correct |
9 |
Correct |
105 ms |
235104 KB |
Output is correct |
10 |
Correct |
110 ms |
235136 KB |
Output is correct |
11 |
Correct |
121 ms |
235116 KB |
Output is correct |
12 |
Correct |
106 ms |
235056 KB |
Output is correct |
13 |
Correct |
106 ms |
235084 KB |
Output is correct |
14 |
Correct |
106 ms |
235160 KB |
Output is correct |
15 |
Correct |
110 ms |
235188 KB |
Output is correct |
16 |
Correct |
104 ms |
235120 KB |
Output is correct |
17 |
Correct |
104 ms |
235140 KB |
Output is correct |
18 |
Correct |
104 ms |
235116 KB |
Output is correct |
19 |
Correct |
117 ms |
235068 KB |
Output is correct |
20 |
Correct |
105 ms |
235084 KB |
Output is correct |
21 |
Correct |
108 ms |
235376 KB |
Output is correct |
22 |
Correct |
104 ms |
235120 KB |
Output is correct |
23 |
Correct |
104 ms |
235084 KB |
Output is correct |
24 |
Correct |
106 ms |
235168 KB |
Output is correct |
25 |
Correct |
105 ms |
235084 KB |
Output is correct |
26 |
Correct |
109 ms |
235148 KB |
Output is correct |
27 |
Correct |
105 ms |
235176 KB |
Output is correct |
28 |
Correct |
107 ms |
235136 KB |
Output is correct |
29 |
Correct |
111 ms |
235220 KB |
Output is correct |
30 |
Correct |
103 ms |
235124 KB |
Output is correct |
31 |
Correct |
103 ms |
235108 KB |
Output is correct |
32 |
Correct |
103 ms |
235160 KB |
Output is correct |
33 |
Correct |
104 ms |
235084 KB |
Output is correct |
34 |
Correct |
105 ms |
235176 KB |
Output is correct |
35 |
Correct |
107 ms |
235192 KB |
Output is correct |
36 |
Correct |
109 ms |
235104 KB |
Output is correct |
37 |
Correct |
104 ms |
235084 KB |
Output is correct |
38 |
Correct |
108 ms |
235268 KB |
Output is correct |
39 |
Correct |
104 ms |
235120 KB |
Output is correct |
40 |
Correct |
122 ms |
235076 KB |
Output is correct |
41 |
Correct |
104 ms |
235072 KB |
Output is correct |
42 |
Correct |
105 ms |
235160 KB |
Output is correct |
43 |
Correct |
103 ms |
235180 KB |
Output is correct |
44 |
Correct |
108 ms |
235084 KB |
Output is correct |
45 |
Correct |
118 ms |
235176 KB |
Output is correct |
46 |
Correct |
103 ms |
235116 KB |
Output is correct |
47 |
Correct |
109 ms |
235084 KB |
Output is correct |
48 |
Correct |
106 ms |
235180 KB |
Output is correct |
49 |
Correct |
105 ms |
235180 KB |
Output is correct |
50 |
Correct |
113 ms |
235180 KB |
Output is correct |
51 |
Correct |
106 ms |
235124 KB |
Output is correct |
52 |
Correct |
111 ms |
235084 KB |
Output is correct |
53 |
Correct |
111 ms |
235168 KB |
Output is correct |
54 |
Correct |
120 ms |
235064 KB |
Output is correct |
55 |
Correct |
106 ms |
235096 KB |
Output is correct |
56 |
Correct |
107 ms |
235176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
432 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
235084 KB |
Output is correct |
2 |
Correct |
116 ms |
235136 KB |
Output is correct |
3 |
Correct |
118 ms |
235156 KB |
Output is correct |
4 |
Correct |
108 ms |
235176 KB |
Output is correct |
5 |
Correct |
106 ms |
235084 KB |
Output is correct |
6 |
Correct |
112 ms |
235096 KB |
Output is correct |
7 |
Correct |
123 ms |
235172 KB |
Output is correct |
8 |
Correct |
107 ms |
235176 KB |
Output is correct |
9 |
Correct |
105 ms |
235104 KB |
Output is correct |
10 |
Correct |
110 ms |
235136 KB |
Output is correct |
11 |
Correct |
121 ms |
235116 KB |
Output is correct |
12 |
Correct |
106 ms |
235056 KB |
Output is correct |
13 |
Correct |
106 ms |
235084 KB |
Output is correct |
14 |
Correct |
106 ms |
235160 KB |
Output is correct |
15 |
Correct |
110 ms |
235188 KB |
Output is correct |
16 |
Correct |
104 ms |
235120 KB |
Output is correct |
17 |
Correct |
104 ms |
235140 KB |
Output is correct |
18 |
Correct |
104 ms |
235116 KB |
Output is correct |
19 |
Correct |
117 ms |
235068 KB |
Output is correct |
20 |
Correct |
105 ms |
235084 KB |
Output is correct |
21 |
Correct |
108 ms |
235376 KB |
Output is correct |
22 |
Correct |
104 ms |
235120 KB |
Output is correct |
23 |
Correct |
104 ms |
235084 KB |
Output is correct |
24 |
Correct |
106 ms |
235168 KB |
Output is correct |
25 |
Correct |
105 ms |
235084 KB |
Output is correct |
26 |
Correct |
109 ms |
235148 KB |
Output is correct |
27 |
Correct |
105 ms |
235176 KB |
Output is correct |
28 |
Correct |
107 ms |
235136 KB |
Output is correct |
29 |
Correct |
111 ms |
235220 KB |
Output is correct |
30 |
Correct |
103 ms |
235124 KB |
Output is correct |
31 |
Correct |
103 ms |
235108 KB |
Output is correct |
32 |
Correct |
103 ms |
235160 KB |
Output is correct |
33 |
Correct |
104 ms |
235084 KB |
Output is correct |
34 |
Correct |
105 ms |
235176 KB |
Output is correct |
35 |
Correct |
107 ms |
235192 KB |
Output is correct |
36 |
Correct |
109 ms |
235104 KB |
Output is correct |
37 |
Correct |
104 ms |
235084 KB |
Output is correct |
38 |
Correct |
108 ms |
235268 KB |
Output is correct |
39 |
Correct |
104 ms |
235120 KB |
Output is correct |
40 |
Correct |
122 ms |
235076 KB |
Output is correct |
41 |
Correct |
104 ms |
235072 KB |
Output is correct |
42 |
Correct |
105 ms |
235160 KB |
Output is correct |
43 |
Correct |
103 ms |
235180 KB |
Output is correct |
44 |
Correct |
108 ms |
235084 KB |
Output is correct |
45 |
Correct |
118 ms |
235176 KB |
Output is correct |
46 |
Correct |
103 ms |
235116 KB |
Output is correct |
47 |
Correct |
109 ms |
235084 KB |
Output is correct |
48 |
Correct |
106 ms |
235180 KB |
Output is correct |
49 |
Correct |
105 ms |
235180 KB |
Output is correct |
50 |
Correct |
113 ms |
235180 KB |
Output is correct |
51 |
Correct |
106 ms |
235124 KB |
Output is correct |
52 |
Correct |
111 ms |
235084 KB |
Output is correct |
53 |
Correct |
111 ms |
235168 KB |
Output is correct |
54 |
Correct |
120 ms |
235064 KB |
Output is correct |
55 |
Correct |
106 ms |
235096 KB |
Output is correct |
56 |
Correct |
107 ms |
235176 KB |
Output is correct |
57 |
Correct |
104 ms |
235340 KB |
Output is correct |
58 |
Correct |
104 ms |
235396 KB |
Output is correct |
59 |
Correct |
107 ms |
235364 KB |
Output is correct |
60 |
Correct |
104 ms |
235356 KB |
Output is correct |
61 |
Correct |
110 ms |
235404 KB |
Output is correct |
62 |
Correct |
105 ms |
235340 KB |
Output is correct |
63 |
Correct |
103 ms |
235412 KB |
Output is correct |
64 |
Correct |
112 ms |
235452 KB |
Output is correct |
65 |
Correct |
104 ms |
235368 KB |
Output is correct |
66 |
Correct |
111 ms |
235436 KB |
Output is correct |
67 |
Correct |
105 ms |
235296 KB |
Output is correct |
68 |
Correct |
105 ms |
235308 KB |
Output is correct |
69 |
Correct |
105 ms |
235348 KB |
Output is correct |
70 |
Correct |
126 ms |
235316 KB |
Output is correct |
71 |
Correct |
108 ms |
235352 KB |
Output is correct |
72 |
Correct |
106 ms |
235468 KB |
Output is correct |
73 |
Correct |
106 ms |
235404 KB |
Output is correct |
74 |
Correct |
110 ms |
235520 KB |
Output is correct |
75 |
Correct |
108 ms |
235468 KB |
Output is correct |
76 |
Correct |
112 ms |
235448 KB |
Output is correct |
77 |
Correct |
106 ms |
235308 KB |
Output is correct |
78 |
Correct |
106 ms |
235456 KB |
Output is correct |
79 |
Correct |
108 ms |
235516 KB |
Output is correct |
80 |
Correct |
107 ms |
235372 KB |
Output is correct |
81 |
Correct |
117 ms |
235372 KB |
Output is correct |
82 |
Correct |
111 ms |
235428 KB |
Output is correct |
83 |
Correct |
116 ms |
235380 KB |
Output is correct |
84 |
Correct |
111 ms |
235268 KB |
Output is correct |
85 |
Correct |
108 ms |
235308 KB |
Output is correct |
86 |
Correct |
103 ms |
235088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
235084 KB |
Output is correct |
2 |
Correct |
116 ms |
235136 KB |
Output is correct |
3 |
Correct |
118 ms |
235156 KB |
Output is correct |
4 |
Correct |
108 ms |
235176 KB |
Output is correct |
5 |
Correct |
106 ms |
235084 KB |
Output is correct |
6 |
Correct |
112 ms |
235096 KB |
Output is correct |
7 |
Correct |
123 ms |
235172 KB |
Output is correct |
8 |
Correct |
107 ms |
235176 KB |
Output is correct |
9 |
Correct |
105 ms |
235104 KB |
Output is correct |
10 |
Correct |
110 ms |
235136 KB |
Output is correct |
11 |
Correct |
121 ms |
235116 KB |
Output is correct |
12 |
Correct |
106 ms |
235056 KB |
Output is correct |
13 |
Correct |
106 ms |
235084 KB |
Output is correct |
14 |
Correct |
106 ms |
235160 KB |
Output is correct |
15 |
Correct |
110 ms |
235188 KB |
Output is correct |
16 |
Correct |
104 ms |
235120 KB |
Output is correct |
17 |
Correct |
104 ms |
235140 KB |
Output is correct |
18 |
Correct |
104 ms |
235116 KB |
Output is correct |
19 |
Correct |
117 ms |
235068 KB |
Output is correct |
20 |
Correct |
105 ms |
235084 KB |
Output is correct |
21 |
Correct |
108 ms |
235376 KB |
Output is correct |
22 |
Correct |
104 ms |
235120 KB |
Output is correct |
23 |
Correct |
104 ms |
235084 KB |
Output is correct |
24 |
Correct |
106 ms |
235168 KB |
Output is correct |
25 |
Correct |
105 ms |
235084 KB |
Output is correct |
26 |
Correct |
109 ms |
235148 KB |
Output is correct |
27 |
Correct |
105 ms |
235176 KB |
Output is correct |
28 |
Correct |
107 ms |
235136 KB |
Output is correct |
29 |
Correct |
111 ms |
235220 KB |
Output is correct |
30 |
Correct |
103 ms |
235124 KB |
Output is correct |
31 |
Correct |
103 ms |
235108 KB |
Output is correct |
32 |
Correct |
103 ms |
235160 KB |
Output is correct |
33 |
Correct |
104 ms |
235084 KB |
Output is correct |
34 |
Correct |
105 ms |
235176 KB |
Output is correct |
35 |
Correct |
107 ms |
235192 KB |
Output is correct |
36 |
Correct |
109 ms |
235104 KB |
Output is correct |
37 |
Correct |
104 ms |
235084 KB |
Output is correct |
38 |
Correct |
108 ms |
235268 KB |
Output is correct |
39 |
Correct |
104 ms |
235120 KB |
Output is correct |
40 |
Correct |
122 ms |
235076 KB |
Output is correct |
41 |
Correct |
104 ms |
235072 KB |
Output is correct |
42 |
Correct |
105 ms |
235160 KB |
Output is correct |
43 |
Correct |
103 ms |
235180 KB |
Output is correct |
44 |
Correct |
108 ms |
235084 KB |
Output is correct |
45 |
Correct |
118 ms |
235176 KB |
Output is correct |
46 |
Correct |
103 ms |
235116 KB |
Output is correct |
47 |
Correct |
109 ms |
235084 KB |
Output is correct |
48 |
Correct |
106 ms |
235180 KB |
Output is correct |
49 |
Correct |
105 ms |
235180 KB |
Output is correct |
50 |
Correct |
113 ms |
235180 KB |
Output is correct |
51 |
Correct |
106 ms |
235124 KB |
Output is correct |
52 |
Correct |
111 ms |
235084 KB |
Output is correct |
53 |
Correct |
111 ms |
235168 KB |
Output is correct |
54 |
Correct |
120 ms |
235064 KB |
Output is correct |
55 |
Correct |
106 ms |
235096 KB |
Output is correct |
56 |
Correct |
107 ms |
235176 KB |
Output is correct |
57 |
Runtime error |
432 ms |
524288 KB |
Execution killed with signal 9 |
58 |
Halted |
0 ms |
0 KB |
- |