結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー 👑 chro_96chro_96
提出日時 2024-02-23 22:26:52
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 135 ms / 2,500 ms
コード長 2,225 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 15,760 KB
最終ジャッジ日時 2024-02-23 22:27:15
合計ジャッジ時間 6,194 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,432 KB
testcase_01 AC 5 ms
12,432 KB
testcase_02 AC 36 ms
13,072 KB
testcase_03 AC 20 ms
12,688 KB
testcase_04 AC 93 ms
14,864 KB
testcase_05 AC 70 ms
14,224 KB
testcase_06 AC 36 ms
13,200 KB
testcase_07 AC 77 ms
14,352 KB
testcase_08 AC 28 ms
12,944 KB
testcase_09 AC 130 ms
15,760 KB
testcase_10 AC 135 ms
15,760 KB
testcase_11 AC 130 ms
15,760 KB
testcase_12 AC 132 ms
15,760 KB
testcase_13 AC 130 ms
15,760 KB
testcase_14 AC 130 ms
15,760 KB
testcase_15 AC 129 ms
15,760 KB
testcase_16 AC 122 ms
15,760 KB
testcase_17 AC 125 ms
15,760 KB
testcase_18 AC 122 ms
15,760 KB
testcase_19 AC 123 ms
15,760 KB
testcase_20 AC 121 ms
15,760 KB
testcase_21 AC 122 ms
15,760 KB
testcase_22 AC 122 ms
15,760 KB
testcase_23 AC 99 ms
15,760 KB
testcase_24 AC 99 ms
15,760 KB
testcase_25 AC 102 ms
15,760 KB
testcase_26 AC 99 ms
15,760 KB
testcase_27 AC 100 ms
15,760 KB
testcase_28 AC 99 ms
15,760 KB
testcase_29 AC 99 ms
15,760 KB
testcase_30 AC 104 ms
15,760 KB
testcase_31 AC 105 ms
15,760 KB
testcase_32 AC 61 ms
13,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef struct tree {
  int l;
  int r;
  struct tree *p;
} tree;

int cmp_int (const void *ap, const void *bp) {
  int a = *(int *)ap;
  int b = *(int *)bp;
  
  if (a < b) {
    return -1;
  }
  
  if (a > b) {
    return 1;
  }
  
  return 0;
}

tree *get_root (tree *t) {
  if (t == NULL || t->p == NULL) {
    return t;
  }
  t->p = get_root(t->p);
  return t->p;
}

int main () {
  int n = 0;
  int a = 0;
  int x[100000] = {};
  int t = 0;
  int l[100000] = {};
  int r[100000] = {};
  
  int res = 0;
  
  int ans[300000] = {};
  tree tr[300000] = {};
  int sorted[300000][3] = {};
  int ucnt = 0;
  
  res = scanf("%d", &n);
  res = scanf("%d", &a);
  for (int i = 0; i < n; i++) {
    res = scanf("%d", x+i);
    sorted[i][0] = x[i];
    sorted[i][1] = i;
    sorted[i][2] = 0;
  }
  res = scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    res = scanf("%d", l+i);
    res = scanf("%d", r+i);
    sorted[n+2*i][0] = l[i];
    sorted[n+2*i][1] = i;
    sorted[n+2*i][2] = 1;
    sorted[n+2*i+1][0] = r[i];
    sorted[n+2*i+1][1] = i;
    sorted[n+2*i+1][2] = 2;
  }
  
  qsort(sorted, n+2*t, sizeof(int)*3, cmp_int);
  
  ucnt = 1;
  if (sorted[0][2] == 0) {
    x[sorted[0][1]] = 0;
  } else if (sorted[0][2] == 1) {
    l[sorted[0][1]] = 0;
  } else {
    r[sorted[0][1]] = 0;
  }
  
  for (int i = 1; i < n+2*t; i++) {
    if (sorted[i][0] != sorted[i-1][0]) {
      sorted[ucnt][0] = sorted[i][0];
      ucnt++;
    }
    if (sorted[i][2] == 0) {
      x[sorted[i][1]] = ucnt-1;
    } else if (sorted[i][2] == 1) {
      l[sorted[i][1]] = ucnt-1;
    } else {
      r[sorted[i][1]] = ucnt-1;
    }
  }
  
  for (int i = 0; i < ucnt; i++) {
    tr[i].l = i;
    tr[i].r = i;
    tr[i].p = NULL;
    ans[i] = -1;
  }
  
  for (int i = t; i > 0; i--) {
    int idx = l[i-1];
    tree *rt = NULL;
    while (idx <= r[i-1]) {
      tree *tmp_rt = get_root(tr+idx);
      if (ans[idx] < 0) {
        ans[idx] = i;
      }
      if (rt == NULL) {
        rt = tmp_rt;
      } else if (rt != tmp_rt) {
        tmp_rt->p = rt;
        rt->r = tmp_rt->r;
      }
      idx = rt->r+1;
    }
  }
  
  for (int i = 0; i < n; i++) {
    printf("%d\n", ans[x[i]]);
  }
  
  return 0;
}
0