結果

問題 No.151 セグメントフィッシング
ユーザー uwiuwi
提出日時 2015-02-12 23:46:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 2,984 bytes
コンパイル時間 4,229 ms
コンパイル使用メモリ 76,984 KB
実行使用メモリ 53,592 KB
最終ジャッジ日時 2023-09-06 00:17:54
合計ジャッジ時間 7,971 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
50,640 KB
testcase_01 AC 67 ms
50,568 KB
testcase_02 AC 67 ms
50,576 KB
testcase_03 AC 71 ms
50,692 KB
testcase_04 AC 66 ms
50,576 KB
testcase_05 AC 69 ms
50,476 KB
testcase_06 AC 71 ms
50,724 KB
testcase_07 AC 67 ms
50,584 KB
testcase_08 AC 106 ms
53,032 KB
testcase_09 AC 105 ms
51,020 KB
testcase_10 AC 103 ms
52,956 KB
testcase_11 AC 104 ms
53,112 KB
testcase_12 AC 156 ms
53,344 KB
testcase_13 AC 133 ms
52,536 KB
testcase_14 AC 146 ms
53,252 KB
testcase_15 AC 154 ms
53,288 KB
testcase_16 AC 135 ms
53,052 KB
testcase_17 AC 94 ms
52,764 KB
testcase_18 AC 94 ms
52,776 KB
testcase_19 AC 170 ms
53,592 KB
testcase_20 AC 170 ms
53,200 KB
testcase_21 AC 109 ms
52,816 KB
testcase_22 AC 106 ms
52,824 KB
testcase_23 AC 125 ms
53,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package q3xx;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Q383 {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	static void solve()
	{
		int n = ni(), Q = ni();
		long[] ft = new long[2*n+1];
		long all = 0;
		for(int i = 0;i < Q;i++){
			char x = nc();
			int y = ni(), z = ni();
			if(x == 'R'){
				int pos = (y-i)%(2*n);
				if(pos < 0)pos+=2*n;
				addFenwick(ft, pos, z);
				all += z;
			}else if(x == 'L'){
				int pos = (n+(n-1-y)-i)%(2*n);
				if(pos < 0)pos+=2*n;
				addFenwick(ft, pos, z);
				all += z;
			}else if(x == 'C'){
				long ret = 0;
				// R
				{
					int l = (y-i)%(2*n);
					if(l < 0)l+=2*n;
					int r = (z-1-i)%(2*n);
					if(r < 0)r+=2*n;
					if(l <= r){
						ret += sumFenwick(ft, r) - sumFenwick(ft, l-1);
					}else{
						ret += sumFenwick(ft, r) + all - sumFenwick(ft, l-1);
					}
				}
				// L
				{
					int l = (n+(n-1-(z-1))-i)%(2*n);
					if(l < 0)l+=2*n;
					int r = (n+(n-1-y)-i)%(2*n);
					if(r < 0)r+=2*n;
					if(l <= r){
						ret += sumFenwick(ft, r) - sumFenwick(ft, l-1);
					}else{
						ret += sumFenwick(ft, r) + all - sumFenwick(ft, l-1);
					}
				}
				out.println(ret);
			}
		}
	}
	
	public static long sumFenwick(long[] ft, int i)
	{
		long sum = 0;
		for(i++;i > 0;i -= i&-i)sum += ft[i];
		return sum;
	}
	
	public static void addFenwick(long[] ft, int i, long v)
	{
		if(v == 0)return;
		int n = ft.length;
		for(i++;i < n;i += i&-i)ft[i] += v;
	}
	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static char nc() { return (char)skip(); }
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
0