結果

問題 No.1170 Never Want to Walk
ユーザー fuppy_kyoprofuppy_kyopro
提出日時 2020-08-14 22:16:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,379 ms / 2,000 ms
コード長 7,815 bytes
コンパイル時間 2,778 ms
コンパイル使用メモリ 191,648 KB
実行使用メモリ 372,524 KB
最終ジャッジ日時 2024-04-18 22:06:42
合計ジャッジ時間 18,153 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 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 6 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 AC 7 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 788 ms
157,084 KB
testcase_28 AC 801 ms
153,920 KB
testcase_29 AC 791 ms
150,632 KB
testcase_30 AC 846 ms
154,208 KB
testcase_31 AC 796 ms
153,624 KB
testcase_32 AC 1,278 ms
299,824 KB
testcase_33 AC 1,379 ms
372,524 KB
testcase_34 AC 1,257 ms
357,972 KB
testcase_35 AC 1,145 ms
298,024 KB
testcase_36 AC 1,180 ms
333,792 KB
testcase_37 AC 1,282 ms
304,084 KB
testcase_38 AC 1,176 ms
288,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;
 
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl;
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
// #define int ll
 
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
// #define mp make_pair
//#define endl '\n'

int nn;

class UnionFind {
public:
  int n;
  vi par; //�e
  vi ran; //�؂̐[��
  vi num; //�v�f��
  int g; // group��

  UnionFind(){}

  UnionFind(int _n) {
    n = _n;
    g = n;
    par.resize(n); ran.resize(n); num.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i; ran[i] = 0;
      if (i < nn) {
        num[i] = 1;
      }
    }
  }
 
  //�؂̍������߂�
  int find(int x) {
    if (par[x] == x) {
      return x;
    }
    else {
      return par[x] = find(par[x]);
    }
  }
 
  //x��y�̑�����W���𕹍�
  void unite(int x, int y) {
    x = find(x); y = find(y);
    int numsum = num[x] + num[y];
    if (x == y) {
      return;
    }
    if (ran[x]<ran[y]) {
      par[x] = y;
    }
    else {
      par[y] = x;
      if (ran[x] == ran[y]) {
        ran[x]++;
      }
    }
    num[x] = num[y] = numsum;
    g--;
  }
 
  //x��y�������W���ɑ����邩�ۂ�
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

UnionFind uf;

class RangeEdges {
    // https://twitter.com/noshi91/status/1193177214453338113
    // https://codeforces.com/contest/786/submission/85182398
    // https://atcoder.jp/contests/nikkei2019-2-qual/submissions/14711404
    public:
    int n; // 問題で与えられた頂点数
    int V; // 構築するグラフの頂点数
    vector<vector<pll>> G;
    vector<bool> used;

    // 0 ~ n - 1: 各頂点
    // n, n + 2, n + 4, ..., n + 2 * (K - 1): out側の各区間(図の上側)
    // n + 1, n + 3, ..., n + 2 * K - 1: in側の各区間(図の下側)
    // n + 2 * K ~ 辺を貼ることによって増えたスーパー頂点

    RangeEdges(int n): n(n), V(n) {
        G.resize(9 * n);
        used.resize(9 * n, false);
        init(0, 0, n);
    }

    void init(int k, int l, int r) {
        if (r - l == 1) {
            G[n + 2 * k].push_back(pll(0, l));
            G[l].push_back(pll(0, n + 2 * k + 1));
            chmax(V, n + 2 * k + 2);
            return;
        }

        int lch = 2 * k + 1, rch = 2 * k + 2;
        G[n + 2 * k].push_back(pll(0, n + 2 * lch));
        G[n + 2 * k].push_back(pll(0, n + 2 * rch));
        G[n + 2 * lch + 1].push_back(pll(0, n + 2 * k + 1));
        G[n + 2 * rch + 1].push_back(pll(0, n + 2 * k + 1));
        init(lch, l, (l + r) / 2);
        init(rch, (l + r) / 2, r);
    }

    void get_ranges(int a, int b, int k, int l, int r, vector<int>& res) {
        if (r <= a || b <= l) {
            return;
        }
        if (a <= l && r <= b) {
            res.push_back(k);
            return;
        }
        else {
            get_ranges(a, b, k * 2 + 1, l, (l + r) / 2, res);
            get_ranges(a, b, k * 2 + 2, (l + r) / 2, r, res);
            return;
        }
    }

    // [lr1.first, lr1.second) -> [lr2.first, lr2.second)にcostの辺を貼る
    void add_edge(pii lr1, pii lr2, ll cost) {
        vector<int> in_ranges;
        get_ranges(lr1.first, lr1.second, 0, 0, n, in_ranges);
        rep (i, in_ranges.size()) in_ranges[i] = n + in_ranges[i] * 2 + 1;
        vector<int> out_ranges;
        get_ranges(lr2.first, lr2.second, 0, 0, n, out_ranges);
        rep (i, out_ranges.size()) out_ranges[i] = n + out_ranges[i] * 2;
        // DEBUG_VEC(in_ranges);
        // DEBUG_VEC(out_ranges);

        V += 2;
        while (G.size() < V) {
            G.push_back(vector<pll>());
            used.push_back(false);
        }

        for (int u: in_ranges) {
            G[u].push_back(pll(0, V - 2));
        }
        G[V - 2].push_back(pll(cost, V - 1));
        for (int v: out_ranges) {
            G[V - 1].push_back(pll(0, v));
        }
    }

    vl dijkstra(int s) {
        vl dist(V, INF);
        dist[s] = 0;
        priority_queue<pll, vector<pll>, greater<pll>> pq;
        pq.push(pll(0, s));
        while (pq.size()) {
            pll temp = pq.top();
            pq.pop();
            int u = temp.second;
            ll cost = temp.first;
            if (dist[u] < cost) continue;
            rep (i, G[u].size()) {
                int v = G[u][i].second;
                ll ncost = cost + G[u][i].first;
                if (dist[v] > ncost) {
                    dist[v] = ncost;
                    pq.push(pll(ncost, v));
                }
            }
        }

        dist.resize(n);
        return dist;
    }    

    void dfs(int ori_u, int u) {
        used[u] = true;
        rep (i, G[u].size()) {
            int v = G[u][i].second;
            if (used[v]) continue;
            if (v < nn) {
                uf.unite(ori_u, v);
            }
            dfs(ori_u, v);
        }
    }
};

signed main() {
    fio();
    int n, a, b;
    cin >> n >> a >> b;
    nn = n;
    vi x(n);
    rep (i, n) cin >> x[i];

    RangeEdges graph(n);

    rep (i, n) {
        auto itr = lower_bound(all(x), x[i] + a);
        int u = itr - x.begin();
        itr = upper_bound(all(x), x[i] + b);
        int v = itr - x.begin();
        // DEBUG(i);
        // DEBUG(pii(u, v));
        graph.add_edge(pii(i, i + 1), pii(u, v), 1);
        itr = lower_bound(all(x), x[i] - b);
        u = itr - x.begin();
        itr = upper_bound(all(x), x[i] - a);
        v = itr - x.begin();
        graph.add_edge(pii(i, i + 1), pii(u, v), 1);
    }
    // DEBUG(graph.V);
    uf = UnionFind(graph.V);

    rep (i, n) {
        if (not graph.used[i]) {
            // DEBUG(i);
            graph.dfs(i, i);
        }
    }

    map<int, int> mp;
    rep (i, n) {
        mp[uf.find(i)]++;
    }
    rep (i, n) {
        cout << mp[uf.find(i)] << endl;
    }
}
0