結果

問題 No.2942 Sigma Music Game Level Problem
ユーザー ks2mks2m
提出日時 2024-10-18 22:22:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,389 ms / 6,000 ms
コード長 2,848 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 79,344 KB
実行使用メモリ 159,160 KB
最終ジャッジ日時 2024-10-18 22:50:41
合計ジャッジ時間 24,205 ms
ジャッジサーバーID
(参考情報)
judge7 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
53,680 KB
testcase_01 AC 53 ms
52,396 KB
testcase_02 AC 78 ms
53,348 KB
testcase_03 AC 54 ms
52,584 KB
testcase_04 AC 77 ms
53,092 KB
testcase_05 AC 90 ms
53,448 KB
testcase_06 AC 83 ms
54,904 KB
testcase_07 AC 90 ms
53,272 KB
testcase_08 AC 129 ms
57,808 KB
testcase_09 AC 118 ms
57,928 KB
testcase_10 AC 107 ms
55,296 KB
testcase_11 AC 1,084 ms
112,396 KB
testcase_12 AC 1,219 ms
132,240 KB
testcase_13 AC 1,195 ms
78,056 KB
testcase_14 AC 1,114 ms
116,816 KB
testcase_15 AC 836 ms
133,264 KB
testcase_16 AC 1,148 ms
77,944 KB
testcase_17 AC 792 ms
91,732 KB
testcase_18 AC 1,174 ms
97,712 KB
testcase_19 AC 1,301 ms
100,172 KB
testcase_20 AC 866 ms
111,168 KB
testcase_21 AC 1,107 ms
158,984 KB
testcase_22 AC 1,105 ms
159,160 KB
testcase_23 AC 1,058 ms
77,412 KB
testcase_24 AC 80 ms
53,160 KB
testcase_25 AC 78 ms
53,416 KB
testcase_26 AC 1,389 ms
157,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int q = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		int[] t = new int[q];
		int[] l = new int[q];
		int[] r = new int[q];
		for (int i = 0; i < q; i++) {
			sa = br.readLine().split(" ");
			t[i] = Integer.parseInt(sa[0]);
			l[i] = Integer.parseInt(sa[1]);
			if (t[i] == 2) {
				r[i] = Integer.parseInt(sa[2]);
			}
		}
		br.close();

		int m = 200001;
		FenwickTree ft1 = new FenwickTree(m);
		FenwickTree ft2 = new FenwickTree(m);
		for (int i = 0; i < n; i++) {
			ft1.add(a[i], 1);
			ft2.add(a[i], a[i]);
		}

		boolean flg = false;
		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < q; i++) {
			if (t[i] == 1) {
				ft1.add(l[i], 1);
				ft2.add(l[i], l[i]);
			} else if (t[i] == 2) {
				long v1 = ft1.sum(l[i], r[i] + 1);
				long v2 = ft2.sum(l[i], r[i] + 1);
				pw.println(v1 + " " + v2);
				flg = true;
			}
		}
		if (!flg) {
			pw.println("Not Found!");
		}
		pw.flush();
	}
}

class FenwickTree {
	private int n;
	private long[] data;

	/**
	 * 長さnの配列(a[0]~a[n-1])を作る。初期値は全て0。<br>
	 * O(n)
	 * 
	 * @param n 配列の長さ
	 */
	public FenwickTree(int n) {
		this.n = n;
		data = new long[n];
	}

	/**
	 * 初期データを元にFenwick Treeを構成する。<br>
	 * O(n)
	 * 
	 * @param data 初期データ
	 */
	public FenwickTree(long[] data) {
		this(data.length);
		build(data);
	}

	/**
	 * a[p] += x を行う。<br>
	 * O(log n)
	 * 
	 * @param p 加算位置(0≦p<n)
	 * @param x 加算値
	 */
	void add(int p, long x) {
		assert 0 <= p && p < n : "p=" + p;

		p++;
		while (p <= n) {
			data[p - 1] += x;
			p += p & -p;
		}
	}

	/**
	 * a[l] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param l 開始位置(含む)    (0≦l≦r≦n)
	 * @param r 終了位置(含まない)(0≦l≦r≦n)
	 */
	long sum(int l, int r) {
		assert 0 <= l && l <= r && r <= n : "l=" + l + ", r=" + r;

		return sum(r) - sum(l);
	}

	/**
	 * a[0] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param r 終了位置(含まない)(0≦r≦n)
	 */
	long sum(int r) {
		assert 0 <= r && r <= n : "r=" + r;

		long s = 0;
		while (r > 0) {
			s += data[r - 1];
			r -= r & -r;
		}
		return s;
	}

	private void build(long[] dat) {
		System.arraycopy(dat, 0, data, 0, n);
		for (int i = 1; i <= n; i++) {
			int p = i + (i & -i);
			if (p <= n) {
				data[p - 1] += data[i - 1];
			}
		}
	}
}
0