結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー ferinferin
提出日時 2019-09-07 00:05:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,220 bytes
コンパイル時間 2,274 ms
コンパイル使用メモリ 172,472 KB
実行使用メモリ 18,704 KB
最終ジャッジ日時 2023-09-07 03:21:18
合計ジャッジ時間 20,856 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,384 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 WA -
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 WA -
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 534 ms
14,040 KB
testcase_12 AC 520 ms
13,996 KB
testcase_13 AC 373 ms
13,884 KB
testcase_14 AC 519 ms
13,992 KB
testcase_15 AC 545 ms
13,984 KB
testcase_16 AC 592 ms
13,924 KB
testcase_17 AC 654 ms
14,136 KB
testcase_18 AC 640 ms
14,276 KB
testcase_19 AC 390 ms
14,000 KB
testcase_20 AC 387 ms
14,396 KB
testcase_21 AC 397 ms
13,948 KB
testcase_22 AC 381 ms
13,852 KB
testcase_23 AC 394 ms
14,056 KB
testcase_24 AC 368 ms
14,012 KB
testcase_25 AC 376 ms
14,264 KB
testcase_26 AC 381 ms
13,876 KB
testcase_27 AC 369 ms
14,360 KB
testcase_28 AC 384 ms
13,996 KB
testcase_29 AC 507 ms
14,004 KB
testcase_30 AC 548 ms
14,096 KB
testcase_31 AC 573 ms
14,248 KB
testcase_32 AC 162 ms
14,440 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

template<typename dat_type, typename lazy_type>
struct segtree {
    ll n;
    vector<dat_type> dat;
    dat_type dat_d;
    vector<lazy_type> lazy;
    lazy_type lazy_d;

    dat_type merge_dat(dat_type l, dat_type r) {
        dat_type ret;
        ret.sum = l.sum + r.sum;
        ret.max = max(l.max, r.max);
        ret.min = min(l.min, r.min);
        ret.lcm = l.lcm/__gcd(l.lcm, r.lcm)*r.lcm;
        return ret;
    }

    segtree() {}
    segtree(int n_, dat_type dd, ll ld) : dat_d(dd), lazy_d(ld) {
        n = 1; while(n < n_) n *= 2;
        dat.assign(n*2-1, dat_d);
        lazy.assign(n*2-1, lazy_d);
    }

    void eval(int k, int l, int r) {
        if(lazy[k] == lazy_d) return;
        dat[k].sum = lazy[k] * (r-l);
        dat[k].max = dat[k].min = lazy[k];
        if(k*2+1 < n*2-1) {
            lazy[2*k+1] = lazy[k];
            lazy[2*k+2] = lazy[k];
        }
        lazy[k] = lazy_d;
    }

    void update(int a, int b, ll x, int k, int l, int r) {
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] = x;
            eval(k, l, r);
            return;
        }
        int m = (l+r)>>1;
        update(a, b, x, 2*k+1, l, m);
        update(a, b, x, 2*k+2, m, r);
        dat[k] = merge_dat(dat[2*k+1], dat[2*k+2]);
    }
    void update(int a, int b, ll x) { update(a, b, x, 0, 0, n); }

    void update_gcd(int a, int b, ll x, int k, int l, int r) {
        eval(k, l, r);
        if(b <= l || r <= a || dat[k].lcm%x==0) return;
        if(a <= l && r <= b && dat[k].max == dat[k].min) {
            lazy[k] = __gcd(dat[k].max, x);
            eval(k, l, r);
            return;
        }
        int m = (l+r)>>1;
        update_gcd(a, b, x, 2*k+1, l, m);
        update_gcd(a, b, x, 2*k+2, m, r);
        dat[k] = merge_dat(dat[2*k+1], dat[2*k+2]);
    }
    void update_gcd(int a, int b, ll x) { update_gcd(a, b, x, 0, 0, n); }

    dat_type query(int a, int b, int k, int l, int r) {
        eval(k, l, r);
        if(b <= l || r <= a) return dat_d;
        if(a <= l && r <= b) return dat[k];

        int m=(l+r)>>1;
        dat_type vl = query(a, b, 2*k+1, l, m);
        dat_type vr = query(a, b, 2*k+2, m, r);
        return merge_dat(vl, vr);
    }
    dat_type query(int a, int b) { return query(a, b, 0, 0, n); }
};

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];

    struct node {
        ll sum, max, min, lcm;
        node() : sum(0), max(-LLINF), min(LLINF), lcm(1) {}
        node(ll v) : sum(v), max(v), min(v), lcm(v) {}
    };
    segtree<node, ll> seg(n, node(), -1);
    REP(i, n) seg.update(i, i+1, a[i]);

    while(q--) {
        ll type;
        cin >> type;
        if(type == 1) {
            ll l, r, x;
            cin >> l >> r >> x;
            l--, r--;
            seg.update(l, r+1, x);
        } else if(type == 2) {
            ll l, r, x;
            cin >> l >> r >> x;
            l--, r--;
            seg.update_gcd(l, r+1, x);
        } else if(type == 3) {
            ll l, r;
            cin >> l >> r;
            l--, r--;
            cout << seg.query(l, r+1).max << endl;
        } else if(type == 4) {
            ll l, r;
            cin >> l >> r;
            l--, r--;
            cout << seg.query(l, r+1).sum << endl;
        }

        // FOR(j, 444, 488) cout << seg.query(j, j+1).max << " ";
        // cout << endl;
    }

    return 0;
}
0