結果

問題 No.1170 Never Want to Walk
ユーザー yasunoriyasunori
提出日時 2024-01-09 16:45:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,909 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 224,256 KB
実行使用メモリ 14,536 KB
最終ジャッジ日時 2024-09-27 19:55:49
合計ジャッジ時間 11,258 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 4 ms
6,944 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 508 ms
14,196 KB
testcase_33 AC 513 ms
14,140 KB
testcase_34 AC 512 ms
14,248 KB
testcase_35 AC 515 ms
14,208 KB
testcase_36 AC 498 ms
14,156 KB
testcase_37 AC 473 ms
14,208 KB
testcase_38 AC 484 ms
14,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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() - 1;
        int a3 = itr3 - X.begin() - 1;
        int a4 = itr4 - X.begin() - 1;
        V[i] = {a1, a2, a3, a4};
    }

    lazySegtree<monoid_min<int>, mapping_add<monoid_min<int>>> seg(N);
    for(int i = N-1; i >= 0; i--){
        auto[a, b, c, d] = V[i];
        b++;
        d++;
        chmax(a, 0);
        chmin(a, N);
        chmax(b, 0);
        chmin(b, N);
        chmax(c, 0);
        chmin(c, N);
        chmax(d, 0);
        chmin(d, N);
        if(a < b) seg.apply(a, b, mapping_add<monoid_min<int>>(monoid_min<int>(i)));
        if(c < d) seg.apply(c, d, mapping_add<monoid_min<int>>(monoid_min<int>(i)));
    }

    UF U(N);
    for(int i = 0; i < N; i++){
        int j = seg.get(i, i+1).val;
        if(0 <= j && j < N) U.unite(i, j);
    }

    vector<int> ans(N);
    for(int i = 0; i < N; i++){
        ans[i] = U.siz(i);
    }

    for(int 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;
}
0