結果

問題 No.151 セグメントフィッシング
ユーザー nebukuro09nebukuro09
提出日時 2017-03-11 10:50:56
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 2,365 bytes
コンパイル時間 576 ms
コンパイル使用メモリ 100,900 KB
実行使用メモリ 6,480 KB
最終ジャッジ日時 2023-09-03 12:17:34
合計ジャッジ時間 2,112 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 28 ms
6,164 KB
testcase_13 AC 26 ms
4,700 KB
testcase_14 AC 27 ms
4,636 KB
testcase_15 AC 27 ms
6,480 KB
testcase_16 AC 27 ms
4,660 KB
testcase_17 AC 18 ms
4,648 KB
testcase_18 AC 17 ms
4,644 KB
testcase_19 AC 40 ms
4,708 KB
testcase_20 AC 39 ms
5,952 KB
testcase_21 AC 20 ms
4,704 KB
testcase_22 AC 20 ms
4,648 KB
testcase_23 AC 20 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto s = readln.split;
    auto N = s[0].to!int;
    auto Q = s[1].to!int;

    int M = 2 * N;
    auto st = new SegmentTree(2*N);
    int sym(int n) {return 2*N-n-1;}
    int rot(int n, int t) {
        int pos = (n - t) % M;
        pos = (pos + M) % M;
        return pos;
    }

    foreach (q; 1..Q+1) {
        s = readln.split;
        auto x = s[0];
        auto y = s[1].to!int;
        auto z = s[2].to!int;
        if (x == "L") {
            int pos = rot(sym(y), q);
            st.add(pos, z);
        }
        else if (x == "R") {
            int pos = rot(y, q);
            st.add(pos, z);
        }
        else {
            int l1 = rot(y, q);
            int r1 = rot(z-1,q);
            int l2 = rot(sym(y), q);
            int r2 = rot(sym(z-1), q);
            long ans = 0;
            if (l1 <= r1)
                ans += st.sum(l1, r1);
            else 
                ans += st.sum(l1, M-1) + st.sum(0, r1);
            if (r2 <= l2)
                ans += st.sum(r2, l2);
            else
                ans += st.sum(r2, M-1) + st.sum(0, l2);
            ans.writeln;
        }
    }
}

class SegmentTree {
    long[] table;
    int size;

    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new long[](size);
        fill(table, 0);
    }

    void add(int pos, long num) {
        return add(pos, num, 0, 0, size/2-1);
    }

    void add(int pos, long num, int i, int left, int right) {
        table[i] += num;
        if (left == right)
            return;
        auto mid = (left + right) / 2;
        if (pos <= mid)
            add(pos, num, i*2+1, left, mid);
        else
            add(pos, num, i*2+2, mid+1, right);
    }

    long sum(int pl, int pr) {
        return sum(pl, pr, 0, 0, size/2-1);
    }

    long sum(int pl, int pr, int i, int left, int right) {
        if (pl > right || pr < left)
            return 0;
        else if (pl <= left && right <= pr)
            return table[i];
        else
            return
                sum(pl, pr, i*2+1, left, (left+right)/2) +
                sum(pl, pr, i*2+2, (left+right)/2+1, right);
    }
}
0