본문 바로가기
Beakjoon&프로그래머스/Java

[백준/Java] 6069번 Switching Lights

by 현장 2026. 3. 25.

-Code

import java.io.*;
import java.util.*;

public class BOJ6069 {
    public static void main(String[] args) throws IOException {
        BufferedReader br =
                new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int lampCnt = Integer.parseInt(st.nextToken());
        int workCnt = Integer.parseInt(st.nextToken());

        boolean[] lampInfo = new boolean[lampCnt + 1];
        for (int work = 0; work < workCnt; work++) {
            st = new StringTokenizer(br.readLine());
            int comm = Integer.parseInt(st.nextToken());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            // 전등 조작
            if (comm == 0) {
                // 반전 처리
                for (int i = start; i <= end; i++) {
                    lampInfo[i] = !lampInfo[i];
                }
            } else {
                // 점등된 전등 수 출력
                int lightsUpCnt = 0;
                for (int i = start; i <= end; i++) {
                    if (lampInfo[i]) lightsUpCnt++;
                }
                System.out.println(lightsUpCnt);
            }
        }
    }
}