結果

問題 No.1000 Point Add and Array Add
ユーザー htensaihtensai
提出日時 2020-06-11 17:48:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 886 ms / 2,000 ms
コード長 2,326 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 75,452 KB
実行使用メモリ 87,808 KB
最終ジャッジ日時 2023-09-06 08:20:00
合計ジャッジ時間 13,417 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,212 KB
testcase_01 AC 43 ms
49,420 KB
testcase_02 AC 44 ms
49,276 KB
testcase_03 AC 43 ms
49,252 KB
testcase_04 AC 42 ms
49,360 KB
testcase_05 AC 42 ms
49,472 KB
testcase_06 AC 42 ms
49,460 KB
testcase_07 AC 43 ms
49,500 KB
testcase_08 AC 43 ms
49,612 KB
testcase_09 AC 44 ms
49,352 KB
testcase_10 AC 42 ms
49,264 KB
testcase_11 AC 44 ms
49,224 KB
testcase_12 AC 111 ms
53,752 KB
testcase_13 AC 85 ms
52,200 KB
testcase_14 AC 122 ms
53,184 KB
testcase_15 AC 108 ms
52,388 KB
testcase_16 AC 787 ms
79,300 KB
testcase_17 AC 645 ms
69,948 KB
testcase_18 AC 886 ms
87,316 KB
testcase_19 AC 855 ms
87,624 KB
testcase_20 AC 824 ms
87,264 KB
testcase_21 AC 858 ms
87,808 KB
testcase_22 AC 834 ms
87,448 KB
testcase_23 AC 859 ms
87,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int q = Integer.parseInt(first[1]);
		ArrayDeque<Order> stack = new ArrayDeque<>();
		String[] second = br.readLine().split(" ", n);
		for (int i = 1; i <= n; i++) {
		    stack.push(new Order('A', i, Integer.parseInt(second[i - 1])));
		}
		for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ", 3);
		    stack.push(new Order(line[0].charAt(0), Integer.parseInt(line[1]), Integer.parseInt(line[2])));
		}
		BinaryIndexedTree bit = new BinaryIndexedTree(n + 2);
		long[] ans = new long[n + 1];
		while (stack.size() > 0) {
		    Order x = stack.pop();
		    if (x.type == 'A') {
		        ans[x.left] += x.right * bit.getSum(x.left);
		    } else {
		        bit.add(x.left, 1);
		        bit.add(x.right + 1, -1);
		    }
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= n; i++) {
		    if (i > 1) {
		        sb.append(" ");
		    }
		    sb.append(ans[i]);
		}
		System.out.println(sb);
	}
	
	static class Order {
	    char type;
	    int left;
	    int right;
	    
	    public Order(char type, int left, int right) {
	        this.type = type;
	        this.left = left;
	        this.right = right;
	    }
	}
}

class BinaryIndexedTree {
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
    }
    
    public void add(int idx, int value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public long get(int idx) {
        return getSum(idx) - getSum(idx - 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;
    }
}
0