結果

問題 No.1982 [Cherry 4th Tune B] 絶険
ユーザー tentententen
提出日時 2023-06-30 11:55:54
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 4,832 bytes
コンパイル時間 2,976 ms
コンパイル使用メモリ 96,068 KB
実行使用メモリ 99,580 KB
最終ジャッジ日時 2024-07-07 00:18:21
合計ジャッジ時間 34,346 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int q = sc.nextInt();
        PriorityQueue<KeyValue> walls = new PriorityQueue<>();
        int[] lefts = new int[k];
        int[] rights = new int[k];
        int[] colors = new int[k];
        int[] heights = new int[k];
        for (int i = 0; i < k; i++) {
            lefts[i] = sc.nextInt();
            rights[i] = sc.nextInt();
            colors[i] = sc.nextInt();
            heights[i] = sc.nextInt();
            walls.add(new KeyValue(i, lefts[i]));
        }
        PriorityQueue<KeyValue> questions = new PriorityQueue<>();
        long[] values = new long[q];
        for (int i = 0; i < q; i++) {
            questions.add(new KeyValue(i, sc.nextInt()));
            values[i] = sc.nextLong();
        }
        BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
        PriorityQueue<KeyValue> inQueue = new PriorityQueue<>();
        int[] ans = new int[q];
        for (int i = 1; i <= n; i++) {
            while (walls.size() > 0 && walls.peek().value == i) {
                KeyValue x = walls.poll();
                bit.add(x.idx + 1, heights[x.idx]);
                inQueue.add(new KeyValue(x.idx, rights[x.idx]));
            }
            while (questions.size() > 0 && questions.peek().value == i) {
                KeyValue x = questions.poll();
                if (bit.getSum(n) < values[x.idx]) {
                    ans[x.idx] = -1;
                } else {
                    int left = 0;
                    int right = n;
                    while (right - left > 1) {
                        int m = (left + right) / 2;
                        if (bit.getSum(m) >= values[x.idx]) {
                            right = m;
                        } else {
                            left = m;
                        }
                    }
                    ans[x.idx] = colors[left];
                }
            }
            while (inQueue.size() > 0 && inQueue.peek().value == i)  {
                KeyValue x = inQueue.poll();
                bit.add(x.idx + 1, -heights[x.idx]);
            }
        }
        System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
    }
    
    static class KeyValue implements Comparable<KeyValue> {
        int idx;
        int value;
        
        public KeyValue(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(KeyValue another) {
            return value - another.value;
        }
    }
}
class BinaryIndexedTree {
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
    }
    
    public void add(int idx, long value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public long getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public long getSum(int x) {
        int mask = 1;
        long ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0