結果

問題 No.1170 Never Want to Walk
ユーザー msm1993
提出日時 2020-09-11 16:23:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 121,408 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-26 03:34:51
合計ジャッジ時間 6,302 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <climits>
#include <numeric>
#include <queue>
#include <cmath>
#include <iomanip>
#include <array>
#include <string>
#include <stack>
#include <cassert>
#include <memory>
#include <random>
#include <fstream>
#include <cfloat>
#include <complex>

class UnionFind {
	std::vector<int> vec;
public:
	UnionFind(int size) : vec(size, -1) {};
	int find(int a) {
		return vec[a] < 0 ? a : vec[a] = find(vec[a]);
	}
	int size_of(int a) {
		return -vec[find(a)];
	}
	void unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b) return;
		if (vec[a] > vec[b]) std::swap(a, b);
		vec[a] += vec[b];
		vec[b] = a;
	}
};
int main() {
	int n, a, b; std::cin >> n >> a >> b;
	std::vector<int> stations(n); for (auto& s : stations) std::cin >> s;
	int right = 0;
	UnionFind uft(n);
	for (auto i = 0; i < n; ++i) {
		const auto s = stations[i];
		const auto begin = std::distance(stations.begin(), std::lower_bound(stations.begin(), stations.end(), s + a));
		const auto end = std::distance(stations.begin(), std::upper_bound(stations.begin(), stations.end(), s + b));
		if (begin < right) {
			uft.unite(i, begin);
		}
		else {
			right = begin;
		}
		while (right < end) {
			uft.unite(i, right++);
		}
	}
	for (auto i = 0; i < n; ++i) {
		std::cout << uft.size_of(i) << '\n';
	}
}
0