P7073 [CSP-J2020] 表达式
cin.putback()
可以将你已经读入了的吐出来
暴力
暴力建树,暴力更改、求解
#include <iostream>
using namespace std;
const int MaxN = 1e6 + 10;
struct E {
int x; // 数值
char c; // 符号
int l, r, f; // 左儿子,右儿子,父亲
} e[MaxN];
int n, m = 1e5 + 1, q;
int s[MaxN], top;
int x;
int C(int x) {
if (e[x].c) {
return e[x].x = (e[x].c == '&' ? C(e[x].l) && C(e[x].r) : (e[x].c == '!' ? !C(e[x].r) : C(e[x].l) || C(e[x].r)));
} else {
return e[x].x;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
for (char c; cin >> c; s[++top] = x) {
if (c >= '0' && c <= '9') {
cin.putback(c);
break;
} else if (c == 'x') {
cin >> x;
} else if (c == '&' || c == '|') {
e[s[top]].f = x, e[s[top - 1]].f = x;
e[x = m++] = {0, c, s[top--], s[top--], -1};
} else if (c == '!') {
e[x = m++] = {0, c, -1, s[top--], -1};
}
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> e[i].x;
}
// C(s[1]);
for (cin >> q; q; q--) {
int x;
cin >> x;
e[x].x ^= 1;
cout << C(s[1]) << endl;
e[x].x ^= 1;
}
return 0;
}
优化一点
因为只有那一个叶子变了,所以当前叶子从下往上更新就可以了
#include <iostream>
using namespace std;
const int MaxN = 1e6 + 90;
struct E {
bool x; // 数值
char c; // 符号
int l, r, f; // 左儿子,右儿子,父亲
} e[MaxN];
int n, m = 1e5, q;
int s[MaxN], top;
int x;
void update(int u) {
if (e[u].l == -1 && e[u].r == -1) {
update(e[u].f);
return;
}
if (u == -1) {
return;
}
e[u].x = (e[u].c == '&' ? e[e[u].l].x & e[e[u].r].x : (e[u].c == '!' ? !e[e[u].r].x : e[e[u].l].x | e[e[u].r].x));
update(e[u].f);
}
int C(int u) {
if (u == -1) {
return 1;
}
if (e[u].c) {
return e[u].x = (e[u].c == '&' ? C(e[u].l) & C(e[u].r) : (e[u].c == '!' ? !C(e[u].r) : C(e[u].l) | C(e[u].r)));
} else {
return e[u].x;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
for (char c; cin >> c; s[++top] = x) {
if (c >= '0' && c <= '9') {
cin.putback(c);
break;
} else if (c == 'x') {
cin >> x;
} else if (c == '&' || c == '|') {
x = m++;
e[s[top]].f = x, e[s[top - 1]].f = x;
e[x] = {0, c, s[top--], s[top--], -1};
} else if (c == '!') {
x = m++;
e[s[top]].f = x;
e[x] = {0, c, -1, s[top--], -1};
}
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> e[i].x;
e[i].l = e[i].r = -1;
}
C(s[1]);
for (cin >> q; q; q--) {
cin >> x;
e[x].x ^= 1; // 取反
update(x); // 更新
cout << e[s[1]].x << endl; // 输出
e[x].x ^= 1; // 取反
update(x); // 更新
}
return 0;
}
正解
可以记录当前结点改变了,是否会对根造成改变,只要对于当前节点做出一些判断即可:
根结点边了绝对有影响。
-
记录当前结点对根结点有影响。
-
如果当前结点是个
!那么给予它改变的结点就是它的右儿子 -
如果当前节点是个
&:-
如果之前的值是
1,左右儿子都会对它有影响 -
如果它的左儿子是
1,那它的右儿子会对它有影响 -
如果它的右儿子是
1,那它的左儿子会对它有影响
-
-
如果当前节点是个
|:-
如果之前的值是
0,左右儿子都会对它有影响 -
如果它的左儿子是
0,那它的右儿子会对它有影响 -
如果它的右儿子是
0,那它的左儿子会对它有影响
-
#include <iostream>
using namespace std;
const int MaxN = 1e6 + 10;
struct E {
int x; // 数值
char c; // 符号
int l, r, o; // 左儿子,右儿子,是否会对根有影响
} e[MaxN];
int n, m = 1e5 + 1, q;
int s[MaxN], top;
int x;
void F(int u) {
e[u].o = 1;
if (e[u].c == '!') {
F(e[u].r);
} else if (e[u].c == '&') {
if (e[u].x) {
F(e[u].l), F(e[u].r);
} else {
if (e[e[u].l].x) {
F(e[u].r);
}
if (e[e[u].r].x) {
F(e[u].l);
}
}
} else if (e[u].c == '|') {
if (!e[u].x) {
F(e[u].l), F(e[u].r);
} else {
if (!e[e[u].l].x) {
F(e[u].r);
}
if (!e[e[u].r].x) {
F(e[u].l);
}
}
}
}
int C(int u) {
if (u == -1) {
return 1;
}
if (e[u].c) {
return e[u].x = (e[u].c == '&' ? C(e[u].l) & C(e[u].r)
: (e[u].c == '!' ? !C(e[u].r)
: C(e[u].l) | C(e[u].r)));
} else {
return e[u].x;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
for (char c; cin >> c; s[++top] = x) {
if (c >= '0' && c <= '9') {
cin.putback(c);
break;
} else if (c == 'x') {
cin >> x;
} else if (c == '&' || c == '|') {
x = m++;
e[x] = {0, c, s[top--], s[top--]};
} else if (c == '!') {
x = m++;
e[x] = {0, c, -1, s[top--]};
}
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> e[i].x;
}
C(s[1]);
F(s[1]);
for (cin >> q; q; q--) {
cin >> x;
cout << (e[s[1]].x ^ e[x].o) << endl;
}
return 0;
}