0000...00000 으로 총 21개의 0으로 기차의 상태를 체크한다. ( << 연산을 적용하면 10 부터 시작하기 때문) 명령의 경우
case 1 => 해당 위치에 << 연산을 적용시킨 결과와 or 연산 적용 case 2 => 1번에 not 을 적용시킨 결과와 and 연산 적용 case 3 => << 연산으로 한칸 밀어내고 21개의 1 과 and 연산으로 사이즈 조절 case 4 => >> 연산으로 한칸 밀어내고 not 연산을 적용시킨 ( 11111110) 과 and 연산
나올 수 있는 모든 경우를 카운트한다.
참고) 비트마스크 연산
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.Arrays.stream;
public class Main {
static int n, m;
static int[] trains;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] input = stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
n = input[0];
m = input[1];
trains = new int[n + 1];
for (int i = 0; i < m; i++) { // command
input = stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
int command = input[0];
int index = input[1];
int seat = input.length == 2 ? 0 : input[2];
switch (command) {
case 1 -> trains[index] |= (1 << seat);
case 2 -> trains[index] &= ~(1 << seat);
case 3 -> {
trains[index] <<= 1;
trains[index] &= ((1 << 21) - 1);
}
case 4 -> {
trains[index] >>= 1;
trains[index] &= ~1;
}
}
}
int ans = (int) stream(trains, 1, n + 1)
.distinct()
.count();
System.out.println(ans);
}
}