結果

問題 No.1170 Never Want to Walk
ユーザー snow39snow39
提出日時 2020-08-14 22:11:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 2,497 bytes
コンパイル時間 1,339 ms
コンパイル使用メモリ 108,768 KB
実行使用メモリ 12,588 KB
最終ジャッジ日時 2024-04-18 22:01:20
合計ジャッジ時間 4,983 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 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 1 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 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 120 ms
11,940 KB
testcase_28 AC 118 ms
11,756 KB
testcase_29 AC 118 ms
11,384 KB
testcase_30 AC 118 ms
12,004 KB
testcase_31 AC 117 ms
11,652 KB
testcase_32 AC 106 ms
12,452 KB
testcase_33 AC 117 ms
12,124 KB
testcase_34 AC 112 ms
12,476 KB
testcase_35 AC 112 ms
12,500 KB
testcase_36 AC 108 ms
12,164 KB
testcase_37 AC 109 ms
12,304 KB
testcase_38 AC 111 ms
12,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  ll A,B;
  cin>>N>>A>>B;
  vector<ll> X(N);
  rep(i,N) cin>>X[i];

  DisjointSet us(N);
  vector<pair<int,int>> qry;
  for(int i=0;i<N;i++){
    {//[l,r]
      int l=lower_bound(all(X),X[i]-B)-X.begin();
      int r=upper_bound(all(X),X[i]-A)-X.begin();
      r--;
      if(l<=r){
        us.unite(i,l);
        us.unite(i,r);
        if(l<r) qry.push_back(mkp(l,r));
      }
    }
    {//[l,r]
      int l=lower_bound(all(X),X[i]+A)-X.begin();
      int r=upper_bound(all(X),X[i]+B)-X.begin();
      r--;
      if(l<=r){
        us.unite(i,l);
        us.unite(i,r);
        if(l<r) qry.push_back(mkp(l,r));
      }
    }
  }

  int Q=qry.size();
  vector<int> ord(Q);
  rep(i,Q) ord[i]=i;
  sort(all(ord),[&](int a,int b){
    if(qry[a].second==qry[b].second) return a<b;
    else return qry[a].second<qry[b].second;
  });

  vector<int> left(N,0);
  rep(i,N) left[i]=i-1;
  for(auto i:ord){
    int a=qry[i].first;
    int b=qry[i].second;

    int pos=b;
    while(left[pos]>=a&&pos>=0){
      us.unite(b,pos);
      chmin(left[pos],left[left[a]]);
      pos=left[pos];
    }
  }

  rep(i,N) cout<<us.findSize(i)<<"\n";

  return 0;
}
0