結果

問題 No.1170 Never Want to Walk
ユーザー drken1215
提出日時 2020-08-14 21:41:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 174,744 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-10 14:43:24
合計ジャッジ時間 7,932 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }


struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};


int main() {
    int N;
    long long A, B;
    cin >> N >> A >> B;
    vector<long long> x(N);
    for (int i = 0; i < N; ++i) cin >> x[i];

    int prev = 0;
    UnionFind uf(N);
    for (int i = 0; i < N; ++i) {
        int left = lower_bound(x.begin(), x.end(), x[i] + A) - x.begin();
        int right = upper_bound(x.begin(), x.end(), x[i] + B) - x.begin();
        if (left < prev) uf.merge(i-1, i);
        for (int j = max(left, prev); j < right; ++j) {
            uf.merge(i, j);
        }
        prev = right;
    }
    for (int i = 0; i < N; ++i) {
        cout << uf.size(i) << endl;
    }

}















0