QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#432062 | #8770. Comparator | 通顶雪道 (Qiuyang Mang, Youran Sun, Qingshuo Guo)# | TL | 39ms | 15948kb | C++14 | 3.6kb | 2024-06-06 17:32:57 | 2024-06-06 17:32:58 |
Judging History
answer
#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 200005, M = 1100;
char s[N];
namespace eval{
int st1[N], st2[N], top1, top2;
inline int gao(char op){
if(op == '!'){
int x = st2[top2--];
return !x;
}
int y = st2[top2--], x = st2[top2--];
if(op == '=') return x == y;
if(op == '|') return x | y;
if(op == '&') return x & y;
if(op == '^') return x ^ y;
assert(0);
}
inline int calc(char *str, int x, int y){
map<char, int> pri;
pri['('] = 1;
pri['^'] = 2;
pri['|'] = 3;
pri['&'] = 4;
pri['='] = 5;
pri['!'] = 6;
int n = strlen(s + 1);
top1 = 0, top2 = 0;
for(int i = 1; i <= n; i++){
if(str[i] == '0' || str[i] == '1'){
st2[++top2] = str[i] - '0';
continue;
}
if(str[i] == 'x'){
st2[++top2] = x;
continue;
}
if(str[i] == 'y'){
st2[++top2] = y;
continue;
}
if(str[i] == '('){
st1[++top1] = '(';
continue;
}
if(str[i] == ')'){
while(st1[top1] != '('){
int val = gao(st1[top1]);
st2[++top2] = val;
top1--;
}
top1--;
continue;
}
while(top1 && pri[st1[top1]] > pri[str[i]]){
int val = gao(st1[top1]);
st2[++top2] = val;
top1--;
}
st1[++top1] = str[i];
}
while(top1){
int val = gao(st1[top1]);
st2[++top2] = val;
top1--;
}
assert(top2 == 1);
return st2[1];
}
}
inline void test_expression(){
scanf("%s", s + 1);
for(int x = 0; x <= 1; x++)
for(int y = 0; y <= 1; y++){
printf("x = %d, y = %d, f = %d\n", x, y, eval::calc(s, x, y));
}
}
unordered_set<int> vec[M][11][2];
vector<int> in[M], out[N];
int mp[2][2], ed[M][M], buf[M][M], ans1, ans2, ans3, n, k;
bitset<M> outB[M], inB[M];
inline void addedge(int x, int y){
if(x == y) ans1++;
ed[x][y] = 1;
out[x].push_back(y);
outB[x][y] = 1;
in[y].push_back(x);
inB[y][x] = 1;
}
int main(){
read(n), read(k);
int S = (1 << k);
for(int a = 0; a < S; a++){
for(int i = 0; i < k; i++){
int c = ((1 << i) & a) ? 1 : 0;
for(int b = 0; b < S; b++)
vec[b][i][c].insert(a);
}
}
map<int, int> vis;
for(int i = 1, x, y, z; i <= n; i++){
read(x), read(y), --x, --y;
scanf("%s", s + 1);
read(z);
int tmp = x * 10 + y;
for(int u = 0; u <= 1; u++)
for(int v = 0; v <= 1; v++){
mp[u][v] = eval::calc(s, u, v);
tmp = (tmp << 1) | mp[u][v];
}
if(vis[tmp]) continue;
vis[tmp] = 1;
for(int u = 0; u <= 1; u++)
for(int v = 0; v <= 1; v++) if(mp[u][v]){
for(int a = 0; a < S; a++){
int c = ((1 << x) & a) ? 1 : 0;
if(c != u) continue;
vector<int> era;
for(auto b : vec[a][y][v]){
if(z) addedge(a, b);
era.push_back(b);
buf[a][b] = 1;
}
for(auto b : era)
for(int j = 0; j <= k; j++){
int d = ((1 << j) & b) ? 1 : 0;
vec[a][j][d].erase(b);
}
}
}
}
int z;
read(z);
if(z){
for(int a = 0; a < S; a++)
for(int b = 0; b < S; b++)
if(!buf[a][b]) addedge(a, b);
}
for(int x = 0; x < S; x++)
for(int y = 0; y < S; y++)
if(ed[x][y] && ed[y][x]) ans2++;
for(int x = 0; x < S; x++)
for(auto y : out[x])
ans3 += outB[y].count() - (outB[x] & outB[y]).count();
cout << ans1 << " " << ans2 << " " << ans3 << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14020kb
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: 14036kb
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: 39ms
memory: 15948kb
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: -100
Time Limit Exceeded
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...