結果

問題 No.469 区間加算と一致検索の問題
ユーザー 37zigen37zigen
提出日時 2016-12-20 12:44:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,189 ms / 5,000 ms
コード長 1,656 bytes
コンパイル時間 4,622 ms
コンパイル使用メモリ 75,828 KB
実行使用メモリ 106,260 KB
最終ジャッジ日時 2023-08-23 13:34:00
合計ジャッジ時間 41,782 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
56,184 KB
testcase_01 AC 151 ms
55,576 KB
testcase_02 AC 171 ms
60,160 KB
testcase_03 AC 308 ms
59,132 KB
testcase_04 AC 312 ms
60,940 KB
testcase_05 AC 345 ms
61,308 KB
testcase_06 AC 354 ms
61,620 KB
testcase_07 AC 374 ms
61,356 KB
testcase_08 AC 367 ms
60,772 KB
testcase_09 AC 293 ms
60,776 KB
testcase_10 AC 355 ms
60,856 KB
testcase_11 AC 274 ms
61,104 KB
testcase_12 AC 379 ms
61,568 KB
testcase_13 AC 330 ms
62,748 KB
testcase_14 AC 258 ms
59,896 KB
testcase_15 AC 286 ms
60,332 KB
testcase_16 AC 365 ms
61,088 KB
testcase_17 AC 349 ms
60,220 KB
testcase_18 AC 360 ms
61,068 KB
testcase_19 AC 253 ms
60,364 KB
testcase_20 AC 378 ms
61,416 KB
testcase_21 AC 261 ms
60,368 KB
testcase_22 AC 226 ms
57,396 KB
testcase_23 AC 772 ms
69,712 KB
testcase_24 AC 760 ms
69,464 KB
testcase_25 AC 761 ms
69,488 KB
testcase_26 AC 762 ms
69,696 KB
testcase_27 AC 763 ms
69,624 KB
testcase_28 AC 762 ms
69,404 KB
testcase_29 AC 753 ms
69,464 KB
testcase_30 AC 765 ms
69,452 KB
testcase_31 AC 763 ms
70,948 KB
testcase_32 AC 760 ms
69,428 KB
testcase_33 AC 1,171 ms
104,220 KB
testcase_34 AC 1,181 ms
103,848 KB
testcase_35 AC 1,169 ms
106,260 KB
testcase_36 AC 1,172 ms
104,580 KB
testcase_37 AC 1,183 ms
105,168 KB
testcase_38 AC 1,166 ms
104,836 KB
testcase_39 AC 1,161 ms
104,524 KB
testcase_40 AC 1,169 ms
103,208 KB
testcase_41 AC 1,188 ms
103,832 KB
testcase_42 AC 1,168 ms
104,692 KB
testcase_43 AC 1,186 ms
104,648 KB
testcase_44 AC 1,149 ms
104,484 KB
testcase_45 AC 1,160 ms
104,444 KB
testcase_46 AC 1,159 ms
104,656 KB
testcase_47 AC 1,189 ms
105,444 KB
testcase_48 AC 170 ms
55,908 KB
testcase_49 AC 148 ms
58,020 KB
testcase_50 AC 1,155 ms
104,476 KB
testcase_51 AC 1,165 ms
104,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;

public class Q469 {
	public static void main(String[] args) {
		PrintWriter pw = new PrintWriter(System.out);
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int q = sc.nextInt();
		Random rand = new Random();
		long p2 = 1_000_000_000 + 7;
		long m2 = 1_000_000_000 + 9;
		long p1 = BigInteger.probablePrime(30, rand).longValue();
		long m1 = BigInteger.probablePrime(30, rand).longValue();
		long[] pow1 = new long[n];
		long[] pow2 = new long[n];
		pow1[0] = 1;
		pow2[0] = 1;
		for (int i = 1; i < n; ++i) {
			pow1[i] = pow1[i - 1] * p1 % m1;
			pow2[i] = pow2[i - 1] * p2 % m2;
		}
		long[] accum1 = new long[n];
		long[] accum2 = new long[n];
		accum1[0] = pow1[0];
		accum2[0] = pow2[0];
		for (int i = 1; i < n; ++i) {
			accum1[i] = (accum1[i - 1] + pow1[i]) % m1;
			accum2[i] = accum2[i - 1] + pow2[i];
		}

		HashMap<Long, Integer> map = new HashMap<>();
		map.put(0L, 0);
		long h1 = 0;
		long h2 = 0;
		for (int query = 0; query < q; ++query) {
			String s = sc.next();
			if (s.equals("!")) {
				int l = sc.nextInt();
				int r = sc.nextInt();
				int k = sc.nextInt();
				h1 += (accum1[r - 1] - (l > 0 ? accum1[l - 1] : 0)) * k % m1;
				h2 += (accum2[r - 1] - (l > 0 ? accum2[l - 1] : 0)) * k % m2;
				while (h1 < 0)
					h1 += m1;
				while (h2 < 0)
					h2 += m2;
				h1 %= m1;
				h2 %= m2;
				if (!map.containsKey(h1 << 31 | h2)) {
					map.put(h1 << 31 | h2, query + 1);
				}
			} else {
				pw.println(map.get(h1 << 31 | h2));
			}
		}
		pw.close();
	}
}
0