結果

問題 No.728 ギブ and テイク
ユーザー annin256annin256
提出日時 2018-08-26 23:15:12
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 2,794 ms / 3,000 ms
コード長 1,135 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 31,232 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 04:03:15
合計ジャッジ時間 21,197 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int n, a[300000], L[300000], R[300000], b[100][3000], bc[100];

int cmp(const void *x, const void *y) {
  return *(int *)x - *(int *)y;
}

int gt(int *a, int n, int x) {
  int l = -1;
  int r = n;
  while (r - l > 1) {
    int m = (r + l) / 2;
    if (a[m] >= x) {
      r = m;
    } else {
      l = m;
    }
  }
  return n - r;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &L[i], &R[i]);
    b[i / 3000][i % 3000] = a[i] + R[i];
    bc[i / 3000]++;
  }
  for (int i = 0; i < 100; i++) {
    qsort(b[i], bc[i], sizeof(int), cmp);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int j = i;
    while (j - 3000 >= 0 && a[j - 3000] >= a[i] - L[i]) j -= 3000;
    while (j - 1 >= 0 && a[j - 1] >= a[i] - L[i]) j -= 1;
    while (j < i) {
      if (j % 3000 == 0 && j + 3000 <= i) {
        ans += gt(b[j / 3000], bc[j / 3000], a[i]);
        j += 3000;
      } else {
        if (a[j] + R[j] >= a[i]) ans++;
        j += 1;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
0