結果
問題 |
No.1170 Never Want to Walk
|
ユーザー |
|
提出日時 | 2020-08-15 00:09:36 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 902 ms / 2,000 ms |
コード長 | 1,751 bytes |
コンパイル時間 | 1,098 ms |
コンパイル使用メモリ | 131,648 KB |
実行使用メモリ | 9,472 KB |
最終ジャッジ日時 | 2024-10-10 16:56:43 |
合計ジャッジ時間 | 13,519 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <numeric> using namespace std; class UnionFind { private: vector<long long> par,_size,_right; public: UnionFind(long long n) : par(n+1),_size(n+1,1),_right(n+1) { iota(par.begin(), par.end(), 0); iota(_right.begin(), _right.end(), 0); } long long root(long long x) { if (x == par[x]) return x; else return par[x] = root(par[x]); } bool isSame(long long x, long long y) { return root(x) == root(y); } bool merge(long long x, long long y) { x = root(x); y = root(y); if (isSame(x,y)) return false; if (_size[x] < _size[y]) swap(x,y); par[y] = x; _size[x] += _size[y]; _right[x] = max(_right[x], _right[y]); return true; } void range_merge(long long x, long long y) { if (x >= y) return; while(this->right(x) < y) { this->merge(this->right(x), this->right(x)+1); } } long long size(long long x) { return _size[root(x)]; } long long right(long long x) { return _right[root(x)]; } }; int main() { int n,a,b; cin >> n >> a >> b; vector x(n,0); for (int i=0; i<n; i++) cin >> x[i]; UnionFind uni(n); vector imos(n+1, 0); for (int i=0; i<n-1; i++) { auto l = lower_bound(x.begin(), x.end(), x[i]+a); auto r = upper_bound(x.begin(), x.end(), x[i]+b); if (l >= r) continue; int lindex = l - x.begin(); int rindex = r - x.begin(); uni.merge(i, lindex); uni.range_merge(lindex, rindex-1); cerr<<i<<endl; } for (int i=0; i<n; i++) { cout << uni.size(i) << endl; } }