結果
問題 | No.1170 Never Want to Walk |
ユーザー |
|
提出日時 | 2020-08-14 23:09:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 368 ms / 2,000 ms |
コード長 | 1,982 bytes |
コンパイル時間 | 1,184 ms |
コンパイル使用メモリ | 100,060 KB |
実行使用メモリ | 19,516 KB |
最終ジャッジ日時 | 2024-10-10 16:36:05 |
合計ジャッジ時間 | 7,604 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; struct UnionFind { private: vector<ll> child, tree; vector<vector<ll>> list; public: //頂点数v UnionFind(ll v) { tree.resize(v); list.resize(v); for(ll i = 0; i < v; i++) { tree[i] = i; list[i].push_back(i); } } //木の根を求める ll root(ll x) { if(x == tree[x]) { for(ll i = 0; i < child.size(); i++) { tree[child[i]] = x; } child.clear(); return x; } else { child.push_back(x); return x = root(tree[x]); } } // xの所属するグループの大きさ ll size(ll x) { return list[root(x)].size(); } vector<ll> nodes(ll no) { return list[root(no)]; } // xとyの集合を合わせる bool unit(ll x, ll y) { x = root(x); y = root(y); if(x == y) { return false; } if(list[x].size() < list[y].size()) { swap(x, y); } for(int no : list[y]) { list[x].emplace_back(no); } tree[y] = x; return true; } // xとyが同じグループに所属するか bool isUnit(ll x, ll y) { return root(x) == root(y); } }; int main() { int n, a, b; cin >> n >> a >> b; vector<ll> x(n); for(int i = 0; i < n; i++) { cin >> x[i]; } UnionFind uf(n); for(int i = 0; i < n; i++) { auto check1 = lower_bound(x.begin(), x.end(), x[i] + a); auto check2 = upper_bound(x.begin(), x.end(), x[i] + b); int l, r; if(check1 != x.end()) { l = distance(x.begin(), check1); r = distance(x.begin(), check2); for(int j = r - 1; j >= l; j--) { if(uf.isUnit(i, j)) { break; } uf.unit(i, j); } } } for(int i = 0; i < n; i++) { cout << uf.size(i) << endl; } return 0; }