結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー ferinferin
提出日時 2019-09-07 00:16:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,049 ms / 5,000 ms
コード長 5,232 bytes
コンパイル時間 2,003 ms
コンパイル使用メモリ 175,460 KB
実行使用メモリ 14,384 KB
最終ジャッジ日時 2023-10-24 02:38:54
合計ジャッジ時間 23,618 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 4 ms
4,372 KB
testcase_02 AC 5 ms
4,372 KB
testcase_03 AC 5 ms
4,372 KB
testcase_04 AC 5 ms
4,372 KB
testcase_05 AC 4 ms
4,372 KB
testcase_06 AC 4 ms
4,372 KB
testcase_07 AC 5 ms
4,372 KB
testcase_08 AC 6 ms
4,372 KB
testcase_09 AC 5 ms
4,372 KB
testcase_10 AC 5 ms
4,372 KB
testcase_11 AC 923 ms
14,120 KB
testcase_12 AC 927 ms
14,120 KB
testcase_13 AC 716 ms
14,120 KB
testcase_14 AC 970 ms
14,120 KB
testcase_15 AC 1,011 ms
14,120 KB
testcase_16 AC 1,049 ms
14,384 KB
testcase_17 AC 1,040 ms
14,384 KB
testcase_18 AC 1,035 ms
14,384 KB
testcase_19 AC 719 ms
14,120 KB
testcase_20 AC 763 ms
14,384 KB
testcase_21 AC 714 ms
14,120 KB
testcase_22 AC 738 ms
14,384 KB
testcase_23 AC 727 ms
14,120 KB
testcase_24 AC 656 ms
14,120 KB
testcase_25 AC 688 ms
14,384 KB
testcase_26 AC 652 ms
14,120 KB
testcase_27 AC 675 ms
14,384 KB
testcase_28 AC 668 ms
14,120 KB
testcase_29 AC 951 ms
14,120 KB
testcase_30 AC 992 ms
14,120 KB
testcase_31 AC 1,033 ms
14,384 KB
testcase_32 AC 166 ms
14,384 KB
testcase_33 AC 285 ms
14,120 KB
testcase_34 AC 303 ms
14,384 KB
testcase_35 AC 290 ms
14,120 KB
testcase_36 AC 296 ms
14,384 KB
testcase_37 AC 288 ms
14,384 KB
権限があれば一括ダウンロードができます

ソースコード

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);
        // if(l.lcm/__gcd(l.lcm,r.lcm) >= 1.0*LLINF/r.lcm) ret.lcm = LLINF;
        //else
        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 = dat[k].lcm = 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 || x%dat[k].lcm==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;
        }
    }

    return 0;
}
0