結果

問題 No.1950 片道きゃっちぼーる
ユーザー tnakao0123tnakao0123
提出日時 2022-05-21 14:23:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,946 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 43,784 KB
実行使用メモリ 7,984 KB
最終ジャッジ日時 2023-10-20 16:13:45
合計ジャッジ時間 4,497 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,276 KB
testcase_01 AC 3 ms
7,276 KB
testcase_02 AC 2 ms
7,276 KB
testcase_03 AC 91 ms
7,944 KB
testcase_04 WA -
testcase_05 AC 2 ms
7,276 KB
testcase_06 AC 83 ms
7,944 KB
testcase_07 AC 85 ms
7,944 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 88 ms
7,944 KB
testcase_12 AC 88 ms
7,944 KB
testcase_13 AC 91 ms
7,944 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 88 ms
7,944 KB
testcase_17 WA -
testcase_18 AC 92 ms
7,944 KB
testcase_19 WA -
testcase_20 AC 86 ms
7,944 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1950.cc:  No.1950 片道きゃっちぼーる - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];
  UFT() {}

  void init(int n) {
    for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

int xs[MAX_N], as[MAX_N], ys[MAX_N];
UFT uft;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", xs + i);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  uft.init(n);
  for (int i = 0; i < n; i++) ys[i] = xs[i] + as[i];

  for (int i = 0; i < n; i++) {
    int x0 = xs[i] - as[i], x1 = xs[i] + as[i];
    int j0 = lower_bound(xs, xs + n, x0) - xs;
    int j1 = lower_bound(xs, xs + n, x1) - xs;

    if (j0 < n && xs[j0] == x0) {
      if (! uft.same(i, j0)) {
	int k = uft.merge(i, j0);
	ys[k] = max(ys[i], ys[j0]);
      }
    }
    if (j1 < n && xs[j1] == x1) {
      if (! uft.same(i, j1)) {
	int k = uft.merge(i, j1);
	ys[k] = max(ys[i], ys[j1]);
      }
    }
  }
  
  for (int i = 0; i < n; i++)
    printf("%d\n", ys[uft.root(i)] - xs[i]);

  return 0;
}
0