結果

問題 No.2242 Cities and Teleporters
ユーザー 👑 chro_96chro_96
提出日時 2023-03-12 11:51:43
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 664 ms / 3,000 ms
コード長 2,380 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 31,972 KB
実行使用メモリ 34,644 KB
最終ジャッジ日時 2023-10-18 10:33:31
合計ジャッジ時間 11,889 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
33,192 KB
testcase_01 AC 11 ms
33,128 KB
testcase_02 AC 12 ms
33,192 KB
testcase_03 AC 11 ms
33,192 KB
testcase_04 AC 11 ms
33,128 KB
testcase_05 AC 442 ms
34,548 KB
testcase_06 AC 420 ms
33,824 KB
testcase_07 AC 432 ms
34,548 KB
testcase_08 AC 664 ms
34,632 KB
testcase_09 AC 454 ms
34,480 KB
testcase_10 AC 211 ms
34,632 KB
testcase_11 AC 266 ms
34,480 KB
testcase_12 AC 263 ms
34,632 KB
testcase_13 AC 309 ms
34,480 KB
testcase_14 AC 400 ms
34,548 KB
testcase_15 AC 333 ms
34,568 KB
testcase_16 AC 409 ms
34,480 KB
testcase_17 AC 579 ms
34,644 KB
testcase_18 AC 304 ms
34,480 KB
testcase_19 AC 489 ms
34,548 KB
testcase_20 AC 298 ms
34,380 KB
testcase_21 AC 289 ms
34,380 KB
testcase_22 AC 469 ms
34,380 KB
testcase_23 AC 460 ms
34,480 KB
testcase_24 AC 409 ms
34,548 KB
testcase_25 AC 437 ms
34,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;
}

int main () {
  int n = 0;
  int h[200000][2] = {};
  int t[200000] = {};
  int q = 0;
  int a = 0;
  int b = 0;
  
  int res = 0;
  
  int max[18][200000] = {};
  int org_h[200000] = {};
  int max_idx[18][200000] = {};
  
  res = scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    res = scanf("%d", h[i]);
    h[i][1] = i;
    org_h[i] = h[i][0];
  }
  for (int i = 0; i < n; i++) {
    res = scanf("%d", t+i);
  }
  
  qsort(h, n, sizeof(int)*2, cmp_int);
  
  max[0][0] = t[h[0][1]];
  for (int j = 1; j < n; j++) {
    max[0][j] = t[h[j][1]];
    if (max[0][j] < max[0][j-1]) {
      max[0][j] = max[0][j-1];
    }
  }
  
  for (int i = 1; i < 18; i++) {
    int idx = -1;
    for (int j = 0; j < n; j++) {
      while (idx < n-1 && h[idx+1][0] <= max[i-1][j]) {
        idx++;
      }
      if (idx < 0) {
        max[i][j] = max[i-1][j];
      } else {
        max[i][j] = max[i-1][idx];
      }
      if (max[i][j] < max[i-1][j]) {
        max[i][j] = max[i-1][j];
      }
      if (j > 0 && max[i][j] < max[i][j-1]) {
        max[i][j] = max[i][j-1];
      }
    }
  }
  
  for (int i = 0; i < 18; i++) {
    int idx = -1;
    for (int j = 0; j < n; j++) {
      while (idx < n-1 && h[idx+1][0] <= max[i][j]) {
        idx++;
      }
      max_idx[i][j] = idx;
    }
  }
  
  res = scanf("%d", &q);
  while (q > 0) {
    res = scanf("%d", &a);
    res = scanf("%d", &b);
    a--;
    b--;
    if (t[a] >= org_h[b]) {
      printf("1\n");
    } else {
      int idx = -1;
      int r = n;
      int ans[2] = { 0, (1<<18) };
      int bit = 17;
      while (r-idx > 1) {
        int nidx = (idx+r)/2;
        if (h[nidx][0] <= t[a]) {
          idx = nidx;
        } else {
          r = nidx;
        }
      }
      while (ans[1]-ans[0] > 1) {
        int nxt = (ans[0]+ans[1])/2;
        if (idx >= 0 && org_h[b] <= max[bit][idx]) {
          ans[1] = nxt;
        } else {
          ans[0] = nxt;
          if (idx >= 0) {
            idx = max_idx[bit][idx];
          }
        }
        bit--;
      }
      if (ans[1] < n) {
        printf("%d\n", ans[1]+1);
      } else {
        printf("-1\n");
      }
    }
    q--;
  }
  
  return 0;
}
0