提出 #72701600


ソースコード 拡げる

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

public class Main {
    static class Monster {
        int id;
        long x, y;
        
        Monster(int id, long x, long y) {
            this.id = id;
            this.x = x;
            this.y = y;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int Q = Integer.parseInt(st.nextToken());

        Monster[] monsters = new Monster[N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            long x = Long.parseLong(st.nextToken());
            long y = Long.parseLong(st.nextToken());
            monsters[i] = new Monster(i, x, y);
        }

        Arrays.sort(monsters, (a, b) -> {
            boolean aTop = isTop(a);
            boolean bTop = isTop(b);
            
          
            if (aTop != bTop) {
                return aTop ? -1 : 1; 
            }
            
           
            long cross = (a.x * b.y) - (a.y * b.x);
            
            if (cross == 0) return 0;
            return cross > 0 ? -1 : 1;
        });

   
        int[] monsterGroupMap = new int[N]; 
        ArrayList<Integer> groupCounts = new ArrayList<>();
        
        int currentCount = 1;
        monsterGroupMap[monsters[0].id] = 0;
        int groupIdx = 0;

        for (int i = 1; i < N; i++) {
           
            boolean sameHalf = (isTop(monsters[i]) == isTop(monsters[i-1]));
            long cross = (monsters[i].x * monsters[i-1].y) - (monsters[i].y * monsters[i-1].x);
            
            if (sameHalf && cross == 0) {
                currentCount++;
            } else {
                groupCounts.add(currentCount);
                currentCount = 1;
                groupIdx++;
            }
            monsterGroupMap[monsters[i].id] = groupIdx;
        }
        groupCounts.add(currentCount);

        int groupsSize = groupCounts.size();
        long[] prefixSum = new long[groupsSize];
        prefixSum[0] = groupCounts.get(0);
        for (int i = 1; i < groupsSize; i++) {
            prefixSum[i] = prefixSum[i-1] + groupCounts.get(i);
        }
        long totalMonsters = prefixSum[groupsSize - 1];

        for (int i = 0; i < Q; i++) {
            st = new StringTokenizer(br.readLine());
            int startId = Integer.parseInt(st.nextToken()) - 1;
            int endId = Integer.parseInt(st.nextToken()) - 1;

            int startGroup = monsterGroupMap[startId];
            int endGroup = monsterGroupMap[endId];

            long ans = 0;

            if (startGroup >= endGroup) {
                long countAtEndPrev = (endGroup == 0) ? 0 : prefixSum[endGroup - 1];
                ans = prefixSum[startGroup] - countAtEndPrev;
            } else {
                long countStartPart = prefixSum[startGroup]; 
                long countEndPart = totalMonsters - prefixSum[endGroup - 1]; 
                ans = countStartPart + countEndPart;
            }
            out.println(ans);
        }
        out.flush();
    }

    static boolean isTop(Monster m) {
        if (m.y > 0) return true;
        if (m.y < 0) return false;
        return m.x > 0; 
    }
}

提出情報

提出日時
問題 E - Laser Takahashi
ユーザ addy
言語 Java24 (OpenJDK 24.0.2)
得点 450
コード長 3515 Byte
結果 AC
実行時間 860 ms
メモリ 81272 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 3
AC × 33
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 03_random3_04.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 05_killer_00.txt, 05_killer_01.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 55 ms 38400 KiB
00_sample_01.txt AC 51 ms 38472 KiB
00_sample_02.txt AC 51 ms 38612 KiB
01_random_00.txt AC 396 ms 61440 KiB
01_random_01.txt AC 655 ms 77136 KiB
01_random_02.txt AC 688 ms 78440 KiB
01_random_03.txt AC 166 ms 46556 KiB
01_random_04.txt AC 622 ms 77764 KiB
01_random_05.txt AC 721 ms 79968 KiB
01_random_06.txt AC 752 ms 78036 KiB
01_random_07.txt AC 809 ms 78728 KiB
01_random_08.txt AC 738 ms 80232 KiB
01_random_09.txt AC 713 ms 79988 KiB
02_random2_00.txt AC 687 ms 79812 KiB
02_random2_01.txt AC 722 ms 80380 KiB
02_random2_02.txt AC 842 ms 81060 KiB
02_random2_03.txt AC 797 ms 78272 KiB
02_random2_04.txt AC 752 ms 80280 KiB
02_random2_05.txt AC 782 ms 78104 KiB
02_random2_06.txt AC 731 ms 80376 KiB
02_random2_07.txt AC 778 ms 78332 KiB
02_random2_08.txt AC 697 ms 79744 KiB
02_random2_09.txt AC 860 ms 81252 KiB
03_random3_00.txt AC 808 ms 78008 KiB
03_random3_01.txt AC 829 ms 78772 KiB
03_random3_02.txt AC 645 ms 77392 KiB
03_random3_03.txt AC 784 ms 78648 KiB
03_random3_04.txt AC 693 ms 77892 KiB
04_handmade_00.txt AC 423 ms 75468 KiB
04_handmade_01.txt AC 444 ms 75556 KiB
04_handmade_02.txt AC 235 ms 58260 KiB
05_killer_00.txt AC 859 ms 81272 KiB
05_killer_01.txt AC 846 ms 80780 KiB