求调代码
查看原帖
求调代码
566833
遥遥领先楼主2023/10/18 21:01

求调这坨代码:

#include <bits/stdc++.h>

using namespace std;

inline int read()
{
	int ww = 0,ee = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-')
		{
			ee = -1;
		}
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		ww = ww * 10 + ch - '0';
		ch = getchar();
	}
	return ww * ee;
}


void file()
{
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
}

const int N = 1000010;

struct node
{
	bool type;
	int ls,rs;
	
	int val;
	
}T[N];

int rt,tt;

int stk[N],tp;

int n,val[N];

int num[N];

int ans;

int is[N];

int cal_the_number(string s,int frm)
{
	int len = s.size();
	int number = 0;
	for (int i = frm;i < len;i++)
	{
		number = number * 10 + s[i] - '0';
	}
	return number;
}


void push_back(node tmp)
{
	tt++;
	if (tmp.type == 0)
	{
		if (tmp.val == '|')
		{
			tmp.ls = stk[tp];
			stk[tp] = tt;
		}
		else
		{
			tmp.ls = stk[tp-1];
			tmp.rs = stk[tp];
			stk[--tp] = tt;
		}
	}
	else
	{
		stk[++tp] = tt;
	}
	T[tt] = tmp;
}


void read_in_and_build_tree()
{
	string str;
	while (cin >> str)
	{
		if (isdigit(str[0]))
		{
			break;
		}
		node _new;
		if (str[0] == 'x')
		{
			_new.type = 1;
			_new.val = cal_the_number(str,1);
		}
		else
		{
			_new.val = str[0];
			_new.type = 0;
		}
	}
	
	rt = stk[tp];
	
	n = cal_the_number(str,0);
	for (int i = 1;i <= n;i++)
	{
		cin >> val[i];
	}
}


bool get_tree_ans(int x)
{
	if (T[x].type == 1)
	{
		return num[x] = val[T[x].val];
	}
	else
	{
		if (T[x].val == '!')
		{
			return num[x] = !get_tree_ans(T[x].ls);
		}
		if (T[x].val == '|')
		{
			return num[x] = get_tree_ans(T[x].ls) | get_tree_ans(T[x].rs);
		}
		if (T[x].val == '&')
		{
			return num[x] = get_tree_ans(T[x].ls) & get_tree_ans(T[x].rs);
		}
	}
}


void dfs(int x)
{
	if (T[x].type == 0)
	{
		if (T[x].val == '!')
		{
			dfs(T[x].ls);
		}
		if (T[x].val == '&')
		{
			if (num[T[x].ls] && !num[T[x].rs])
			{
				dfs(T[x].rs);
			}
			if (!num[T[x].ls] && num[T[x].rs])
			{
				dfs(T[x].ls);
			}
			if (num[T[x].ls] && num[T[x].rs])
			{
				dfs(T[x].ls);
				dfs(T[x].rs);
			}
		}
		if (T[x].val == '|')
		{
			if (num[T[x].ls] && !num[T[x].rs])
			{
				dfs(T[x].ls);
			}
			if (!num[T[x].ls] && num[T[x].rs])
			{
				dfs(T[x].rs);
			}
			if (!num[T[x].ls] && !num[T[x].rs])
			{
				dfs(T[x].ls);
				dfs(T[x].rs);
			}
		}
	}
	else
	{
		is[T[x].val] = 1;
	}
}


void init_the_tree()
{
	ans = get_tree_ans(rt);
	dfs(rt);
}


void read_in_and_answer_the_question()
{
	int q;
	cin >> q;
	while (q--)
	{
		int que;
		cin >> que;
		cout << (ans ^ is[que]) << endl;
	}
}


void cin_fast()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int main()
{
	cin_fast();
	
	read_in_and_build_tree(); 
	
	init_the_tree();
	
	read_in_and_answer_the_question();
	return 0;
}

有点长

2023/10/18 21:01
加载中...