結果
問題 | No.924 紲星 |
ユーザー | akakimidori |
提出日時 | 2020-02-08 01:26:12 |
言語 | C (gcc 12.3.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,111 bytes |
コンパイル時間 | 516 ms |
コンパイル使用メモリ | 32,128 KB |
実行使用メモリ | 118,992 KB |
最終ジャッジ日時 | 2024-09-25 08:31:39 |
合計ジャッジ時間 | 14,588 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | AC | 722 ms
42,452 KB |
testcase_14 | AC | 676 ms
27,576 KB |
testcase_15 | AC | 638 ms
33,940 KB |
testcase_16 | AC | 534 ms
105,704 KB |
testcase_17 | AC | 1,162 ms
43,284 KB |
testcase_18 | AC | 1 ms
6,940 KB |
ソースコード
#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) { static node *BUF = NULL; if (BUF == NULL) { BUF = ALLOC (200000 * 18, node); } node *t = BUF++; 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; }