Problem:

Bit Mask

   A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ th flag corresponds to 1 (ON) or 0 (OFF).
  The state can be managed by the corresponding decimal integer, because the sequence of bits is a binary representation where each bit is 0 or 1.

  On the other hand, a mask is a special bit sequence which can be used to set specified bits of a given bit sequence to ON/OFF. It can also be used to extract/exclude a  bit sequence based on a specified pattern.

  Given a sequence of bits with 64 flags which represent a state, perform the following operations using a set of pre-defined masks. Note that each flag of the bits is initialized by OFF.

  
test(i): 	Print 1 if $i$-th flag is ON, otherwise 0

  
set(m): 	Set flags specified by mask $m$ to ON

  
clear(m):	Set flags specified by mask $m$ to OFF

  
flip(m):	Inverse flags specified by mask $m$

  
all(m):	Print 1 if all flags specified by mask $m$ are ON, otherwise 0

  
any(m):	Print 1 if at least one flag specified by mask $m$ is ON, otherwise 0

  
none(m):	Print 1 if all flags specified by mask $m$ are OFF, otherwise 0

  
count(m):	Print the number of flags specifed by mask $m$ with ON

  
val(m):	Print the decimal value of the state specified by mask $m$

Input

  The input is given in the following format.

$n$
$mask_0$
$mask_1$
:
$mask_{n-1}$
$q$
$query_1$
$query_2$
:
$query_q$

  $n$ represents the number of masks. $mask_i$ represents state of $i$-th mask and is given in the following format:

$k$ $b_0$ $b_1$ ... $b_k$

$k$ is the number of ON in the bits. The following $k$ integers $b_j$ show that $b_j$-th bit is ON.

$query_i$ represents $i$-th query and is given in the following format:

0 $i$

or

1 $m$

or

2 $m$

or

3 $m$

or

4 $m$

or

5 $m$

or

6 $m$

or

7 $m$

or

8 $m$

The first digit 0, 1,...,8 represents the operation test(i), set(m), clear(m), flip(m), all(m), any(m), none(m), count(m) or val(m) respectively.

Output

Print the result in a line for each test, all, any, none, count and val operation.

Constraints

  
$1 \leq n \leq 10$

  
$1 \leq k \leq 64$

  
$1 \leq q \leq 200,000$

  
$0 \leq i < 64$

  
$0 \leq m < n$

Sample Input 1

3
3 0 1 3
1 3
3 0 1 2
8
1 0
2 1
3 1
4 2
5 2
6 2
7 2
8 2

Sample Output 1

0
1
0
2
3



Sample input:
3
3 0 1 3
1 3
3 0 1 2
8
1 0
2 1
3 1
4 2
5 2
6 2
7 2
8 2


Sample output:
0
1
0
2
3

Submission status: Accepted

Solution code:
masks = []
for _ in range(int(input())):
    k, *b = map(int, input().split())
    mask = 0
    for i in range(k):
        mask += 1 << b[i]
    masks.append(mask)
x = 0
MASK = (1<<64) - 1
def test(i):
    global x
    print(+((x & (1 << i)) > 0))
def Set(i):
    global x
    global masks
    x |= masks[i]
def clear(i):
    global x
    global masks
    x = (x | masks[i]) ^ masks[i]
def flip(i):
    global x
    global masks
    x ^= masks[i]
def all(i):
    global x
    global masks
    print(+(x & masks[i] == masks[i]))
def any(i):
    global x
    global masks
    print(+(x & masks[i] > 0))
def none(i):
    global x
    global masks
    print(+(x & masks[i] == 0))
def count(i):
    global x
    global masks
    print(bin(x & masks[i]).count('1'))
def val(i):
    global x
    global masks
    print(x & masks[i])
    
f = [test, Set, clear, flip, all, any, none, count, val].__getitem__
for _ in range(int(input())):
    op, *i = map(int, input().split())
    f(op)(*i)

Submission status: Accepted

Solution code:
def allbits(n,mask):
    tmp_n = format(n,"032b")
    tmp_mask = format(mask,"032b")
    ans = 0
    for i in range(32):
        if tmp_mask[i] == "1" and tmp_n[i] == "1":
            ans += 1
    if ans == tmp_mask.count("1"):
        print(1)
    else:
        print(0)

def anybits(n,mask):
    tmp_n = format(n,"032b")
    tmp_mask = format(mask,"032b")
    flag = False
    for i in range(32):
        if tmp_mask[i] == "1" and tmp_n[i] == "1":
            flag = True
            break
    if flag:
        print(1)
    else:
        print(0)

def nonebits(n,mask):
    tmp_n = format(n,"032b")
    tmp_mask = format(mask,"032b")
    flag = False
    for i in range(32):
        if tmp_mask[i] == "1" and tmp_n[i] == "1":
            flag = True
            break
    if flag:
        print(0)
    else:
        print(1)

def countbits(n,mask):
    tmp_n = format(n,"032b")
    tmp_mask = format(mask,"032b")
    cnt = 0
    for i in range(32):
        if tmp_mask[i] == "1" and tmp_n[i] == "1":
            cnt += 1
    print(cnt)

def bit_reverse(n,mask):
    tmp_n = list(format(n,"032b"))
    tmp_mask = list(format(mask,"032b"))
    for i in range(32):
        if tmp_mask[i] == "1":
            tmp_n[i] = "0" if tmp_n[i] == "1" else "0"
    s = ""
    for i in tmp_n:
        s += i
    return int(s,2)


mask = []
for i in range(int(input())):
    tmp = list(map(int,input().split()))
    s = 0
    for j in tmp[1:]:
        s += 2**j
    mask.append(s)


b = 0
for i in range(int(input())):
    q,m = map(int,input().split())

    if q == 0:##test
        print((b>>m)&1)
    elif q == 1:##set
        b = b|mask[m]
    elif q == 2:##clear
        ##b = bit_reverse(b,mask[m])
        b = b & ~mask[m]
    elif q == 3:##flip
        b = b^mask[m]
    elif q == 4:##all
        ##allbits(b,mask[m])
        print(1 if b&mask[m]==mask[m] else 0)
    elif q == 5:##any
        print(1 if b&mask[m] else 0)
    elif q == 6:##none
        print(1 if not b&mask[m] else 0)
    elif q == 7:##count
        print((format(b&mask[m],"032b")).count("1"))
    elif q == 8:##val
        print(b&mask[m])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Bitset I - Bit Mask
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_10_D&lang=jp

"""
class Bit:
    def __init__(self, masks = []):
        self.data = 0
        self.masks = masks

    def process(self, op, arg):
        funcs = (self.test, self.set, self.clear, self.flip, self.all, self.any, self.none, self.count, self.val)
        funcs[op](arg)

    def test(self, arg):
        print(int(bool(self.data & 1 << int(arg))))

    def set(self, arg):
        for m in self.masks[arg]:
            self.data |= 1 << int(m)

    def clear(self, arg):
        for m in self.masks[arg]:
            self.data &= ~(1 << int(m))

    def flip(self, arg):
        for m in self.masks[arg]:
            self.data ^= 1 << int(m)

    def all(self, arg):
        print(int(all([self.data & 1 << m for m in self.masks[arg]])))

    def any(self, arg):
        print(int(any([self.data & 1 << m for m in self.masks[arg]])))

    def none(self, arg):
        print(int(all([(self.data & 1 << m)==0 << m for m in self.masks[arg]])))

    def count(self, arg):
        print(len([True for m in self.masks[arg] if self.data & 1 << m]))

    def val(self, arg):
        print(sum(self.data & 1 << m for m in self.masks[arg]))


masks = []
for _ in range(int(input())):
    _, *m = input().split()
    m = [int(i) for i in m]
    masks.append(m)
b = Bit(masks)

for _ in range(int(input())):
    op, arg = map(int, input().split())
    b.process(op, arg)

Submission status: Accepted

Solution code:
x = 0
MASK = 2 ** 64 - 1

def x_test(i):
    return int((x & (1 << i)) > 0)
def x_set(m):
    global x
    x |= M[m]
def x_clear(m):
    global x
    x = (x | M[m]) ^ M[m]
def x_flip(m):
    global x
    x ^= M[m]
def x_all(m):
    return int(x & M[m] == M[m])
def x_any(m):
    return int(x & M[m] > 0)
def x_none(m):
    return int(x & M[m] == 0)
def x_count(m):
    return bin(x & M[m]).count("1")
def x_val(m):
    return x & M[m]
    
command = [
    x_test,
    x_set,
    x_clear,
    x_flip,
    x_all,
    x_any,
    x_none,
    x_count,
    x_val
]

M = []
N = int(input())
for i in range(N):
    k, *B = map(int, input().split())
    M.append(sum(1 << b for b in B))
    
q = int(input())
for j in range(q):
    t, cmd = map(int, input().split())
    ans = command[t](cmd)
    if ans is not None:
        print(ans)

Submission status: Accepted

Solution code:
"""
    showBit(n):nを2進数表示
    bitCheck(n,i):nのi桁目を返す
    bitOn(n,i):nのi桁目を1にする
    bitOff(n,i):nのi桁目を0にする
    bitFlip(n,i):nのi桁目を反転する
    isbitAll(n):nが(1<<64)-1か判定
    isbitAny(n):nが0でないか判定
    isbitNone(n):nが0か判定
    bitCount(n):nの1の数を判定
"""
def showBit(n):
    print("{:b}".format(n))

def bitCheck(n,i):
    return (int(bool(n&1<<i)))

def bitOn(n,i):
    return n|1<<i

def MaskOn(n,m):
    return n|m

def bitOff(n,i):
    return n&~(1<<i)

def MaskOff(n,m):
    return n&~m

def bitFlip(n,i):
    return n^1<<i

def MaskFlip(n,m):
    return n^m

def isbitAll(n):
    return int(bitCount(~n)==0)

def isMaskAll(n,m):
    return int(bitCount(~n&m)==0)

def isbitAny(n):
    return int(bitCount(n)>0)

def isMaskAny(n,m):
    return int(bitCount(n&m)>0)

def isbitNone(n):
    return int(bitCount(n)==0)

def isMaskNone(n,m):
    return int(bitCount(n&m)==0)

def bitCount(n):
    return bin(n).count('1')

def MaskCount(n,m):
    return bitCount(n&m)



b=0
masks=[]
n=int(input())
for i in range(n):
    mask=0
    m=list(map(int,input().split()))
    for j in range(m[0]):
        mask=bitOn(mask,m[j+1])
    masks.append(mask)

q=int(input())
for i in range(q):
    qry=list(map(int,input().split()))
    if qry[0]==0:
        print(bitCheck(b,qry[1]))
    elif qry[0]==1:
        b=MaskOn(b,masks[qry[1]])
    elif qry[0]==2:
        b=MaskOff(b,masks[qry[1]])
    elif qry[0]==3:
        b=MaskFlip(b,masks[qry[1]])
    elif qry[0]==4:
        print(isMaskAll(b,masks[qry[1]]))
    elif qry[0]==5:
        print(isMaskAny(b,masks[qry[1]]))
    elif qry[0]==6:
        print(isMaskNone(b,masks[qry[1]]))
    elif qry[0]==7:
        print(MaskCount(b,masks[qry[1]]))
    elif qry[0]==8:
        print(b&masks[qry[1]])

Submission status: Accepted

Solution code:
def main():

    m = int(input())

    m_list = list()
    flag  = 1
    state = 0

    for _ in range(m):
        inp = input().split()
        p = [int(a) for a in inp]

        s = 0
        for i in range(len(p)):
            if i == 0:
                continue
            s = s | (flag << p[i])

        m_list.append(s)

    q = int(input())

    for _ in range(q):

        param = input().split()
        c = int(param[0])
        if len(param) >= 2:
            param[1] = int(param[1])

        if c == 0:
            if state & (flag << (param[1])) :
                print("1")
            else:
                print("0")

        elif c == 1:
            state = state | m_list[param[1]]

        elif c == 2:
            state = state & (~m_list[param[1]] & 0xffffffffffffffff)
            
        elif c == 3:
            state = state ^ m_list[param[1]]

        elif c == 4:
            if state & m_list[param[1]] == m_list[param[1]]:
                print("1")
            else:
                print("0")
            
        elif c == 5:
            if (state & m_list[param[1]]) != 0:
                print("1")
            else:
                print("0")

        elif c == 6:
            if (state & m_list[param[1]]) == 0:
                print("1")
            else:
                print("0")

        elif c == 7:
            current = state & m_list[param[1]]
            print(format(current, "064b").count("1"))

        elif c == 8:
            current = state & m_list[param[1]]
            print(int(current))

main()

Submission status: Accepted

Solution code:
flag=[0 for i in range(64)]

def Test(flag,i):
    print(flag[i])

def Set(flag,m):
    for i in m:
        flag[i]=1
    return flag

def Clear(flag,m):
    for i in m:
        flag[i]=0
    return flag

def Flip(flag,m):
    for i in m:
        if flag[i]==0:
            flag[i]=1
        else:
            flag[i]=0
    return flag

def All(flag,m):
    judge=[flag[i] for i in m]
    if all(judge)==True:
        print(1)
    else:
        print(0)

def Any(flag,m):
    judge=[flag[i] for i in m]
    if any(judge)==True:
        print(1)
    else:
        print(0)

def none(flag,m):
    judge=[flag[i] for i in m]
    if not any(judge)==True:
        print(1)
    else:
        print(0)

def Count(flag,m):
    judge=[flag[i] for i in m]
    print(judge.count(1))

def Val(flag,m):
    a=0
    for i in m:
        a+=flag[i]*2**i
    print(a)

n=int(input())
mask=[]
for i in range(n):
    m=list(map(int,input().split()))
    mask.append(m[1:])

q=int(input())
for i in range(q):
    query=list(map(int,input().split()))
    if query[0]==0:
        Test(flag,query[1])
    elif query[0]==1:
        flag=Set(flag,mask[query[1]])
    elif query[0]==2:
        flag=Clear(flag,mask[query[1]])
    elif query[0]==3:
        flag=Flip(flag,mask[query[1]])
    elif query[0]==4:
        All(flag,mask[query[1]])
    elif query[0]==5:
        Any(flag,mask[query[1]])
    elif query[0]==6:
        none(flag,mask[query[1]])
    elif query[0]==7:
        Count(flag,mask[query[1]])
    else:
        Val(flag,mask[query[1]])

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    bits = [0] * 64

    num_mask = int(input())
    masks = [[] for _ in range(num_mask)]  # type: ignore
    for idx in range(num_mask):
        k, *v = map(lambda x: int(x), input().split())
        for value in v:
            masks[idx].append(value)

    num_query = int(input())
    for _ in range(num_query):
        op, *v = map(lambda x: int(x), input().split())
        if (0 == op):
            if (1 == bits[v[0]]):
                print(1)
            else:
                print(0)
        elif (1 == op):
            for m_idx in masks[v[0]]:
                bits[m_idx] = 1
        elif (2 == op):
            for m_idx in masks[v[0]]:
                bits[m_idx] = 0
        elif (3 == op):
            for m_idx in masks[v[0]]:
                bits[m_idx] = 1 if bits[m_idx] == 0 else 0
        elif (4 == op):
            all_flg = 1
            for m_idx in masks[v[0]]:
                all_flg *= bits[m_idx]
            if all_flg:
                print(1)
            else:
                print(0)
        elif (5 == op):
            any_flg = 0
            for m_idx in masks[v[0]]:
                any_flg += bits[m_idx]
            if any_flg:
                print(1)
            else:
                print(0)
        elif (6 == op):
            none_flg = 0
            for m_idx in masks[v[0]]:
                none_flg += bits[m_idx]
            if not none_flg:
                print(1)
            else:
                print(0)
        elif (7 == op):
            count = 0
            for m_idx in masks[v[0]]:
                count += bits[m_idx]
            print(count)
        else:
            ans = [0] * 64
            for m_idx in masks[v[0]]:
                ans[m_idx] = bits[m_idx]
            bin_str = "".join([str(elem) for elem in ans[::-1]])
            print(f"{int(bin_str, 2)}")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Bitset I - Bit Mask
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_10_D&lang=jp

"""
class Bit:
    def __init__(self, masks = []):
        self.data = 0
        self.masks = masks

    def process(self, op, arg):
        funcs = (self.test, self.set, self.clear, self.flip, self.all, self.any, self.none, self.count, self.val)
        funcs[op](arg)

    def test(self, arg):
        print(int(bool(self.data & 1 << int(arg))))

    def set(self, arg):
        for m in self.masks[arg]:
            self.data |= 1 << int(m)

    def clear(self, arg):
        for m in self.masks[arg]:
            self.data &= ~(1 << int(m))

    def flip(self, arg):
        for m in self.masks[arg]:
            self.data ^= 1 << int(m)

    def all(self, arg):
        print(int(all([self.data & 1 << m for m in self.masks[arg]])))

    def any(self, arg):
        print(int(any([self.data & 1 << m for m in self.masks[arg]])))

    def none(self, arg):
        print(int(all([(self.data & 1 << m)==0 << m for m in self.masks[arg]])))

    def count(self, arg):
        print(len([True for m in self.masks[arg] if self.data & 1 << m]))

    def val(self, arg):
        print(sum(self.data & 1 << m for m in self.masks[arg]))


masks = []
for _ in range(int(input())):
    _, *m = input().split()
    m = [int(i) for i in m]
    masks.append(m)
b = Bit(masks)

for _ in range(int(input())):
    op, arg = map(int, input().split())
    b.process(op, arg)

Submission status: Accepted

Solution code:
n = int(input())

list_mask = []

for _ in range(n):

    k, *list_b = map(int, input().split())

    mask = 0

    for i in range(k):
        mask += 2 ** list_b[i]

    list_mask.append(mask)

q = int(input())

bit_flag = 0


for _ in range(q):

    command, *list_num = input().split()

    m = int(list_num[0])


    if   command == "0":
        # test(i)
        i = m
        if bit_flag & (2 ** i):
            print(1)
        else:
            print(0)
    elif command == "1":
        # set(m)
        bit_flag = bit_flag | list_mask[m]
    elif command == "2":
        # clear(m)
        bit_flag = bit_flag & ~list_mask[m]
    elif command == "3":
        # flip(m)
        bit_flag = bit_flag ^ list_mask[m]
    elif command == "4":
        # all(m)
        if (bit_flag & list_mask[m]) == list_mask[m]:
            print(1)
        else:
            print(0)
    elif command == "5":
        # any(m)
        if bit_flag & list_mask[m]:
            print(1)
        else:
            print(0)
    elif command == "6":
        # none(m)
        if not bit_flag & list_mask[m]:
            print(1)
        else:
            print(0)
    elif command == "7":
        # count(m)
        print(bin(bit_flag & list_mask[m]).count("1"))
    elif command == "8":
        # val(m)
        print(bit_flag & list_mask[m])
    else:
        raise

Submission status: Accepted

Solution code:
mask = (1<<64)-1
a = 0
def test(a,i):
    if a>>i & 1:
        print(1)
    else:
        print(0)
def _set(a,m):
    a = a | mask[m]
    return a
def clear(a,m):
    a = a & ~mask[m]
    return a
def flip(a,m):
    a = a ^ mask[m]  
    return a
def _all(a,m):
    if a & mask[m] == mask[m]:
        print(1)
    else:
        print(0)
def _any(a,m):
    if a&mask[m] == 0:
        print(0)
    else:
        print(1)
def none(a,m):
    if a & mask[m] == 0:
        print(1)
    else:
        print(0)
def count(a,m):
    print(bin(a&mask[m]).count('1'))
def val(a, m):
    return a & mask[m]
n = int(input())
mask = []
for i in range(n):
    ans = 0
    s, *ma = map(int,input().split())
    for i in ma:
        ans = ans | (1<<i)
    mask += [ans]
q = int(input())
for i in range(q):
    s, *t = map(int, input().split())
    if s == 0:test(a,t[0])
    if s == 1:a = _set(a,t[0])
    if s == 2:a = clear(a,t[0])
    if s == 3:a = flip(a,t[0])
    if s == 4:_all(a,t[0])
    if s == 5:_any(a,t[0])
    if s == 6:none(a, t[0])
    if s == 7:count(a,t[0])
    if s == 8:print(val(a,t[0]))

Submission status: Accepted

Solution code:
flag=[0 for i in range(64)]

def Test(flag,i):
    print(flag[i])

def Set(flag,m):
    for i in m:
        flag[i]=1
    return flag

def Clear(flag,m):
    for i in m:
        flag[i]=0
    return flag

def Flip(flag,m):
    for i in m:
        if flag[i]==0:
            flag[i]=1
        else:
            flag[i]=0
    return flag

def All(flag,m):
    judge=[flag[i] for i in m]
    if all(judge)==True:
        print(1)
    else:
        print(0)

def Any(flag,m):
    judge=[flag[i] for i in m]
    if any(judge)==True:
        print(1)
    else:
        print(0)

def none(flag,m):
    judge=[flag[i] for i in m]
    if not any(judge)==True:
        print(1)
    else:
        print(0)

def Count(flag,m):
    judge=[flag[i] for i in m]
    print(judge.count(1))

def Val(flag,m):
    a=0
    for i in m:
        a+=flag[i]*2**i
    print(a)

n=int(input())
mask=[]
for i in range(n):
    m=list(map(int,input().split()))
    mask.append(m[1:])

q=int(input())
for i in range(q):
    query=list(map(int,input().split()))
    if query[0]==0:
        Test(flag,query[1])
    elif query[0]==1:
        flag=Set(flag,mask[query[1]])
    elif query[0]==2:
        flag=Clear(flag,mask[query[1]])
    elif query[0]==3:
        flag=Flip(flag,mask[query[1]])
    elif query[0]==4:
        All(flag,mask[query[1]])
    elif query[0]==5:
        Any(flag,mask[query[1]])
    elif query[0]==6:
        none(flag,mask[query[1]])
    elif query[0]==7:
        Count(flag,mask[query[1]])
    else:
        Val(flag,mask[query[1]])

Submission status: Accepted

Solution code:
q = int(input())
 
MASK = (1 << 64) -1

mask = []
for i in range(q):
    retmask = 0
    ret = [i for i in map(int,input().split())]
    for i in range(1,ret[0]+1):
        retmask += 1 << ret[i]
    mask.append(retmask)

q = int(input())
n = 0
for i in range(q):
    query = [i for i in map(int,input().split())]
    if query[0] == 0:
        temp = 1 << query[1]
        if (n & temp) == 0:
            print(0)
        else:
            print(1)
 
    elif query[0] == 1:
        n = (n | mask[query[1]])
 
    elif query[0] == 2:
        n = n & (~ mask[query[1]] & MASK)
         
    elif query[0] == 3:
        n = n ^ mask[query[1]]
         
    elif query[0] == 4:
        if n & mask[query[1]] == mask[query[1]]:
            print(1)
        else:
            print(0)

        
            
    elif query[0] == 5:
        if n & mask[query[1]] > 0:
            print(1)
        else:
            print(0)
             
    elif query[0] == 6:
        if n & mask[query[1]] == 0:
            print(1)
        else:
            print(0)
            
    elif query[0] == 7:
        print(bin(n & mask[query[1]]).count("1"))
        
         
    elif query[0] == 8:
        print(mask[query[1]] & n)

Submission status: Accepted

Solution code:
import sys


class BitFlag:
    #ALL_ON  = 0xffffffffffffffff
    ALL_OFF = 0x0000000000000000

    def __init__(self, mask_digits):
        self.FLAGS = self.ALL_OFF
        self.MaskFor1bit = [1 << i for i in range(64)]
        self.Masks = [self.make_mask(digits) for digits in mask_digits]

    @staticmethod
    def make_mask(digits):
        ret = 0
        for digit in digits:
            ret += 1 << digit
        return ret

    def _test(self, i):
        return self.FLAGS & self.MaskFor1bit[i] != self.ALL_OFF

    def _set(self, m):
        self.FLAGS |= self.Masks[m]

    def _clear(self, m):
        self.FLAGS &= ~self.Masks[m]

    def _flip(self, m):
        self.FLAGS ^= self.Masks[m]

    def _all(self, m):
        return self.FLAGS & self.Masks[m] == self.Masks[m]

    def _any(self, m):
        return self.FLAGS & self.Masks[m] != self.ALL_OFF

    def _none(self, m):
        return self.FLAGS & self.Masks[m] == self.ALL_OFF

    def _count(self, m):
        return bin(self.FLAGS & self.Masks[m]).count('1')

    def _val(self, m):
        return self.FLAGS & self.Masks[m]


nm = int(input())
digits = []
for i in range(nm):
    k, *arg = map(int, input().split())
    digits.append(arg)

bf = BitFlag(digits)

commands = {'0': bf._test,
            '1': bf._set,
            '2': bf._clear,
            '3': bf._flip,
            '4': bf._all,
            '5': bf._any,
            '6': bf._none,
            '7': bf._count,
            '8': bf._val
            }

qn = int(input())
lines = sys.stdin.readlines()
ans = [None] * qn

for i in range(qn):
    q, arg = lines[i].split()
    ans[i] = commands[q](int(arg))

[print(int(x)) for x in ans if x is not None]

Submission status: Accepted

Solution code:
n = int(input())
masklist = [[] for i in range(n)]
for i in range(n):
    mask = list(map(int, input().split()))
    masklist[i] = mask[1:mask[0]+1]

q = int(input())
bin_q = format(0, '064b')
qlist = list(bin_q)

for _ in range(q):
    query = list(map(int, input().split()))
    command = query[0]
    masknum = query[1]

    if command == 0:    # test
        i = 63 - query[1]
        print(qlist[i])

    elif command == 1:  # set
        for m in masklist[masknum]:
            qlist[63 - m] = '1'

    elif command == 2:  # clear
        for m in masklist[masknum]:
            qlist[63 - m] = '0'

    elif command == 3:  # flip
        for m in masklist[masknum]:
            if qlist[63 - m] == '0':
                qlist[63 - m] = '1'
            else:
                qlist[63 - m] = '0'

    elif command == 4:  # all
        for m in masklist[masknum]:
            if qlist[63 - m] == '0':
                print(0)
                break
        else:
            print(1)

    elif command == 5:  # any
        for m in masklist[masknum]:
            if qlist[63 - m] == '1':
                print(1)
                break
        else:
            print(0)

    elif command == 6:  # none
        for m in masklist[masknum]:
            if qlist[63 - m] == '1':
                print(0)
                break
        else:
            print(1)

    elif command == 7:  # count
        cnt = 0
        for m in masklist[masknum]:
            if qlist[63 - m] == '1':
                cnt += 1
        print(cnt)

    elif command == 8:  # val
        sum = 0
        for m in masklist[masknum]:
            if qlist[63 - m] == '1':
                sum += pow(2, m)
        print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# ITP2_10_C: Bitset 1 - Bit Flag


class BitMask:
    def __init__(self, size):
        self.size = size
        self.masks = []
        self.data = 0 & (1 << self.size)

    def add_mask(self, m):
        mask = 0 & (1 << self.size)
        for i in m:
            mask |= 1 << i
        self.masks.append(mask)

    def set(self, m):
        self.data |= self.masks[m]

    def test(self, i):
        return self.data & (1 << i) > 0

    def clear(self, m):
        self.data &= ~(self.masks[m])

    def flip(self, m):
        self.data ^= (self.masks[m])

    def all(self, m):
        return self.data & self.masks[m] == self.masks[m]

    def any(self, m):
        return not self.data & self.masks[m] == 0

    def none(self, m):
        return self.data & self.masks[m] == 0

    def count(self, m):
        def test(i):
            return data & (1 << i) > 0
        data = self.data & self.masks[m]
        return sum(1 for i in range(self.size) if test(i))

    def val(self, m):
        return self.data & self.masks[m]


def run():
    bit = BitMask(64)
    m = int(input())
    for _ in range(m):
        mask = [int(i) for i in input().split()]
        bit.add_mask(mask[1:])

    q = int(input())
    for _ in range(q):
        cmd, val = [int(i) for i in input().split()]

        if cmd == 0:
            if bit.test(val):
                print(1)
            else:
                print(0)
        elif cmd == 1:
            bit.set(val)
        elif cmd == 2:
            bit.clear(val)
        elif cmd == 3:
            bit.flip(val)
        elif cmd == 4:
            if bit.all(val):
                print(1)
            else:
                print(0)
        elif cmd == 5:
            if bit.any(val):
                print(1)
            else:
                print(0)
        elif cmd == 6:
            if bit.none(val):
                print(1)
            else:
                print(0)
        elif cmd == 7:
            print(bit.count(val))
        elif cmd == 8:
            print(bit.val(val))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
MASK = (1 << 64) - 1

x = 0
def c_test(i):
    return+(x & (1 << i) > 0)
def c_set(m):
    global x
    x |= M[m]
def c_clear(m):
    global x
    m = M[m]
    x = (x | m) ^ m
def c_flip(m):
    global x
    x ^= M[m]
def c_all(m):
    m = M[m]
    return+(x & m == m)
def c_any(m):
    m = M[m]
    return+(x & m > 0)
def c_none(m):
    m = M[m]
    return+(x & m == 0)
def c_count(m):
    m = M[m]
    return bin(x & m).count('1')
def c_val(m):
    m = M[m]
    return x & m


fmap = [
    c_test,
    c_set,
    c_clear,
    c_flip,
    c_all,
    c_any,
    c_none,
    c_count,
    c_val,
]

def query(Q):
    for q in range(Q):
        t, *cmd = map(int, input().split())
        res = fmap[t](*cmd)
        if res is not None:
            yield res


M = []
N = int(input())
for i in range(N):
    k, *B = map(int, input().split())
    M.append(sum(1 << b for b in B))

Q = int(input())

print(*query(Q), sep='\n')