結果

問題 No.2942 Sigma Music Game Level Problem
ユーザー tentententen
提出日時 2024-10-21 15:11:03
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,726 bytes
コンパイル時間 6,360 ms
コンパイル使用メモリ 78,232 KB
実行使用メモリ 60,588 KB
最終ジャッジ日時 2024-11-07 22:09:06
合計ジャッジ時間 12,844 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,736 KB
testcase_01 AC 54 ms
37,700 KB
testcase_02 AC 53 ms
37,524 KB
testcase_03 AC 53 ms
37,732 KB
testcase_04 AC 52 ms
37,784 KB
testcase_05 AC 54 ms
37,708 KB
testcase_06 AC 52 ms
37,220 KB
testcase_07 AC 59 ms
37,556 KB
testcase_08 AC 109 ms
39,672 KB
testcase_09 AC 88 ms
39,192 KB
testcase_10 AC 84 ms
38,700 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 55 ms
37,412 KB
testcase_25 AC 53 ms
37,840 KB
testcase_26 AC 846 ms
57,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        sc.nextInt();
        SegmentTree st = new SegmentTree(20001);
        StringBuilder sb = new StringBuilder();
        while (n-- > 0) {
            st.add(sc.nextInt());
        }
        while (q-- > 0) {
            int type = sc.nextInt();
            if (type == 1) {
                st.add(sc.nextInt());
            } else if (type == 2) {
                int left = sc.nextInt();
                int right = sc.nextInt();
                sb.append(st.getCount(left, right + 1)).append(" ").append(st.getSum(left, right + 1)).append("\n");
            } else {
                sc.nextInt();
            }
        }
        System.out.print(sb.length() == 0 ? "Not Found!\n" : sb);
    }
    
    static class SegmentTree {
        int size;
        int[] counts;
        long[] sums;
        
        public SegmentTree(int x) {
            size = 2;
            while (size < x) {
                size <<= 1;
            }
            counts = new int[size * 2 - 1];
            sums = new long[size * 2 - 1];
        }
        
        public void add(int x) {
            int current = x + size - 1;
            counts[current]++;
            sums[current] += x;
            calc((current - 1) / 2);
        }
        
        private void calc(int idx) {
            counts[idx] = counts[idx * 2 + 1] + counts[idx * 2 + 2];
            sums[idx] = sums[idx * 2 + 1] + sums[idx * 2 + 2];
            if (idx > 0) {
                calc((idx - 1) / 2);
            }
        }
        
        public int getCount(int left, int right) {
            return getCount(0, 0, size, left, right);
        }
        
        private int getCount(int idx, int min, int max, int left, int right) {
            if (max <= left || right <= min) {
                return 0;
            } else if (left <= min && max <= right) {
                return counts[idx];
            } else {
                return getCount(idx * 2 + 1, min, (min + max) / 2, left, right)
                    + getCount(idx * 2 + 2, (min + max) / 2, max, left, right);
            }
        }

        public long getSum(int left, int right) {
            return getSum(0, 0, size, left, right);
        }
        
        private long getSum(int idx, int min, int max, int left, int right) {
            if (max <= left || right <= min) {
                return 0;
            } else if (left <= min && max <= right) {
                return sums[idx];
            } else {
                return getSum(idx * 2 + 1, min, (min + max) / 2, left, right)
                    + getSum(idx * 2 + 2, (min + max) / 2, max, left, right);
            }
        }
    }
}

class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");

    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0