結果

問題 No.318 学学学学学
ユーザー akakimidori
提出日時 2019-05-26 20:24:41
言語 C
(gcc 8.3.0)
結果
AC  
実行時間 49 ms
コード長 1,981 Byte
コンパイル時間 121 ms
使用メモリ 2,272 KB
最終ジャッジ日時 2019-08-12 22:45:47

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
99_system_test1.txt AC 5 ms
964 KB
99_system_test2.txt AC 10 ms
972 KB
99_system_test3.txt AC 12 ms
1,028 KB
99_system_test4.txt AC 9 ms
968 KB
99_system_test5.txt AC 11 ms
984 KB
eq_01.txt AC 47 ms
2,264 KB
eq_02.txt AC 49 ms
2,264 KB
eq_03.txt AC 45 ms
2,272 KB
eq_04.txt AC 43 ms
2,268 KB
eq_05.txt AC 41 ms
2,260 KB
eq_06.txt AC 37 ms
1,952 KB
eq_07.txt AC 48 ms
2,272 KB
eq_08.txt AC 45 ms
2,268 KB
eq_09.txt AC 41 ms
2,264 KB
eq_10.txt AC 40 ms
2,272 KB
eq_11.txt AC 39 ms
2,272 KB
eq_12.txt AC 34 ms
2,264 KB
gaaa_01.txt AC 34 ms
2,268 KB
gaaa_02.txt AC 32 ms
2,268 KB
gaaa_03.txt AC 31 ms
2,272 KB
gaaa_04.txt AC 32 ms
2,264 KB
handmade_01.txt AC 1 ms
968 KB
handmade_02.txt AC 1 ms
968 KB
handmade_03.txt AC 1 ms
964 KB
handmade_04.txt AC 1 ms
964 KB
handmade_05.txt AC 1 ms
964 KB
sample_01.txt AC 1 ms
972 KB
sample_02.txt AC 1 ms
968 KB
sample_03.txt AC 1 ms
968 KB
テストケース一括ダウンロード

ソースコード

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

typedef int32_t i32;

typedef i32 RUQ_val;

typedef struct range_update_query {
  RUQ_val *val;
  int32_t size;
  int32_t bit;
  RUQ_val empty;
} RUQ;

RUQ* new_RUQ (const int32_t n) {
  int32_t k = 0;
  while ((1 << k) < n) ++k;
  RUQ *s = (RUQ *) calloc (1, sizeof (RUQ));
  s->val = (RUQ_val *) calloc (2 << k, sizeof (RUQ_val));
  s->size = 1 << k;
  s->bit = k;
  s->empty = -1;
  return s;
}

static inline void propagate (RUQ *s, int32_t k) {
  if (s->val[k] == s->empty) return;
  s->val[2 * k] = s->val[2 * k + 1] = s->val[k];
  s->val[k] = s->empty;
}

void update (RUQ *s, int32_t l, int32_t r, RUQ_val v) {
  for (int32_t i = s->bit; i > 0; --i) {
    propagate (s, (l + s->size) >> i);
    propagate (s, (r - 1 + s->size) >> i);
  }
  for (l += s->size, r += s->size; l < r; l >>= 1, r >>= 1) {
    if (l & 1) s->val[l++] = v;
    if (r & 1) s->val[--r] = v;
  }
}

RUQ_val find (RUQ *s, int32_t x) {
  x += s->size;
  for (int32_t i = s->bit; i >= 0; --i) {
    if (s->val[x >> i] != s->empty) {
      return s->val[x >> i];
    }
  }
  return s->empty;
}

typedef struct node {
  i32 v;
  i32 k;
} node;

int cmp_node (const void *a, const void *b) {
  i32 d = ((node *)a)->v - ((node *)b)->v;
  if (d != 0) return d < 0 ? -1 : 1;
  d = ((node *)a)->k - ((node *)b)->k;
  return d == 0 ? 0 : d < 0 ? -1 : 1;
}

void run (void) {
  i32 n;
  scanf ("%" SCNi32, &n);
  node *p = (node *) calloc (n, sizeof (node));
  for (i32 i = 0; i < n; ++i) {
    i32 a;
    scanf ("%" SCNi32, &a);
    p[i] = (node) {a, i};
  }
  qsort (p, n, sizeof (node), cmp_node);
  RUQ *s = new_RUQ (n);
  for (i32 i = 0; i < n;) {
    i32 j = i + 1;
    while (j < n && p[i].v == p[j].v) ++j;
    update (s, p[i].k, p[j - 1].k + 1, p[i].v);
    i = j;
  }
  for (i32 i = 0; i < n; ++i) {
    printf ("%" PRIi32, find (s, i));
    putchar (i == n - 1 ? '\n' : ' ');
  }
}

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