結果

問題 No.1012 荷物収集
ユーザー nanaenanae
提出日時 2020-03-20 22:49:47
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 4,275 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 174,264 KB
実行使用メモリ 14,608 KB
最終ジャッジ日時 2023-09-04 06:22:07
合計ジャッジ時間 11,254 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 137 ms
14,608 KB
testcase_05 AC 137 ms
13,296 KB
testcase_06 AC 137 ms
13,332 KB
testcase_07 AC 137 ms
14,052 KB
testcase_08 AC 138 ms
13,280 KB
testcase_09 AC 138 ms
13,788 KB
testcase_10 AC 137 ms
12,748 KB
testcase_11 AC 137 ms
12,712 KB
testcase_12 AC 137 ms
13,336 KB
testcase_13 AC 138 ms
13,300 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 114 ms
9,244 KB
testcase_25 AC 149 ms
12,092 KB
testcase_26 AC 80 ms
6,984 KB
testcase_27 AC 20 ms
4,560 KB
testcase_28 AC 145 ms
11,084 KB
testcase_29 AC 85 ms
6,604 KB
testcase_30 AC 148 ms
10,244 KB
testcase_31 AC 63 ms
7,252 KB
testcase_32 AC 51 ms
6,692 KB
testcase_33 AC 80 ms
7,636 KB
testcase_34 AC 87 ms
10,016 KB
testcase_35 AC 124 ms
9,080 KB
testcase_36 AC 81 ms
9,804 KB
testcase_37 AC 81 ms
7,468 KB
testcase_38 AC 118 ms
10,292 KB
testcase_39 AC 54 ms
6,584 KB
testcase_40 AC 64 ms
7,036 KB
testcase_41 AC 162 ms
12,136 KB
testcase_42 AC 79 ms
7,828 KB
testcase_43 AC 108 ms
9,756 KB
testcase_44 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;

enum mod = 1_000_000_007L;

struct Mint {
    long x;

    this(long a) {
        x = a % mod;
        if (x < 0) x += mod;
    }

    this(Mint a) {
        x = a.x;
    }

    ref Mint opAssign(long a) {
        this = Mint(a);
        return this;
    }

    ref Mint opOpAssign(string op)(Mint rhs) {
        static if (op == "+") {
            x += rhs.x;
            if (x >= mod) x -= mod;
        }
        static if (op == "-") {
            x -= rhs.x;
            if (x < 0) x += mod;
        }
        static if (op == "*") {
            (x *= rhs.x) %= mod;
        }
        static if (op == "/") {
            this *= rhs.inv();
        }
        return this;
    }

    ref Mint opOpAssign(string op)(long rhs) {
        static if (op == "^^") {
            this = powmod(this, rhs);
            return this;
        }
        else {
            return mixin("this " ~ op ~ "= Mint(rhs)");
        }
    }

    const Mint powmod(Mint a, long b) {
        Mint res = 1, p = a;
        while (b > 0) {
            if (b & 1) res *= p;
            p *= p;
            b /= 2;
        }
        return res;
    }

    const Mint inv() {
        return powmod(this, mod - 2);
    }

    Mint opUnary(string op)() if (s == "-") {
        return Mint(-x);
    }

    Mint opBinary(string op, T)(const T rhs) {
        return mixin("Mint(this) " ~ op ~ "= rhs");
    }

    Mint opBinaryRight(string op)(const long rhs) {
        return mixin("Mint(rhs) " ~ op ~ "= this");
    }

    bool opEquals(Mint a, Mint b) {
        return a.x == b.x;
    }

    bool opEquals(long rhs) {
        long y = rhs % mod;
        if (y < 0) y += mod;
        return x == y;
    }

    string toString() {
        import std.conv : to;
        return x.to!string;
    }
}

unittest {
    long powmod(long a, long b) {
        return b > 0 ? powmod(a, b / 2)^^2 % mod * a^^(b & 1) % mod : 1L;
    }
    auto a = Mint(2), b = Mint(3);
    assert(a + b == 5);
    assert(a + 5 == 7);
    assert(1 + b == 4);
    assert(a - b == mod - 1);
    assert(a * b == 6);
    assert(a / b == 2 * powmod(3, mod - 2));
    assert(a^^10 == 1024);

    Mint z;
    assert(z == 0);

    (z += 2) *= 3;
    assert(z == 6);
}

alias Query = Tuple!(long, "x", int, "idx");


void main() {
    int N, Q;
    scan(N, Q);
    auto x = new long[](N);
    auto w = new long[](N);
    foreach (i ; 0 .. N) {
        scan(x[i], w[i]);
    }

    zip(x, w).sort();

    auto ws = new long[](N + 1);
    foreach (i ; 1 .. N + 1) {
        ws[i] = ws[i-1] + w[i-1];
    }

    auto y = readln.split.to!(long[]);
    auto ys = new Query[](Q);
    foreach (i ; 0 .. Q) {
        ys[i] = Query(y[i], i);
    }
    ys.sort();

    long tot;
    foreach (i ; 0 .. N) {
        tot += x[i] * w[i];
    }

    int les;

    auto ans = new long[](Q);
    long pre = 0;
    foreach (i ; 0 .. Q) {
        auto d = ys[i].x - pre;
        tot += ws[les] * d;
        while (les < N && x[les] <= ys[i].x) {
            tot -= w[les] * (x[les] - pre);
            tot += w[les] * (ys[i].x - x[les]);
            les++;
        }
        tot -= (ws[N] - ws[les]) * d;
        ans[ys[i].idx] = tot;
        pre = ys[i].x;
    }
    writefln("%(%s\n%)", ans);
}



void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

void yes(bool ok, string y = "Yes", string n = "No") {
    return writeln(ok ? y : n);
}
0