結果

問題 No.469 区間加算と一致検索の問題
ユーザー tanzakutanzaku
提出日時 2016-12-19 00:14:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 517 ms / 5,000 ms
コード長 6,990 bytes
コンパイル時間 4,007 ms
コンパイル使用メモリ 79,548 KB
実行使用メモリ 127,312 KB
最終ジャッジ日時 2023-08-23 13:17:09
合計ジャッジ時間 19,433 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,716 KB
testcase_01 AC 44 ms
49,360 KB
testcase_02 AC 80 ms
57,436 KB
testcase_03 AC 97 ms
53,076 KB
testcase_04 AC 107 ms
53,152 KB
testcase_05 AC 109 ms
53,048 KB
testcase_06 AC 113 ms
52,904 KB
testcase_07 AC 116 ms
52,984 KB
testcase_08 AC 120 ms
55,172 KB
testcase_09 AC 84 ms
53,328 KB
testcase_10 AC 98 ms
53,220 KB
testcase_11 AC 88 ms
53,324 KB
testcase_12 AC 119 ms
53,252 KB
testcase_13 AC 102 ms
53,188 KB
testcase_14 AC 76 ms
51,620 KB
testcase_15 AC 76 ms
52,236 KB
testcase_16 AC 115 ms
53,228 KB
testcase_17 AC 111 ms
52,944 KB
testcase_18 AC 115 ms
53,228 KB
testcase_19 AC 79 ms
52,700 KB
testcase_20 AC 118 ms
52,984 KB
testcase_21 AC 82 ms
52,724 KB
testcase_22 AC 48 ms
49,396 KB
testcase_23 AC 241 ms
57,676 KB
testcase_24 AC 238 ms
58,076 KB
testcase_25 AC 234 ms
57,528 KB
testcase_26 AC 239 ms
58,280 KB
testcase_27 AC 236 ms
58,012 KB
testcase_28 AC 239 ms
58,000 KB
testcase_29 AC 241 ms
58,408 KB
testcase_30 AC 242 ms
55,948 KB
testcase_31 AC 227 ms
57,928 KB
testcase_32 AC 234 ms
57,712 KB
testcase_33 AC 517 ms
126,872 KB
testcase_34 AC 515 ms
127,248 KB
testcase_35 AC 508 ms
127,204 KB
testcase_36 AC 508 ms
126,944 KB
testcase_37 AC 510 ms
127,312 KB
testcase_38 AC 483 ms
126,616 KB
testcase_39 AC 497 ms
126,424 KB
testcase_40 AC 479 ms
126,496 KB
testcase_41 AC 490 ms
126,248 KB
testcase_42 AC 510 ms
126,400 KB
testcase_43 AC 487 ms
126,496 KB
testcase_44 AC 494 ms
126,260 KB
testcase_45 AC 494 ms
126,732 KB
testcase_46 AC 492 ms
126,240 KB
testcase_47 AC 499 ms
126,416 KB
testcase_48 AC 45 ms
49,568 KB
testcase_49 AC 43 ms
49,356 KB
testcase_50 AC 489 ms
126,508 KB
testcase_51 AC 491 ms
126,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import static java.util.Arrays.*;

public class _469 {
	private static final int mod = (int)1e9+7;

	final Random random = new Random(0);
	final IOFast io = new IOFast();

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int n = io.nextInt();
			int q = io.nextInt();

			Seg2 seg = new Seg2(n);
			TreeMap<Long, Integer> mp = new TreeMap<>();
			mp.put(0L, 0);
			
			for (int i = 0; i < q; i++) {
				char c = io.nextChar();
				if (c == '!') {
					int l = io.nextInt();
					int r = io.nextInt();
					int v = io.nextInt();
					seg.update(l, r, v);
					if (!mp.containsKey(seg.get())) {
						mp.put(seg.get(), i + 1);
					}
				} else {
					io.out.println(mp.get(seg.get()));
				}
			}
		}
	}
	
	static
	class Seg2 {
		final int n;
		final long[] zob;
		final long[] hash;
		final long[] seg;
		
		public Seg2(final int n) {
			this.n = Integer.highestOneBit(n) << 1;
			zob = new long[this.n << 1];
			seg = new long[this.n << 1];
			hash = new long[this.n << 1];
			
			Random random = new Random();
			for (int i = 0; i < this.n; i++) {
				zob[i + this.n - 1] = random.nextLong();
			}
			for (int i = this.n - 2; i >= 0; i--) {
				zob[i] = zob[2*i+1] + zob[2*i+2];
			}
		}
		
		long get() { return hash[0]; }

		void update(int l, int r, int v) { update(l, r, v, 0, 0, n); }
		void update(int l, int r, int v, int k, int curL, int curR) {
			if(curR <= l || curL >= r) return;
			if(l <= curL && curR <= r) { seg[k] += v; hash[k] += v * zob[k]; return; }
			final int curM = (curL + curR) / 2;
			update(l, r, v, 2 * k + 1, curL, curM);
			update(l, r, v, 2 * k + 2, curM, curR);
			hash[k] = hash[2*k+1] + hash[2*k+2] + seg[k] * zob[k];
		}
	}
	
	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	
	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
		io.out.flush();
	}
	public static void main(String[] args) throws IOException { new _469().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private InputStream in = System.in;
		private PrintWriter out = new PrintWriter(System.out);

		private static int pos, readLen;
		private static final byte[] buffer = new byte[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public byte read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException {
			byte first = (byte)nextChar();
			boolean minus = false;
			int res = 0;
			if (first == '-') {
				minus = true;
			} else {
				res = first - '0';
			}
			while (true) {
				byte b = read();
				if (isSpace[b]) break;
				res = res * 10 + b - '0';
			}
			return minus ? -res : res;
		}
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
//		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public double nextDouble() throws IOException {
			double res = nextChar() - '0';
			while (true) {
				byte c = read();
				if (isSpace[c]) return res;
				if (c == '.') break;
				res = res * 10 + c - '0';
			}
			double d = 1;
			while (true) {
				byte c = read();
//				dump(c);
				if (!isDigit[c]) break;
				res = res * 10 + c - '0';
				d *= 10;
			}
//			dump(1);
			return res / d;
		}
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}
0