QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#432811#8770. Comparatorideograph_advantage#AC ✓291ms16096kbC++205.0kb2024-06-07 18:13:502024-06-07 18:13:50

Judging History

你现在查看的是最新测评结果

  • [2024-06-07 18:13:50]
  • 评测
  • 测评结果:AC
  • 用时:291ms
  • 内存:16096kb
  • [2024-06-07 18:13:50]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define iter(v) v.begin(), v.end()
#define SZ(v) int(v.size())
#define pb emplace_back
#define ff first
#define ss second

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#ifdef zisk
void debug(){cerr << "\n";}
template<class T, class ... U>
void debug(T a, U ... b){ cerr << a << " ", debug(b...); }
template<class T> void pary(T l, T r){
	while(l != r) cerr << *l << " ", l++;
	cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif

template<class A, class B>
ostream& operator<<(ostream& o, pair<A, B> p){
	return o << '(' << p.ff << ',' << p.ss << ')';
}

struct condition{
	int a, b;
	string expr;
	int r;
};

int eval(string &expr, int x, int y){
	//debug("eval", expr, x, y);
	vector<int> stk;
	vector<char> opt;
	auto put_val = [&](int v) -> void{
		//debug("put_val", v);
		while(!opt.empty() && opt.back() != '('){
			if(opt.back() == '!'){
				opt.pop_back();
				v = !v;
				continue;
			}
			int t = stk.back();
			stk.pop_back();
			if(opt.back() == '=')
				opt.pop_back(), v = v == t;
			else if(opt.back() == '&')
				opt.pop_back(), v = v && t;
			else if(opt.back() == '|')
				opt.pop_back(), v = v || t;
			else{ // ^
				opt.pop_back(), v = v ^ t;
			}
		}
		stk.pb(v);
	};
	auto put_opt = [&](char c){
		if(c == ')'){
			assert(!stk.empty() && !opt.empty() && opt.back() == '(');
			int t = stk.back();
			opt.pop_back();
			stk.pop_back();
			put_val(t);
		}
		else{
			opt.pb(c);
		}
	};
	for(char c : expr){
		if(c == '0') put_val(0);
		else if(c == '1') put_val(1);
		else if(c == 'x') put_val(x);
		else if(c == 'y') put_val(y);
		else put_opt(c);
		//debug("cur", c);
		//pary(iter(stk));
		//pary(iter(opt));
	}
	assert(SZ(stk) == 1 && opt.empty());
	return stk[0];
}
int prec(char c){
	if(c=='!'){
		return 0;
	}
	if(c=='='){
		return 1;
	}
	if(c=='&'){
		return 2;
	}
	if(c=='|'){
		return 3;
	}
	return 4;
}
int eval2(string& expr,int x,int y){
	vector<int> z,tmp;
	for(auto h:expr){
		if(h=='1' || h=='0' || h=='x' || h=='y'){
			z.push_back(h);
		}
		else{
			if(h=='('){
				tmp.push_back(h);
			}
			else if(h==')'){
				while(tmp.size()){
					if(tmp.back()=='('){
						tmp.pop_back();
						break;
					}
					z.push_back(tmp.back());
					tmp.pop_back();
				}
			}
			else{
				while(tmp.size()){
					if(tmp.back()=='('){
						tmp.push_back(h);
						break;
					}
					if(prec(h)>prec(tmp.back())){
						z.push_back(tmp.back());
						tmp.pop_back();
					}
					else{
						tmp.push_back(h);
						break;
					}
				}
			}
		}
	}
	vector<int> s;
	for(auto h:z){
		if(h=='0'){
			s.push_back(0);
		}
		else if(h=='1'){
			s.push_back(1);
		}
		else if(h=='x'){
			s.push_back(x);
		}
		else if(h=='y'){
			s.push_back(y);
		}
		else if(h=='!'){
			int a=s.back();
			s.pop_back();
			s.push_back(1^a);
		}
		else if(h=='='){
			int a=s.back();
			s.pop_back();
			int b=s.back();
			s.pop_back();
			s.push_back(a==b);
		}
		else if(h=='&'){
			int a=s.back();
			s.pop_back();
			int b=s.back();
			s.pop_back();
			s.push_back(a&&b);
		}
		else if(h=='|'){
			int a=s.back();
			s.pop_back();
			int b=s.back();
			s.pop_back();
			s.push_back(a||b);
		}
		else if(h=='^'){
			int a=s.back();
			s.pop_back();
			int b=s.back();
			s.pop_back();
			s.push_back(a^b);
		}
	}
	return s[0];
}
inline int get(int i,int k){
	return (i>>k)&1;
}
vector<bitset<1024> > in,out;
int main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, k;
	cin >> n >> k;
	int u=(1<<k);
	in.resize(u);
	out.resize(u);
	vector<condition> opt(n+1);
	for(int i = 0; i < n; i++){
		int a, b;
		string expr;
		int r;
		cin >> a >> b >> expr >> r;
		a--;
		b--;
		expr="("+expr+")";
		opt[i] = condition({a, b, expr, r});
	}

	// test
	for(int i = 0; i < n; i++){
		debug("expression", opt[i].expr);
		for(int x : {0, 1}) for(int y : {0, 1}){
			debug("x", x, "y", y, "ret", eval2(opt[i].expr, x, y));
		}
	}

	int else_ret; cin >> else_ret;
	opt[n].r=else_ret;
	int f[10][10][2][2]={0};
	for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			for(int x=0;x<2;x++){
				for(int y=0;y<2;y++){
					f[i][j][x][y]=n;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int x=0;x<2;x++){
			for(int y=0;y<2;y++){
				int z=eval2(opt[i].expr,x,y);
				if(z){
					f[opt[i].a][opt[i].b][x][y]=min(f[opt[i].a][opt[i].b][x][y],i);
				}
			}	
		}
	}
	//debug(f[0][0][0][1],f[0][0][1][0]);
	for(int i=0;i<u;i++){
		for(int j=0;j<u;j++){
			int w=1e9;
			for(int p=0;p<k;p++){
				for(int q=0;q<k;q++){
					w=min(w,f[p][q][get(i,p)][get(j,q)]);
				}
			}
			if(opt[w].r){
				//debug(i,j,w);
				in[i][j]=1;
				out[j][i]=1;
			}
		}
	}
	int cnt1=0,cnt2=0,cnt3=0;
	for(int i=0;i<u;i++){
		if(in[i][i]!=0){
			cnt1++;
		}
	}
	for(int i=0;i<u;i++){
		for(int j=0;j<u;j++){
			if(in[i][j] && in[j][i]){
				cnt2++;
			}
		}
	}
	for(int i=0;i<u;i++){
		for(int j=0;j<u;j++){
			if(in[i][j]){
				continue;
			}
		//	debug(i,j);

		//	cout << (in[i]&out[j]) << "\n";
			cnt3+=(in[i]&out[j]).count();
		}
	}
	cout << cnt1 << " " << cnt2 << " " << cnt3 << "\n";

}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3560kb

input:

3 2
1 1 (x=0)&(y=1) 1
1 1 (x=1)&(y=(x^x)) 0
2 2 (x=1)|(y=0) 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

4 3
2 1 x=0&(y=1) 1
1 2 !x^!!y 0
2 3 ((x|1)=y)&1&1 1
3 1 !x&!x&!x 0
1

output:

3 25 52

result:

ok single line: '3 25 52'

Test #3:

score: 0
Accepted
time: 35ms
memory: 4472kb

input:

1413 3
1 3 0 0
3 3 !x 0
2 2 x=0 1
1 2 !y^0 1
2 3 (x^1) 0
3 2 ((!0)) 1
1 1 !!1=(y) 0
2 2 !(1^x)&y 1
3 2 (y)&1|!!1 0
3 1 !x=(y&y=y) 0
2 1 (((!1)^!x)) 1
2 3 !0=(0&y)=1&y 0
1 2 ((((!0)))|!1) 0
3 1 !(y=!1=x|(!x)) 0
1 1 ((((y=!y)))&!0) 0
2 3 ((y=1)^!1^!!1|0) 1
2 3 1|(!x)&!x|1|(x=1) 1
2 3 !0^!!!!y&0=(!1&!0...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #4:

score: 0
Accepted
time: 274ms
memory: 11992kb

input:

181737 10
5 2 1 1
1 10 !1=!x 0
10 1 (1^x) 0
2 4 !1 1
10 8 y=(!1)^1 1
6 2 !((x&!x)) 1
1 10 !!((!x)|x) 1
7 10 (((0))) 0
7 3 !(1)^!x 0
10 4 (!1)&x 0
7 7 !y&!0 1
8 8 !1=(x)|1^1 1
2 6 ((!!!x)) 0
7 2 1 1
2 2 y=1=0 0
6 3 (!0) 0
6 4 0 0
1 1 (!1) 1
1 8 y 1
3 5 !x|!x^!1 0
4 7 (!0) 0
3 4 !1&1&!1|!0 1
2 7 ((0|1...

output:

1024 1048576 0

result:

ok single line: '1024 1048576 0'

Test #5:

score: 0
Accepted
time: 17ms
memory: 16096kb

input:

1 3
1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

1 1
1 1 x^y|1 0
1

output:

1 1 0

result:

ok single line: '1 1 0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

1 1
1 1 x&y|1 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

1 1
1 1 x=y|1 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3624kb

input:

2 2
1 2 !x&!y 1
1 1 !x&y 0
1

output:

4 12 2

result:

ok single line: '4 12 2'

Test #10:

score: 0
Accepted
time: 161ms
memory: 3988kb

input:

2 10
9 8 ((((!((!x=1))^(!1&(x|x|!y))&((!y&!x=(x=y)))&!((((x=1))&(0=(y))^(!!(!!x^1=x)&(x)^y&1=!x&1=(((!0^(1)^1))^!(((((y))|x|!y))))^!!0=!y&(0)|(y=x&!y&y=x)))=((((!!y&!!0|!0^!0)=!x))))^0&(((!1=!(!x)))|(((((x=1|!y|y)=(!1^1^0^(0)))=!0^1)))=(!(!1^(((((!1)^!x^0))))=(1)^((((y=((x))|(0)|(!1^1)))|(!!!y))=((!...

output:

0 0 0

result:

ok single line: '0 0 0'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

4 3
1 1 !!!!!!x 0
2 1 !!y 1
1 2 !!!!!x 1
2 2 !!!x 0
1

output:

4 16 0

result:

ok single line: '4 16 0'

Test #12:

score: 0
Accepted
time: 2ms
memory: 4268kb

input:

1 1
1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...

output:

1 1 0

result:

ok single line: '1 1 0'

Test #13:

score: 0
Accepted
time: 291ms
memory: 13028kb

input:

200000 10
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10...

output:

512 262144 134217728

result:

ok single line: '512 262144 134217728'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

10 3
3 1 (!x) 1
3 2 !1&x&1&!y 1
2 1 ((x)&y) 1
1 3 0 0
2 2 !1&0=y&0 1
3 3 (!y)^y 1
2 1 0|((!y)) 0
3 2 x 0
2 2 (y|1^x) 0
2 1 ((!0)|y) 0
1

output:

8 64 0

result:

ok single line: '8 64 0'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

0 3
1

output:

8 64 0

result:

ok single line: '8 64 0'