結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー yosupotyosupot
提出日時 2019-09-06 22:11:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 568 ms / 5,000 ms
コード長 4,210 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 209,892 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2024-09-22 19:34:16
合計ジャッジ時間 12,102 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 513 ms
13,440 KB
testcase_12 AC 505 ms
14,080 KB
testcase_13 AC 353 ms
12,928 KB
testcase_14 AC 509 ms
15,744 KB
testcase_15 AC 545 ms
15,872 KB
testcase_16 AC 566 ms
15,872 KB
testcase_17 AC 509 ms
16,512 KB
testcase_18 AC 527 ms
16,512 KB
testcase_19 AC 160 ms
15,872 KB
testcase_20 AC 167 ms
16,512 KB
testcase_21 AC 159 ms
15,360 KB
testcase_22 AC 169 ms
16,128 KB
testcase_23 AC 164 ms
15,488 KB
testcase_24 AC 141 ms
15,744 KB
testcase_25 AC 154 ms
16,512 KB
testcase_26 AC 142 ms
15,360 KB
testcase_27 AC 150 ms
16,128 KB
testcase_28 AC 153 ms
15,488 KB
testcase_29 AC 497 ms
15,616 KB
testcase_30 AC 533 ms
15,744 KB
testcase_31 AC 568 ms
15,872 KB
testcase_32 AC 60 ms
16,640 KB
testcase_33 AC 118 ms
15,744 KB
testcase_34 AC 122 ms
16,000 KB
testcase_35 AC 117 ms
15,872 KB
testcase_36 AC 121 ms
16,128 KB
testcase_37 AC 122 ms
16,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL
struct PrettyOS {
    ostream& os;
    bool first;
    template <class T> auto operator<<(T&& x) {
        if (!first) os << ", ";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

ll gcd(ll a, ll b) {
    while (b) {
        ll c = b;
        b = a % b;
        a = c;
    }
    return a;
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

struct Node {
    using NP = Node*;
    NP l, r;
    ll sm, ma, sm_lcm, lz = -1;
    bool same = true;
    int sz;
    void update() {
        assert(lz == -1);
        sm = l->sm + r->sm;
        ma = max(l->ma, r->ma);
        sm_lcm = min(TEN(9) + 1, lcm(l->sm_lcm, r->sm_lcm));
        same = l->same && r->same;
        if (l->ma != r->ma) same = false;
    }
    void lzdata(ll x) {
        sm = x * sz;
        ma = sm_lcm = x;
        same = true;
        lz = x;
    }
    void push() {
        if (lz != -1) {
            l->lzdata(lz);
            r->lzdata(lz);
            lz = -1;
        }
    }
    Node(const V<ll>& v, int _sz, int off) : sz(_sz) {
        if (sz == 1) {
            sm = ma = sm_lcm = v[off];
            return;
        }
        l = new Node(v, sz / 2, off);
        r = new Node(v, sz - sz / 2, off + sz / 2);
        update();
    }

    void set(int a, int b, ll x) {
        if (b <= 0 || sz <= a) return;
        if (a <= 0 && sz <= b) {
            lzdata(x);
            return;
        }
        push();
        l->set(a, b, x);
        r->set(a - sz / 2, b - sz / 2, x);
        update();
    }

    void down(int a, int b, ll x) {
        if (b <= 0 || sz <= a) return;
        if (x % sm_lcm == 0) return;
        if (a <= 0 && sz <= b && same) {
            lzdata(gcd(ma, x));
            return;
        }
        push();
        l->down(a, b, x);
        r->down(a - sz / 2, b - sz / 2, x);
        update();
    }

    pair<ll, ll> que(int a, int b) {
        if (b <= 0 || sz <= a) return {-1, 0};
        if (a <= 0 && sz <= b) return {ma, sm};
        push();
        auto lq = l->que(a, b);
        auto rq = r->que(a - sz / 2, b - sz / 2);
        return {max(lq.first, rq.first), lq.second + rq.second};
    }

    ~Node() {
        if (sz == 1) return;
        delete l;
        delete r;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;
    V<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    auto tr = new Node(a, n, 0);

    for (int ph = 0; ph < q; ph++) {
        int ty, l, r;
        cin >> ty >> l >> r; l--;

        if (ty == 1) {
            ll x;
            cin >> x;        
            tr->set(l, r, x);
        } else if (ty == 2) {
            ll x;
            cin >> x;
            tr->down(l, r, x);
        } else if (ty == 3) {
            auto q = tr->que(l, r);
            cout << q.first << "\n";
        } else {
            auto q = tr->que(l, r);
            cout << q.second << "\n";
        }

        /*for (int i = 0; i < n; i++) {
            cout << tr->que(i, i + 1).first << " ";
        }
        cout << endl;*/
    }
    delete tr;
    return 0;
}
0