結果

問題 No.924 紲星
ユーザー akakimidoriakakimidori
提出日時 2020-02-08 01:22:54
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3,248 ms / 4,000 ms
コード長 3,022 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 32,520 KB
実行使用メモリ 181,816 KB
最終ジャッジ日時 2023-10-26 00:32:03
合計ジャッジ時間 26,246 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3,145 ms
181,816 KB
testcase_09 AC 3,035 ms
181,816 KB
testcase_10 AC 3,248 ms
181,816 KB
testcase_11 AC 3,160 ms
181,816 KB
testcase_12 AC 3,095 ms
181,816 KB
testcase_13 AC 1,118 ms
62,104 KB
testcase_14 AC 1,035 ms
38,472 KB
testcase_15 AC 981 ms
49,212 KB
testcase_16 AC 1,041 ms
155,572 KB
testcase_17 AC 1,742 ms
63,212 KB
testcase_18 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int32_t i32;
typedef int64_t i64;

#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))
#define SORT(a,num,cmp) qsort((a),(num),sizeof(*(a)),cmp)

typedef struct node {
    i64 sum;
    i64 cnt;
    const struct node *left;
    const struct node *right;
} node;

void eval(const node *t, i64 *sum, i64 *cnt) {
    if (t != NULL) {
        *sum += t->sum;
        *cnt += t->cnt;
    }
}

node* new_node(const node *l, const node *r) {
    node *t = ALLOC (1, node);
    eval (l, &t->sum, &t->cnt);
    eval (r, &t->sum, &t->cnt);
    t->left = l;
    t->right = r;
    return t;
}

const node* update(const node *t, i32 l, i32 r, i32 x, i64 v) {
    if (l + 1 == r) {
        node *t = new_node(NULL, NULL);
        t->sum = v;
        t->cnt = 1;
        return t;
    }
    const node *left = NULL;
    const node *right = NULL;
    if (t != NULL) {
        left = t->left;
        right = t->right;
    }
    i64 m = (l + r) / 2;
    if (x < m) {
        left = update (left, l, m, x, v);
    } else {
        right = update (right, m, r, x, v);
    }
    return new_node (left, right);
}

void find (const node *t, i32 l, i32 r, i32 x, i32 y, i64 *sum, i64 *cnt) {
    if (t == NULL || r <= x || y <= l) {
        return;
    }
    if (x <= l && r <= y) {
        eval (t, sum, cnt);
        return;
    }
    i64 m = (l + r) / 2;
    find (t->left, l, m, x, y, sum, cnt);
    find (t->right, m, r, x, y, sum, cnt);
}

int cmp_int (const void *a, const void *b) {
    i64 p = *(const i64 *)a;
    i64 q = *(const i64 *)b;
    return p == q ? 0 : p <= q ? -1 : 1;
}

void run (void) {
    i32 n, q;
    scanf ("%" SCNi32 "%" SCNi32, &n, &q);
    i64 *a = ALLOC (n, i64);
    i64 *s = ALLOC (n + 1, i64);
    for (i32 i = 0; i < n; ++i) {
        i64 b;
        scanf ("%" SCNi64, &b);
        a[i] = (b << 18) + i;
        s[i] = b;
    }
    for (i32 i = n - 1; i >= 0; --i) {
        s[i] += s[i + 1];
    }
    SORT (a, n, cmp_int);
    const node **seg = ALLOC (n + 1, const node *);
    for (i32 i = 0; i < n; ++i) {
        i64 v = a[i] >> 18;
        i32 k = a[i] & ((1 << 18) - 1);
        seg[i + 1] = update (seg[i], 0, n, k, v);
    }
    for (i32 i = 0; i < q; ++i) {
        i32 l, r;
        scanf ("%" SCNi32 "%" SCNi32, &l, &r);
        l -= 1;
        i32 ng = 0;
        i32 ok = n;
        while (ok - ng > 1) {
            i32 mid = (ok + ng) / 2;
            i64 sum = 0;
            i64 cnt = 0;
            find (seg[mid], 0, n, l, r, &sum, &cnt);
            if (2 * cnt >= r - l) {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        i64 sum = 0;
        i64 cnt = 0;
        find (seg[ok], 0, n, l, r, &sum, &cnt);
        i64 ans = 0;
        ans += s[l] - s[r] - sum - (r - l - cnt) * (a[ok - 1] >> 18);
        ans += cnt * (a[ok - 1] >> 18) - sum;
        printf ("%" PRIi64 "\n", ans);
    }
}

int main (void) {
    run();
    return 0;
}
0