結果

問題 No.259 セグメントフィッシング+
ユーザー koba-e964koba-e964
提出日時 2016-07-01 00:49:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 3,668 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 154,496 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-20 05:01:10
合計ジャッジ時間 5,345 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 0 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 0 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 130 ms
8,192 KB
testcase_09 AC 130 ms
8,192 KB
testcase_10 AC 139 ms
8,192 KB
testcase_11 AC 134 ms
8,320 KB
testcase_12 AC 128 ms
8,192 KB
testcase_13 AC 146 ms
8,192 KB
testcase_14 AC 144 ms
8,320 KB
testcase_15 AC 148 ms
8,192 KB
testcase_16 AC 146 ms
8,192 KB
testcase_17 AC 150 ms
8,192 KB
testcase_18 AC 156 ms
8,320 KB
testcase_19 AC 144 ms
8,064 KB
testcase_20 AC 138 ms
8,192 KB
testcase_21 AC 146 ms
8,320 KB
testcase_22 AC 142 ms
8,192 KB
testcase_23 AC 68 ms
5,376 KB
testcase_24 AC 100 ms
8,192 KB
testcase_25 AC 97 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }

/**
 * Segment Tree. This data structure is useful for fast folding on intervals of an array
 * whose elements are elements of monoid M. Note that constructing this tree requires the identity
 * element of M and the operation of M.
 * Header requirement: vector
 */
struct SegTree<I, BiOp> {
    n: usize,
    dat: Vec<I>,
    op: BiOp,
    e: I,
}

impl<I, BiOp> SegTree<I, BiOp>
    where BiOp: Fn(I, I) -> I,
          I: Copy {
    pub fn new(n_: usize, op: BiOp, e: I) -> Self {
        let mut n = 1;
        while n < n_ { n *= 2; } // n is a power of 2
        SegTree {n: n, dat: vec![e; 2 * n - 1], op: op, e: e}
    }
    /* ary[k] <- v */
    pub fn update(&mut self, idx: usize, v: I) {
        let mut k = idx + self.n - 1;
        self.dat[k] = v;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]);
        }
    }
    /* l,r are for simplicity */
    fn query_sub(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> I {
        // [a,b) and  [l,r) intersects?
        if r <= a || b <= l { return self.e; }
        if a <= l && r <= b { return self.dat[k]; }
        let vl = self.query_sub(a, b, 2 * k + 1, l, (l + r) / 2);
        let vr = self.query_sub(a, b, 2 * k + 2, (l + r) / 2, r);
        (self.op)(vl, vr)
    }
    /* [a, b] (note: inclusive) */
    pub fn query(&self, a: usize, b: usize) -> I {
        self.query_sub(a, b + 1, 0, 0, self.n)
    }
}

fn tt<BiOp>(st: &mut SegTree<i64, BiOp>, x: i64, y: i64, n: usize) -> i64
    where BiOp: Fn(i64, i64) -> i64 {
    let xmodn = (x % n as i64) as usize;
    let ymodn = (y % n as i64) as usize;
    if x / n as i64 == y / n as i64 {
      return st.query(xmodn, ymodn);
    }
    return st.query(xmodn, n - 1) + st.query(0, ymodn);
}


fn main() {
    let n: usize = get();
    let q: usize = get();
    let mut st = SegTree::<i64, _>::new(2 * n, |x, y| x + y, 0);
    let n2 = 2 * n as i64;
    for _ in 0 .. q {
        let x: String = get_word();
        let t: i64 = get();
        let y: i64 = get();
        let z: i64 = get();
        match x.as_ref() {
            "L" => {
                let pos = ((y + t) % n2) as usize;
                let tmp = st.query(pos, pos);
                st.update(pos, tmp + z);
            },
            "R" => {
                let pos = ((-y + t + n2 - 1) % n2) as usize;
                let tmp = st.query(pos, pos);
                st.update(pos, tmp + z);
            },
            "C" => {
	        let mut q = tt(&mut st, y + t, z + t - 1, 2 * n);
	        q += tt(&mut st, - z + t + n2, -y + t + n2 - 1, 2 * n);
	        println!("{}", q);
            },
            _ => panic!()
        }
    }
}
0