結果

問題 No.1170 Never Want to Walk
ユーザー startcppstartcpp
提出日時 2020-08-14 23:04:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 352 ms / 2,000 ms
コード長 1,700 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 74,096 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-04-18 22:56:41
合計ジャッジ時間 6,789 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 344 ms
6,656 KB
testcase_28 AC 345 ms
6,528 KB
testcase_29 AC 349 ms
6,784 KB
testcase_30 AC 347 ms
6,656 KB
testcase_31 AC 352 ms
6,784 KB
testcase_32 AC 337 ms
6,656 KB
testcase_33 AC 344 ms
6,656 KB
testcase_34 AC 351 ms
6,912 KB
testcase_35 AC 344 ms
7,040 KB
testcase_36 AC 331 ms
6,656 KB
testcase_37 AC 329 ms
6,528 KB
testcase_38 AC 344 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//連結成分のみが重要
//例えば駅iから駅Li~Riへ辺を張るのは、駅Li-(Li+1)-…-Riと辺を張ったあと、iからLiへ辺を張る
//と置き換えても答えは変わらない(と思う)
//前者(直線グラフ)は隣り合う2駅間の辺の個数をimos法で求めればOK. 後者は愚直に.
//辺の数がO(N)くらいになるので、あとはUnion Findでやればよし。
//区間は尺取り4回やればO(N)で求まるけど、O(NlogN)かけて2分探索した方が楽なので、2分探索で実装.
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

struct UF {
	int par[200000];
	int sz[200000];
	
	void init() {
		int i;
		rep(i, 200000) {
			par[i] = i;
			sz[i] = 1;
		}
	}
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	void unit(int x, int y) { x = root(x); y = root(y); if (x != y) { par[x] = y; sz[y] += sz[x]; } }
	int size(int x) { return sz[root(x)]; }
};

int n, a, b;
int x[200000];
int imos[252521];
UF uf;

int main() {
	int i;
	
	cin >> n >> a >> b;
	rep(i, n) cin >> x[i];
	
	uf.init();
	rep(i, n) {
		int l1 = lower_bound(x, x + n, x[i] - b) - x;
		int r1 = upper_bound(x, x + n, x[i] - a) - x;
		int l2 = lower_bound(x, x + n, x[i] + a) - x;
		int r2 = upper_bound(x, x + n, x[i] + b) - x;
		//[l1, r1)
		if (l1 < r1) {
			imos[l1]++;
			imos[r1 - 1]--;
			uf.unit(i, l1);
		}
		//[l2, r2)
		if (l2 < r2) {
			imos[l2]++;
			imos[r2 - 1]--;
			uf.unit(i, l2);
		}
	}
	
	rep(i, n) imos[i + 1] += imos[i];
	rep(i, n - 1) {
		if (imos[i] > 0) {
			uf.unit(i, i + 1);
		}
	}
	
	rep(i, n) cout << uf.size(i) << endl;
	return 0;
}
0