結果

問題 No.404 部分門松列
ユーザー nebukuro09nebukuro09
提出日時 2017-03-24 12:00:48
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 1,546 ms / 2,000 ms
コード長 3,120 bytes
コンパイル時間 1,349 ms
コンパイル使用メモリ 115,144 KB
実行使用メモリ 51,012 KB
最終ジャッジ日時 2023-09-03 12:37:35
合計ジャッジ時間 18,978 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 535 ms
24,088 KB
testcase_14 AC 209 ms
14,004 KB
testcase_15 AC 388 ms
6,980 KB
testcase_16 AC 819 ms
30,228 KB
testcase_17 AC 868 ms
29,848 KB
testcase_18 AC 297 ms
7,216 KB
testcase_19 AC 328 ms
22,204 KB
testcase_20 AC 294 ms
12,608 KB
testcase_21 AC 919 ms
30,536 KB
testcase_22 AC 349 ms
14,464 KB
testcase_23 AC 365 ms
13,984 KB
testcase_24 AC 843 ms
12,636 KB
testcase_25 AC 1,546 ms
51,012 KB
testcase_26 AC 1,345 ms
32,488 KB
testcase_27 AC 134 ms
4,400 KB
testcase_28 AC 574 ms
23,524 KB
testcase_29 AC 1,109 ms
33,416 KB
testcase_30 AC 477 ms
7,080 KB
testcase_31 AC 69 ms
4,380 KB
testcase_32 AC 936 ms
35,900 KB
testcase_33 AC 798 ms
28,496 KB
testcase_34 AC 597 ms
14,144 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 N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto B = A.dup.sort().uniq.array;

    int[int] compressor;
    foreach (i, e; enumerate(B)) {
        compressor[e] = i.to!int;
    }
    foreach (i; 0..N) {
        A[i] = compressor[A[i]];
    }
    auto M = B.length.to!int - 1;

    auto st_left = new SegmentTree(M+1);
    auto st_right = new SegmentTree(M+1);
    auto st_same = new SegmentTree(M+1);
    auto st_ans = new SegmentTree(M+1);

    foreach (i; 1..N) {
        st_right.add(A[i], 1);
    }

    foreach (i; 1..N-1) {
        st_right.add(A[i], -1);
        st_left.add(A[i-1], 1);
        auto same = st_same.sum(A[i-1], A[i-1]);
        st_same.add(A[i-1], -same);
        st_same.add(A[i-1], st_left.sum(A[i-1], A[i-1]) * st_right.sum(A[i-1], A[i-1]));
        
        long ans = 0;
        if (A[i] > 0) {
            ans += st_left.sum(0, A[i]-1) * st_right.sum(0, A[i]-1) - st_same.sum(0, A[i]-1);
        }
        if (A[i] < M) {
            ans += st_left.sum(A[i]+1, M) * st_right.sum(A[i]+1, M) - st_same.sum(A[i]+1, M);
        }
        st_ans.add(A[i], ans);
    }
    
    auto Q = readln.chomp.to!int;
    foreach (q; 0..Q) {
        auto s = readln.split.map!(to!int);
        auto L = s[0];
        auto R = s[1];

        int hi, lo;
        hi = M+1;
        lo = -1;
        while (hi - lo > 1) {
            int mid = (hi + lo) / 2;
            if (B[mid] >= L) hi = mid;
            else lo = mid;
        }
        L = hi;
        
        hi = M+1;
        lo = -1;
        while (hi - lo > 1) {
            int mid = (hi + lo) / 2;
            if (B[mid] <= R) lo = mid;
            else hi = mid;
        }
        R = lo;

        if (L > M || R < 0) {
            0.writeln;
            continue;
        }
        L = max(0, L);
        R = min(M, R);
        st_ans.sum(L, R).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