結果

問題 No.404 部分門松列
ユーザー nebukuro09
提出日時 2017-03-24 12:00:48
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,399 ms / 2,000 ms
コード長 3,120 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 129,556 KB
実行使用メモリ 49,340 KB
最終ジャッジ日時 2024-06-12 18:32:13
合計ジャッジ時間 17,466 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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