結果

問題 No.2242 Cities and Teleporters
ユーザー 👑 chro_96chro_96
提出日時 2023-03-10 22:21:12
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,582 bytes
コンパイル時間 1,468 ms
コンパイル使用メモリ 32,172 KB
実行使用メモリ 38,028 KB
最終ジャッジ日時 2023-10-18 08:03:18
合計ジャッジ時間 7,581 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
32,436 KB
testcase_01 AC 11 ms
32,432 KB
testcase_02 AC 11 ms
32,432 KB
testcase_03 AC 11 ms
32,432 KB
testcase_04 AC 11 ms
32,432 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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][18] = {};
  int q = 0;
  int a = 0;
  int b = 0;
  
  int res = 0;
  
  int max[18][200000] = {};
  int org_h[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]][0];
  for (int j = 1; j < n; j++) {
    max[0][j] = t[h[j][1]][0];
    if (max[0][j] < max[0][j-1]) {
      max[0][j] = max[0][j-1];
    }
  }
  
  for (int i = 1; i < 18; i++) {
    for (int j = 0; j < n; j++) {
      int idx[2] = { -1, n };
      while (idx[1]-idx[0] > 1) {
        int nxt = (idx[0]+idx[1])/2;
        if (h[nxt][0] <= t[j][i-1]) {
          idx[0] = nxt;
        } else {
          idx[1] = nxt;
        }
      }
      if (idx[0] < 0) {
        t[j][i] = t[j][i-1];
      } else {
        t[j][i] = max[i-1][idx[0]];
      }
      if (t[j][i] < t[j][i-1]) {
        t[j][i] = t[j][i-1];
      }
    }
    max[i][0] = t[h[0][1]][i];
    for (int j = 1; j < n; j++) {
      max[i][j] = t[h[j][1]][i];
      if (max[i][j] < max[i][j-1]) {
        max[i][j] = max[i][j-1];
      }
    }
  }
  
  res = scanf("%d", &q);
  while (q > 0) {
    int ans[2] = { 0, n+1 };
    res = scanf("%d", &a);
    res = scanf("%d", &b);
    a--;
    b--;
    while (ans[1]-ans[0] > 1) {
      int nxt = (ans[0]+ans[1])/2;
      int cnt = 0;
      int tmp = 0;
      int org_nxt = nxt;
      while (nxt%2 == 0) {
        nxt /= 2;
        cnt++;
      }
      tmp = t[a][cnt];
      nxt /= 2;
      cnt++;
      while (nxt > 0) {
        if (nxt%2 == 1) {
          int idx[2] = { -1, n };
          while (idx[1]-idx[0] > 1) {
            int nidx = (idx[0]+idx[1])/2;
            if (h[nidx][0] <= tmp) {
              idx[0] = nidx;
            } else {
              idx[1] = nidx;
            }
          }
          if (idx[0] >= 0 && max[cnt][idx[0]] > tmp) {
            tmp = max[cnt][idx[0]];
          }
        }
        nxt /= 2;
        cnt++;
      }
      if (org_h[b] <= tmp) {
        ans[1] = org_nxt;
      } else {
        ans[0] = org_nxt;
      }
    }
    if (ans[1] > n) {
      printf("-1\n");
    } else {
      printf("%d\n", ans[1]);
    }
    q--;
  }
  
  return 0;
}
0