Submission #71519022


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.TreeSet;

class Main {
    static TreeSet<Pair> set = new TreeSet<>();
    static int answer;
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        FastScanner sc = new FastScanner();

        int N = sc.nextInt();
        answer = N;
        int Q = sc.nextInt();
        set.add(new Pair(-100,-100));
        set.add(new Pair(2000000000,2000000000));

        for (int i = 0; i < Q; i++) {
            int L = sc.nextInt();
            int R = sc.nextInt();

            // System.out.println(L);

            addPair(L,R);

            System.out.println(answer);
        }

        out.flush();
    }

    private static void addPair(int L,int R){
        Pair pair = set.lower(new Pair(L+1,R));
        // System.out.println(L + " " + R + " " + pair.l + " " + pair.r);
        if (L - 1 <= pair.r){
            // System.out.println(1007);
            set.remove(pair);
            answer += (pair.r - pair.l + 1);
            addPair(Math.min(L,pair.l) , Math.max(R,pair.r));

            // answer -= (Math.max(R,pair.r) - Math.min(L,pair.l) + 1) - (pair.r - pair.l + 1);
        }else {
            set.add(new Pair(L,R));
            answer -= (R - L + 1);
        }

        Pair pair1 = set.lower(new Pair(R + 2,R+2));
        // System.out.println(1009 + " " + L + " " + R + " " + pair1.l + " " + pair1.r);
        if (L == pair1.l && R == pair1.r){
            return;
        }
        if (R >= pair1.l - 1){
            // System.out.println(998);
            set.remove(pair1);
            answer += (pair1.r - pair1.l + 1);
            addPair(Math.min(L,pair1.l) , Math.max(R,pair1.r));

            // answer -= (Math.max(R,pair1.r) - Math.min(L,pair1.l) + 1) - (pair1.r - pair1.l + 1);
        }else {
            set.add(new Pair(L,R));
            answer -= (R - L + 1);
        }
    }

    private static class Pair implements Comparable<Pair>{
        int l,r;
        public Pair(int L,int R){
            l = L;
            r = R;
        }

        @Override
        public int compareTo(Pair o) {
            return Integer.compare(l,o.l);
        }
    }

    private static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return buflen > 0;
            }
        }

        private int readByte() {
            if (hasNextByte()) return buffer[ptr++];
            else return -1;
        }

        private static boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
            return !hasNextByte();
        }

        public String next() {
            if (hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}

Submission Info

Submission Time
Task E - Cover query
User AAH_tomato
Language Java24 (OpenJDK 24.0.2)
Score 450
Code Size 4852 Byte
Status AC
Exec Time 830 ms
Memory 123876 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 42
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt
Case Name Status Exec Time Memory
example_00.txt AC 45 ms 37984 KiB
example_01.txt AC 40 ms 38304 KiB
hand_00.txt AC 462 ms 68220 KiB
hand_01.txt AC 526 ms 67776 KiB
hand_02.txt AC 528 ms 67788 KiB
hand_03.txt AC 529 ms 67648 KiB
hand_04.txt AC 578 ms 121384 KiB
hand_05.txt AC 634 ms 123876 KiB
hand_06.txt AC 568 ms 69204 KiB
hand_07.txt AC 432 ms 62372 KiB
hand_08.txt AC 434 ms 62336 KiB
hand_09.txt AC 448 ms 62472 KiB
hand_10.txt AC 421 ms 62404 KiB
hand_11.txt AC 467 ms 64328 KiB
random_00.txt AC 548 ms 69064 KiB
random_01.txt AC 548 ms 68868 KiB
random_02.txt AC 534 ms 72040 KiB
random_03.txt AC 561 ms 71304 KiB
random_04.txt AC 565 ms 69752 KiB
random_05.txt AC 722 ms 68904 KiB
random_06.txt AC 722 ms 70188 KiB
random_07.txt AC 716 ms 69184 KiB
random_08.txt AC 729 ms 69448 KiB
random_09.txt AC 737 ms 68708 KiB
random_10.txt AC 543 ms 68488 KiB
random_11.txt AC 561 ms 69932 KiB
random_12.txt AC 541 ms 67968 KiB
random_13.txt AC 548 ms 68792 KiB
random_14.txt AC 623 ms 69864 KiB
random_15.txt AC 543 ms 71028 KiB
random_16.txt AC 554 ms 71060 KiB
random_17.txt AC 543 ms 71488 KiB
random_18.txt AC 534 ms 70968 KiB
random_19.txt AC 552 ms 71052 KiB
random_20.txt AC 708 ms 63928 KiB
random_21.txt AC 657 ms 63968 KiB
random_22.txt AC 709 ms 66004 KiB
random_23.txt AC 694 ms 64548 KiB
random_24.txt AC 830 ms 73740 KiB
random_25.txt AC 570 ms 71564 KiB
random_26.txt AC 568 ms 71332 KiB
random_27.txt AC 553 ms 71924 KiB