juruo needs dalaos' help
查看原帖
juruo needs dalaos' help
528287
多喝岩浆楼主2022/5/31 20:56
#include<bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;
struct Suffix_Tree {
	char op; // 操作符 
	bool sum; // 值 false or true 
	int l, r;
} tree[N];
int head[N]; // 每个点是 Suffix_Tree 中的哪个点 
string st;
int n, a[N];
stack <int> s; 
stack <int> g; 
bool b[N]; 
int p = 0;//节点标号 
bool ans;
void build () { // 预处理 ---- 建树  
	int v = 0;//操作数值
	for (int i = 0; i < st.size (); i ++ ) {
		if (st[i] == 'x') { // 处理操作数 gou
			while (st[i + 1] >= '0' && st[i + 1] <= '9') {
				i ++ ;
				v = v * 10 + st[i] - '0';
			}
			s.push (a[v]);
			g.push (v);
			p ++ ;
			head[v] = p;
			tree[p].sum = a[v]; 
			v = 0; 
		}
		if (st[i] == '&') { //处理操作符  '&'
		 	
			int x = s.top (); s.pop ();
			int y = s.top (); s.pop ();
			int x_laber = g.top (); g.pop ();
			int y_laber = g.top (); g.pop ();
			++ p;
			tree[p].op = '&';
			tree[p].sum = x & y;
			tree[p].l = x_laber; tree[p].r = y_laber;
			s.push (x & y);
			g.push (p);
		}
		if (st[i] == '|') { //处理操作符  '|'
			
			int x = s.top (); s.pop ();
			int y = s.top (); s.pop ();
			int x_laber = g.top (); g.pop ();
			int y_laber = g.top (); g.pop ();
			++ p; 
			tree[p].op = '|';
			tree[p].sum = x | y;
			tree[p].l = x_laber; tree[p].r = y_laber;
			s.push (x | y);
			g.push (p);
		}
		if (st[i] == '!') { //处理操作符  '!'
			int asdf = s.top (); s.pop ();
			int u = g.top (); g.pop ();
			asdf = !asdf;
			tree[u].sum = asdf;
			s.push (asdf);
			g.push (u);
		}
	} 
}
bool dfs (int fa, int x) {
	//system ("pause");
	int l = tree[x].l, r = tree[x].r;
	if (fa != 0) {
		int bro;
		if (tree[fa].l == x) bro = tree[fa].r;
		else bro = tree[fa].l;	
		if (b[fa] == 1) {
			if (tree[x].sum == 1 && tree[bro].sum == 0 && tree[fa].op == '&') b[x] = 0;
			if (tree[x].sum == 1 && tree[bro].sum == 1 && tree[fa].op == '&') b[x] = 1;
			if (tree[x].sum == 0 && tree[bro].sum == 0 && tree[fa].op == '&') b[x] = 0;
			if (tree[x].sum == 0 && tree[bro].sum == 1 && tree[fa].op == '&') b[x] = 1;
			if (tree[x].sum == 1 && tree[bro].sum == 0 && tree[fa].op == '|') b[x] = 1;
			if (tree[x].sum == 1 && tree[bro].sum == 1 && tree[fa].op == '|') b[x] = 0;
			if (tree[x].sum == 0 && tree[bro].sum == 0 && tree[fa].op == '|') b[x] = 1;
			if (tree[x].sum == 0 && tree[bro].sum == 1 && tree[fa].op == '|') b[x] = 0;
		}
		else b[x] = 0;
	}
	if(l != 0 && r != 0 && l != fa && r != fa) {
		dfs (x, l);
		dfs (x, r);
	}
}
int main () {
	getline (cin, st);
	cin >> n;
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	build (); // 建树 
//	for (int i = 1; i <= n; i ++ ) cout << tree[i].l << ' ' << tree[i].r << ' ' << char (tree[i].op) << ' ' << tree[i].sum << endl;
	int T;
	cin >> T;
	ans = tree[p].sum;
	b[p] = 1;
//	cout << p << endl;
	dfs (0, p);
	while (T -- ) {
		int w;
		cin >> w;
		if (b[head[w]]) cout << !ans << endl;
		else cout << ans << endl; 
	}
	return 0;
}
2022/5/31 20:56
加载中...