結果

問題 No.619 CardShuffle
ユーザー 37zigen37zigen
提出日時 2017-12-12 03:39:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,813 ms / 3,000 ms
コード長 5,573 bytes
コンパイル時間 2,372 ms
コンパイル使用メモリ 79,140 KB
実行使用メモリ 118,132 KB
最終ジャッジ日時 2023-08-24 13:52:40
合計ジャッジ時間 41,763 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,376 KB
testcase_01 AC 51 ms
49,516 KB
testcase_02 AC 58 ms
51,740 KB
testcase_03 AC 55 ms
50,456 KB
testcase_04 AC 50 ms
49,548 KB
testcase_05 AC 50 ms
49,464 KB
testcase_06 AC 58 ms
50,460 KB
testcase_07 AC 58 ms
51,852 KB
testcase_08 AC 56 ms
50,396 KB
testcase_09 AC 58 ms
50,264 KB
testcase_10 AC 48 ms
49,492 KB
testcase_11 AC 46 ms
49,408 KB
testcase_12 AC 48 ms
49,524 KB
testcase_13 AC 49 ms
49,328 KB
testcase_14 AC 51 ms
49,360 KB
testcase_15 AC 48 ms
49,480 KB
testcase_16 AC 2,514 ms
118,132 KB
testcase_17 AC 2,035 ms
113,072 KB
testcase_18 AC 2,322 ms
118,092 KB
testcase_19 AC 1,982 ms
113,888 KB
testcase_20 AC 1,660 ms
113,576 KB
testcase_21 AC 2,813 ms
115,808 KB
testcase_22 AC 2,371 ms
114,936 KB
testcase_23 AC 2,795 ms
116,064 KB
testcase_24 AC 2,317 ms
113,120 KB
testcase_25 AC 1,923 ms
115,488 KB
testcase_26 AC 1,491 ms
108,932 KB
testcase_27 AC 1,334 ms
107,368 KB
testcase_28 AC 1,514 ms
108,764 KB
testcase_29 AC 1,355 ms
109,376 KB
testcase_30 AC 1,214 ms
107,316 KB
testcase_31 AC 41 ms
49,256 KB
testcase_32 AC 1,665 ms
113,272 KB
testcase_33 AC 1,783 ms
115,384 KB
testcase_34 AC 1,515 ms
112,376 KB
testcase_35 AC 481 ms
60,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.NoSuchElementException;

class Main {
	public static void main(String[] args) {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}

	final long MOD = 1_000_000_000 + 7;

	void run() {
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		n = (n + 1) / 2;
		SegTree seg = new SegTree(n);
		for (int i = 0; i < n; ++i) {
			long v = sc.nextLong();
			int j = i + seg.n - 1;
			String leftop = "!", rightop = "!";
			if (i != n - 1)
				rightop = sc.next();
			if (i != 0)
				leftop = seg.val[j - 1].right_op;
			seg.val[j].dq.add(v);
			seg.val[j].left_op = leftop;
			seg.val[j].right_op = rightop;
		}

		for (int i = seg.n - 2; i >= 0; --i) {
			seg.val[i] = merge(seg.val[2 * i + 1], seg.val[2 * i + 2]);
		}
		int Q = sc.nextInt();
		while (Q-- > 0) {
			String T = sc.next();
			int x = sc.nextInt();
			int y = sc.nextInt();
			if (T.equals("?")) {
				pw.println(seg.query((x - 1) / 2, (y - 1) / 2 + 1));
			} else {
				if (x % 2 == 1) {
					x = (x - 1) / 2;
					y = (y - 1) / 2;
					seg.swapVal(x, y);
				} else {
					x = x / 2 - 1;
					y = y / 2 - 1;
					seg.swapOp(x, y);
				}
			}
		}
		pw.close();

	}

	class SegTree {
		int n;
		V[] val;

		public SegTree(int n_) {
			n = 1;
			while (n < n_) {
				n *= 2;
			}
			val = new V[2 * n - 1];
			for (int i = 0; i < val.length; ++i) {
				val[i] = new V();
			}
		}

		void swapOp(int i, int j) {
			i += n - 1;
			j += n - 1;
			String opI = val[i].right_op;
			String opJ = val[j].right_op;
			val[i].right_op = opJ;
			val[i + 1].left_op = opJ;
			val[j].right_op = opI;
			val[j + 1].left_op = opI;
			int[] a = new int[] { i, i + 1, j, j + 1 };
			while (a[0] > 0) {
				for (int k = 0; k < a.length; ++k) {
					a[k] = (a[k] - 1) / 2;
					val[a[k]] = merge(val[2 * a[k] + 1], val[2 * a[k] + 2]);
				}
			}
		}

		void swapVal(int i, int j) {
			long x = val[i + n - 1].dq.peek();
			long y = val[j + n - 1].dq.peek();
			setVal(i, y);
			setVal(j, x);
		}

		void setVal(int k, long v) {
			k += n - 1;
			val[k].dq.clear();
			val[k].dq.add(v);
			while (k > 0) {
				k = (k - 1) / 2;
				val[k] = merge(val[2 * k + 1], val[2 * k + 2]);
			}

		}

		long query(int a, int b) {
			long ret = 0;
			V pnd = query(a, b, 0, n, 0);
			for (long v : pnd.dq) {
				ret = ret + v;
				if (ret >= MOD)
					ret -= MOD;
			}
			return ret;
		}

		V query(int a, int b, int l, int r, int k) {
			if (b <= l || r <= a) {
				return new V();
			} else if (a <= l && r <= b) {
				return val[k];
			} else {
				V vl = query(a, b, l, (l + r) / 2, 2 * k + 1);
				V vr = query(a, b, (l + r) / 2, r, 2 * k + 2);
				return merge(vl, vr);
			}
		}
	}

	V merge(V u, V v) {
		V ret = new V();
		for (long n : u.dq) {
			ret.dq.addLast(n);
		}
		boolean f = false;
		if (u.right_op.equals("*") && v.left_op.equals("*")) {
			ret.dq.pollLast();
			ret.dq.addLast(u.dq.peekLast() * v.dq.peekFirst() % MOD);
			f = true;
		}
		for (long n : v.dq) {
			if (!f)
				ret.dq.addLast(n);
			else
				f = false;
		}
		if (ret.dq.size() > 3) {
			long tmp1 = ret.dq.pollFirst();
			while (ret.dq.size() > 3) {
				long tmp2 = ret.dq.pollFirst() + ret.dq.pollFirst();
				if (tmp2 >= MOD)
					tmp2 -= MOD;
				ret.dq.addFirst(tmp2);
			}
			ret.dq.addFirst(tmp1);
		}
		ret.left_op = u.left_op;
		ret.right_op = v.right_op;
		return ret;
	}

	class V {
		Deque<Long> dq = new ArrayDeque<>();
		String left_op = "!";
		String right_op = "!";
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		public boolean hasNext() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			long nl = nextLong();
			if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
				throw new NumberFormatException();
			return (int) nl;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}
0