結果
| 問題 |
No.151 セグメントフィッシング
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-02-12 23:46:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 181 ms / 5,000 ms |
| コード長 | 2,984 bytes |
| コンパイル時間 | 3,840 ms |
| コンパイル使用メモリ | 80,340 KB |
| 実行使用メモリ | 53,064 KB |
| 最終ジャッジ日時 | 2024-06-23 19:25:07 |
| 合計ジャッジ時間 | 7,448 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 19 |
ソースコード
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)); }
}