結果

問題 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  
実行時間 613 ms / 5,000 ms
コード長 4,210 bytes
コンパイル時間 2,648 ms
コンパイル使用メモリ 209,492 KB
実行使用メモリ 16,360 KB
最終ジャッジ日時 2023-10-24 02:37:09
合計ジャッジ時間 13,475 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 3 ms
4,368 KB
testcase_02 AC 4 ms
4,368 KB
testcase_03 AC 3 ms
4,368 KB
testcase_04 AC 3 ms
4,368 KB
testcase_05 AC 3 ms
4,368 KB
testcase_06 AC 3 ms
4,368 KB
testcase_07 AC 3 ms
4,368 KB
testcase_08 AC 3 ms
4,368 KB
testcase_09 AC 3 ms
4,368 KB
testcase_10 AC 4 ms
4,368 KB
testcase_11 AC 546 ms
13,456 KB
testcase_12 AC 533 ms
14,248 KB
testcase_13 AC 371 ms
12,928 KB
testcase_14 AC 528 ms
15,832 KB
testcase_15 AC 566 ms
15,832 KB
testcase_16 AC 599 ms
16,096 KB
testcase_17 AC 542 ms
16,360 KB
testcase_18 AC 548 ms
16,360 KB
testcase_19 AC 174 ms
15,832 KB
testcase_20 AC 187 ms
16,360 KB
testcase_21 AC 183 ms
15,304 KB
testcase_22 AC 182 ms
16,096 KB
testcase_23 AC 185 ms
15,568 KB
testcase_24 AC 161 ms
15,832 KB
testcase_25 AC 174 ms
16,360 KB
testcase_26 AC 161 ms
15,304 KB
testcase_27 AC 166 ms
16,096 KB
testcase_28 AC 184 ms
15,568 KB
testcase_29 AC 529 ms
15,832 KB
testcase_30 AC 563 ms
15,832 KB
testcase_31 AC 613 ms
16,096 KB
testcase_32 AC 60 ms
16,360 KB
testcase_33 AC 138 ms
15,832 KB
testcase_34 AC 150 ms
15,832 KB
testcase_35 AC 138 ms
15,832 KB
testcase_36 AC 137 ms
16,096 KB
testcase_37 AC 140 ms
16,096 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