結果
| 問題 |
No.1170 Never Want to Walk
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-01-09 19:14:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 383 ms / 2,000 ms |
| コード長 | 6,907 bytes |
| コンパイル時間 | 2,518 ms |
| コンパイル使用メモリ | 210,424 KB |
| 最終ジャッジ日時 | 2025-02-18 16:55:53 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 37 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <typename T> using min_priority_queue = priority_queue<T,vector<T>,greater<T>>;
template<typename T>
void printv(vector<T> &v){
bool b = false;
for(auto i : v){
if(b) cout << " ";
else b = true;
cout << i;
}
cout << endl;
}
int64_t rand64(){
int64_t l = rand();
int64_t r = rand();
return (l<<31)+r;
}
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <typename T>
struct monoid_min{
T val;
monoid_min(){
;
}
monoid_min(T x){
val = x;
}
monoid_min e(){
return monoid_min(numeric_limits<T>::max());
}
monoid_min operator*(monoid_min other){
if(val < other.val) return monoid_min(val);
return other;
}
bool operator==(monoid_min other){
return val == other.val;
}
bool operator!=(monoid_min other){
return val != other.val;
}
};
template <typename monoid>
struct mapping_add{
monoid val;
mapping_add(){
val = monoid().e();
}
mapping_add(monoid x){
val = x;
}
mapping_add id(){
return mapping_add();
}
monoid map(monoid s){
return monoid(s*val);
}
//写像 other*self
mapping_add composition(mapping_add other){
return mapping_add(val*other.val);
}
bool operator==(mapping_add other){
return val == other.val;
}
bool operator!=(mapping_add other){
return val != other.val;
}
};
template <typename monoid, typename mapping>
struct lazySegtree{
int sz;
vector<monoid> dat;
vector<mapping> lazy;
lazySegtree(int n){
sz = 1;
while(sz <= n) sz *= 2;
dat.resize(2*sz-1, monoid().e());
lazy.resize(2*sz-1, mapping().id());
}
void eval(int k, int l, int r){
if(lazy[k] != mapping().id()){
dat[k] = lazy[k].map(dat[k]);
if(r - l > 1){
lazy[2*k+1] = lazy[2*k+1].composition(lazy[k]);
lazy[2*k+2] = lazy[2*k+2].composition(lazy[k]);
}
}
lazy[k] = mapping().id();
return;
}
void sub_apply(int a, int b, mapping f, int k, int l, int r){
eval(k, l, r);
if(b <= l || r <= a) return;
if(a <= l && r <= b){
lazy[k] = lazy[k].composition(f);
eval(k, l, r);
}
else{
sub_apply(a, b, f, 2*k+1, l, (l+r)/2);
sub_apply(a, b, f, 2*k+2, (l+r)/2, r);
dat[k] = dat[2*k+1]*dat[2*k+2];
}
return;
}
void apply(int a, int b, mapping f){
sub_apply(a, b, f, 0, 0, sz);
return;
}
monoid sub_get(int a, int b, int k, int l, int r){
if(b <= l || r <= a) return monoid().e();
eval(k, l, r);
if(a <= l && r <= b) return dat[k];
monoid vl = sub_get(a, b, 2*k+1, l, (l+r)/2);
monoid vr = sub_get(a, b, 2*k+2, (l+r)/2, r);
return vl * vr;
}
monoid get(int a, int b){
return sub_get(a, b, 0, 0, sz);
}
};
struct UF {
vector<int> par;
vector<int> ran;
vector<int> sz;
UF(int n){
par.resize(n);
ran.resize(n);
sz.resize(n,1);
for(int i = 0; i < n; i++){
par[i] = i;
}
}
int find(int x){
if(par[x] == x) return x;
int rtn = find(par[x]);
par[x] = rtn;
return rtn;
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(ran[x] < ran[y]){
par[x] = y;
sz[y] += sz[x];
}
else{
par[y] = x;
sz[x] += sz[y];
if(ran[x] == ran[y]){
ran[x]++;
}
}
}
bool same(int x, int y){
return find(x) == find(y);
}
int siz(int x){
return sz[find(x)];
}
};
bool yn(bool b){
if(b) cout << "Yes" << endl;
else cout << "No" << endl;
return b;
}
bool debug;
bool randomInput;
bool debugOutput;
int numOfTestCase;
using ans_type = int;
void input(){
if(numOfTestCase > 1){
}
if(randomInput){
}
else{
}
return;
}
void output_input(){
;
}
ans_type calc(){
int N, A, B;
cin >> N >> A >> B;
int INF = 2001002003;
vector<int> X(N+2);
X[0] = -INF;
X[N+1] = INF;
for(int i = 1; i <= N; i++) cin >> X[i];
vector<tuple<int,int,int,int>> V(N);
for(int i = 0; i < N; i++){
int x = X[i+1];
auto itr1 = lower_bound(X.begin(), X.end(), x - B);
auto itr2 = prev(upper_bound(X.begin(), X.end(), x - A));
auto itr3 = lower_bound(X.begin(), X.end(), x + A);
auto itr4 = prev(upper_bound(X.begin(), X.end(), x + B));
int a1 = itr1 - X.begin() - 1;
int a2 = itr2 - X.begin();
int a3 = itr3 - X.begin() - 1;
int a4 = itr4 - X.begin();
V[i] = {a1, a2, a3, a4};
}
vector<pair<int,int>> I;
for(auto[a, b, c, d] : V){
//cout << a << " " << b << endl;
chmax(a, 0);
chmax(b, 0);
if(a == b) continue;
int l, r;
r = -1;
if(I.size()) tie(l, r) = I[I.size()-1];
if(r <= a) I.push_back({a, b});
else I[I.size()-1].second = b;
}
//cout << endl;
//for(auto [l, r] : I) cout << l << " " << r << endl;
UF U(N);
for(auto[l, r] : I){
for(int i = l; i < r-1; i++){
U.unite(i, i+1);
}
}
for(int i = 0; i < N; i++){
auto[a, b, c, d] = V[i];
if(a >= 0 && a < b) U.unite(i, a);
}
vector<int> ans(N);
for(int i = 0; i < N; i++) ans[i] = U.siz(i);
for(auto i : ans) cout << i << endl;
return ans_type();
}
ans_type calc_simple(){
return ans_type();
}
void output(ans_type ans){
return;
}
int main(){
debug = 0;
randomInput = 0;
debugOutput = 0;
numOfTestCase = 1;
srand(time(NULL));
cout << fixed << setprecision(12);
if(numOfTestCase == 0) cin >> numOfTestCase;
if(debug){
for(int i = 0; i < numOfTestCase; i++){
if(debugOutput) cout << string(16, '-') << endl;
input();
ans_type ans = calc();
ans_type ansSimple = calc_simple();
if(ans != ansSimple){
output_input();
output(ans);
output(ansSimple);
}
}
}
else{
for(int i = 0; i < numOfTestCase; i++){
input();
output(calc());
}
}
return 0;
}