結果

問題 No.2809 Sort Query
ユーザー chro_96chro_96
提出日時 2024-07-12 23:25:27
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,227 ms / 2,000 ms
コード長 3,656 bytes
コンパイル時間 1,498 ms
コンパイル使用メモリ 32,436 KB
実行使用メモリ 28,800 KB
最終ジャッジ日時 2024-07-12 23:26:26
合計ジャッジ時間 57,430 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
23,936 KB
testcase_01 AC 927 ms
28,660 KB
testcase_02 AC 937 ms
28,620 KB
testcase_03 AC 877 ms
28,524 KB
testcase_04 AC 886 ms
28,544 KB
testcase_05 AC 918 ms
28,580 KB
testcase_06 AC 592 ms
27,904 KB
testcase_07 AC 571 ms
27,776 KB
testcase_08 AC 598 ms
27,776 KB
testcase_09 AC 584 ms
27,688 KB
testcase_10 AC 591 ms
27,688 KB
testcase_11 AC 328 ms
28,512 KB
testcase_12 AC 296 ms
28,560 KB
testcase_13 AC 304 ms
28,448 KB
testcase_14 AC 312 ms
28,460 KB
testcase_15 AC 304 ms
28,656 KB
testcase_16 AC 305 ms
28,524 KB
testcase_17 AC 306 ms
28,452 KB
testcase_18 AC 320 ms
28,800 KB
testcase_19 AC 299 ms
28,612 KB
testcase_20 AC 298 ms
28,544 KB
testcase_21 AC 1,005 ms
28,544 KB
testcase_22 AC 1,020 ms
28,660 KB
testcase_23 AC 1,018 ms
28,580 KB
testcase_24 AC 1,001 ms
28,480 KB
testcase_25 AC 1,022 ms
28,516 KB
testcase_26 AC 1,097 ms
28,580 KB
testcase_27 AC 1,074 ms
28,672 KB
testcase_28 AC 1,103 ms
28,528 KB
testcase_29 AC 1,096 ms
28,516 KB
testcase_30 AC 1,098 ms
28,472 KB
testcase_31 AC 222 ms
27,520 KB
testcase_32 AC 220 ms
27,520 KB
testcase_33 AC 247 ms
27,352 KB
testcase_34 AC 230 ms
27,644 KB
testcase_35 AC 224 ms
27,392 KB
testcase_36 AC 217 ms
26,440 KB
testcase_37 AC 215 ms
26,496 KB
testcase_38 AC 255 ms
26,240 KB
testcase_39 AC 221 ms
26,992 KB
testcase_40 AC 223 ms
26,120 KB
testcase_41 AC 1,209 ms
28,512 KB
testcase_42 AC 1,227 ms
28,504 KB
testcase_43 AC 1,203 ms
28,604 KB
testcase_44 AC 1,222 ms
28,512 KB
testcase_45 AC 1,209 ms
28,800 KB
testcase_46 AC 1,201 ms
28,528 KB
testcase_47 AC 1,157 ms
28,508 KB
testcase_48 AC 1,170 ms
28,504 KB
testcase_49 AC 1,156 ms
28,552 KB
testcase_50 AC 1,196 ms
28,520 KB
testcase_51 AC 251 ms
28,632 KB
testcase_52 AC 252 ms
28,584 KB
testcase_53 AC 260 ms
28,632 KB
testcase_54 AC 247 ms
28,504 KB
testcase_55 AC 246 ms
28,520 KB
testcase_56 AC 764 ms
27,288 KB
testcase_57 AC 658 ms
26,980 KB
testcase_58 AC 554 ms
26,688 KB
testcase_59 AC 722 ms
26,880 KB
testcase_60 AC 456 ms
26,872 KB
testcase_61 AC 649 ms
27,496 KB
testcase_62 AC 615 ms
26,628 KB
testcase_63 AC 768 ms
27,400 KB
testcase_64 AC 774 ms
28,132 KB
testcase_65 AC 379 ms
26,476 KB
testcase_66 AC 6 ms
24,064 KB
testcase_67 AC 8 ms
24,024 KB
testcase_68 AC 6 ms
24,036 KB
testcase_69 AC 6 ms
24,048 KB
testcase_70 AC 7 ms
23,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

void add_segt (int *t, int idx, int val, int size) {
  idx = idx+size-1;
  t[idx] += val;
  
  while (idx > 0) {
    idx = (idx-1)/2;
    t[idx] += val;
  }
  
  return;
}

int sum_segt_rec (int *t, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  }
  
  if (a <= l && r <= b) {
    return t[k];
  }
  
  return sum_segt_rec(t, a, b, 2*k+1, l, (l+r)/2)+sum_segt_rec(t, a, b, 2*k+2, (l+r)/2, r);
}

int sum_segt (int *t, int a, int b, int size) {
  return sum_segt_rec(t, a, b, 0, 0, size);
}

int main () {
  int n = 0;
  int q = 0;
  long long a[300000] = {};
  int t[300000] = {};
  int k[300000] = {};
  long long x[300000] = {};
  
  int res = 0;
  
  long long val[600000] = {};
  int val_cnt = 1;
  
  int w[300000] = {};
  int prev = 0;
  
  int segt[2097151] = {};
  int size = 1;
  
  int tmp[300000] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &q);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", a+i);
    val[i] = a[i];
  }
  for (int i = 0; i < q; i++) {
    res = scanf("%d", t+i);
    if (t[i] != 2) {
      res = scanf("%d", k+i);
      if (t[i] == 1) {
        res = scanf("%lld", x+i);
        val[i+n] = x[i];
      }
    }
  }
  
  qsort(val, n+q, sizeof(long long), cmp_ll);
  
  for (int i = 1; i < n+q; i++) {
    if (val[i-1] != val[i]) {
      val[val_cnt] = val[i];
      val_cnt++;
    }
  }
  
  for (int i = 0; i < n; i++) {
    int idx[2] = { 0, val_cnt };
    while (idx[1]-idx[0] > 1) {
      int nxt = (idx[0]+idx[1])/2;
      if (val[nxt] > a[i]) {
        idx[1] = nxt;
      } else {
        idx[0] = nxt;
      }
    }
    a[i] = (long long)idx[0];
  }
  for (int i = 0; i < q; i++) {
    int idx[2] = { 0, val_cnt };
    while (idx[1]-idx[0] > 1) {
      int nxt = (idx[0]+idx[1])/2;
      if (val[nxt] > x[i]) {
        idx[1] = nxt;
      } else {
        idx[0] = nxt;
      }
    }
    x[i] = (long long)idx[0];
  }
  
  while (size <= val_cnt) {
    size <<= 1;
  }
  
  for (int i = 0; i < n; i++) {
    add_segt(segt, (int)a[i], 1, size);
  }
  
  for (int i = 0; i < q; i++) {
    if (t[i] == 1 && prev > 0) {
      a[k[i]-1] = x[i];
      w[k[i]-1] = i;
    } else if (t[i] == 1) {
      add_segt(segt, (int)a[k[i]-1], -1, size);
      add_segt(segt, (int)x[i], 1, size);
      a[k[i]-1] = x[i];
      w[k[i]-1] = i;
    } else if (t[i] == 2 && prev > 0) {
      for (int j = prev; j < i; j++) {
        if (t[j] == 1 && w[k[j]-1] == j) {
          int idx[2] = { -1, val_cnt-1 };
          while (idx[1]-idx[0] > 1) {
            int nxt = (idx[0]+idx[1])/2;
            if (sum_segt(segt, 0, nxt+1, size) < k[j]) {
              idx[0] = nxt;
            } else {
              idx[1] = nxt;
            }
          }
          tmp[j] = idx[1];
        }
      }
      for (int j = prev; j < i; j++) {
        if (t[j] == 1 && w[k[j]-1] == j) {
          add_segt(segt, tmp[j], -1, size);
          add_segt(segt, (int)x[j], 1, size);
        }
      }
      prev = i+1;
    } else if (t[i] == 2) {
      prev = i+1;
    } else if (w[k[i]-1] >= prev) {
      printf("%lld\n", val[a[k[i]-1]]);
    } else {
      int idx[2] = { -1, val_cnt-1 };
      while (idx[1]-idx[0] > 1) {
        int nxt = (idx[0]+idx[1])/2;
        if (sum_segt(segt, 0, nxt+1, size) < k[i]) {
          idx[0] = nxt;
        } else {
          idx[1] = nxt;
        }
      }
      printf("%lld\n", val[idx[1]]);
    }
  }
  
  return 0;
}
0