結果

問題 No.1170 Never Want to Walk
ユーザー ningenMeningenMe
提出日時 2020-08-15 03:57:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 388 ms / 2,000 ms
コード長 3,076 bytes
コンパイル時間 2,592 ms
コンパイル使用メモリ 204,040 KB
実行使用メモリ 13,148 KB
最終ジャッジ日時 2024-04-18 23:36:25
合計ジャッジ時間 9,144 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 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 4 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 326 ms
13,124 KB
testcase_28 AC 325 ms
12,972 KB
testcase_29 AC 334 ms
13,032 KB
testcase_30 AC 332 ms
13,148 KB
testcase_31 AC 325 ms
12,984 KB
testcase_32 AC 377 ms
12,996 KB
testcase_33 AC 386 ms
12,976 KB
testcase_34 AC 386 ms
13,028 KB
testcase_35 AC 388 ms
13,024 KB
testcase_36 AC 380 ms
12,984 KB
testcase_37 AC 378 ms
12,988 KB
testcase_38 AC 386 ms
13,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

//Union Find Tree
class UnionFindTree2 {
	vector<int> parent,maxi,mini;
	inline int root(int n) {
        return (parent[n]<0?n:parent[n] = root(parent[n]));
	}
public:
    UnionFindTree2(int N = 1) : parent(N,-1),maxi(N),mini(N){
        iota(maxi.begin(),maxi.end(),0);
        iota(mini.begin(),mini.end(),0);
	}
    inline bool connected(int n, int m) {
		return root(n) == root(m);
	}
	inline void merge(int n, int m) {
		n = root(n);
		m = root(m);
		if (n == m) return;
		if(parent[n]>parent[m]) swap(n, m);
        parent[n] += parent[m];
        parent[m] = n;
        maxi[n] = std::max(maxi[n],maxi[m]);
        mini[n] = std::min(mini[n],mini[m]);
	}
    inline int min(int n) {
        return mini[root(n)];
    }
    inline int max(int n) {
        return maxi[root(n)];
    }
    inline int size(int n){
        return (-parent[root(n)]);
    }
    inline int operator[](int n) {
		return root(n);
	}
    inline void print() {
        for(int i = 0; i < parent.size(); ++i) cout << root(i) << " ";
        cout << endl;
    }
};

class RangeIndex{
    int length;
public:
    RangeIndex(const int N) {
        for (length = 1; length <= N; length *= 2);
    }
    //[l,r)
    vector<int> range(int l,int r) {
        vector<int> res;
        for(l += length, r += length; l < r; l >>=1, r >>=1) {
            if(l&1) res.push_back(l++);
            if(r&1) res.push_back(--r);
        }
        return res;
    }
    inline int operator[](int idx) {
		return idx+length;
	}
    inline size_t size(void){
        return length;
    }
};

/*
 * @title UnionFindTree
 */
class UnionFindTree {
public:
	vector<int> parent;
	vector<int> rank;

	UnionFindTree(int N) : parent(N), rank(N,0){
		iota(parent.begin(),parent.end(),0);
	} 
	int root(int n) {
		return (parent[n] == n ? n : parent[n] = root(parent[n]));
	}
	inline int connected(int n, int m) {
		return root(n) == root(m);
	}
    inline int operator[](int n) {
		return root(n);
	}
	inline void merge(int n, int m) {
		n = root(n);
		m = root(m);
		if (n == m) return;
		if (rank[n]<rank[m]) {
			parent[n] = m;
		}
		else{
			parent[m] = n;
			if(rank[n] == rank[m]) rank[n]++;
		}
	}
};


int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    long long N,A,B; cin >> N >> A >> B;
    vector<long long> X(N);
    for(int i = 0; i < N; ++i) cin >> X[i];
    X.push_back(1e10);
    RangeIndex ri(N);
    UnionFindTree uf(2*ri.size());
    vector<int> st(2*ri.size(),0);
    for(int i = 0; i < N; ++i) {
        int l = lower_bound(X.begin(),X.end(),X[i]+A)-X.begin();
        int r = upper_bound(X.begin(),X.end(),X[i]+B)-X.begin();
        for(auto& e:ri.range(l,r)) {
            st[e]=1;
            uf.merge(ri[i],e);
        }
    }
    for(int i = 0; i < N; ++i) {
        int j=i+ri.size();
        while(j >>= 1) if(st[j]) uf.merge(ri[i],j);
    }
    vector<int> cnt(2*ri.size(),0);
    for(int i = 0; i < N; ++i) {
        cnt[uf[ri[i]]]++;
    }
    for(int i = 0; i < N; ++i) {
        cout << cnt[uf[ri[i]]] << endl;
    }

    return 0;
}
0