結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー hitonanodehitonanode
提出日時 2019-10-26 18:00:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 8,845 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 180,392 KB
実行使用メモリ 25,648 KB
最終ジャッジ日時 2024-09-22 19:40:11
合計ジャッジ時間 25,916 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 5 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 962 ms
17,920 KB
testcase_12 AC 928 ms
18,108 KB
testcase_13 AC 664 ms
17,792 KB
testcase_14 AC 938 ms
18,432 KB
testcase_15 AC 984 ms
18,508 KB
testcase_16 AC 1,034 ms
18,496 KB
testcase_17 AC 1,339 ms
18,648 KB
testcase_18 AC 1,342 ms
18,804 KB
testcase_19 AC 478 ms
18,432 KB
testcase_20 AC 497 ms
18,632 KB
testcase_21 AC 504 ms
18,436 KB
testcase_22 AC 487 ms
18,560 KB
testcase_23 AC 511 ms
18,424 KB
testcase_24 AC 444 ms
18,432 KB
testcase_25 AC 458 ms
18,776 KB
testcase_26 AC 473 ms
18,348 KB
testcase_27 AC 451 ms
18,568 KB
testcase_28 AC 468 ms
18,432 KB
testcase_29 AC 919 ms
18,432 KB
testcase_30 AC 956 ms
18,552 KB
testcase_31 AC 1,023 ms
18,488 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
///// This part below is only for debug, not used /////
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
///// END /////
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds; // find_by_order(), order_of_key()
template<typename TK> using pbds_set = tree<TK, null_type, less<TK>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename TK, typename TV> using pbds_map = tree<TK, TV, less<TK>, rb_tree_tag, tree_order_statistics_node_update>;
*/

constexpr lint INF = 1e9 + 100;
struct Node
{
    int n;
    lint max_val;
    lint total_sum;
    lint lca;
    Node(int n = 0, int max_val = 0, lint total_sum = 0, lint lca = 1) : n(n), max_val(max_val), total_sum(total_sum), lca(lca) {}
};

constexpr int UPDATE = 1;
constexpr int GCD = 2;
constexpr int MAXVAL = 3;
constexpr int SUM = 4;

struct LazySegTree
{

    using T_NODE = Node;
    using T_DELAY = plint;
    // using T_QUERY = lint;
    using T_RETVAL = T_NODE;

    T_NODE zero_node = Node();
    T_DELAY zero_delay = plint(0, 0);
    T_RETVAL zero_retval = Node();

    int N;
    int head;
    vector<T_NODE> val;
    vector<T_DELAY> dval;

    void merge_node(int pos)
    {
        int l = pos * 2 + 1, r = pos * 2 + 2;
        // Calculate val[pos] from its children val[pos * 2 + 1], val[pos * 2 + 2]
        val[pos].n = val[l].n + val[r].n;
        val[pos].max_val = max(val[l].max_val, val[r].max_val);
        val[pos].total_sum = val[l].total_sum + val[r].total_sum;
        val[pos].lca = min(INF, val[l].lca * val[r].lca / __gcd(val[l].lca, val[r].lca));
    }

    void reflect_delay(int pos)
    {
        // Reflect delay information dval[pos] to val[pos]
        if (dval[pos].second == UPDATE)
        {
            val[pos] = Node(val[pos].n, dval[pos].first, dval[pos].first * val[pos].n, dval[pos].first);
        }
        if (dval[pos].second == GCD)
        {
            if (val[pos].lca < INF and dval[pos].first % val[pos].lca == 0) return;
            if (pos < head)
            {
                pushdown_dval(pos * 2 + 1, dval[pos]);
                resolve_dval(pos * 2 + 1);
                pushdown_dval(pos * 2 + 2, dval[pos]);
                resolve_dval(pos * 2 + 2);
                merge_node(pos);
            }
            else
            {
                lint x = val[pos].total_sum;
                lint xnew = __gcd(x, dval[pos].first);
                val[pos] = Node(1, xnew, xnew, xnew);
            }
        }
    }

    void pushdown_dval(int child_pos, const T_DELAY &d)
    {
        // operate d to dval[child_pos]
        if (d.second == UPDATE or dval[child_pos].second == 0) dval[child_pos] = d;
        else if (d.second == GCD)
        {
            dval[child_pos].first = __gcd(dval[child_pos].first, d.first);
        }
    }

    T_RETVAL merge_retval(const T_RETVAL &l, const T_RETVAL &r)
    {
        T_RETVAL ret;
        ret.n = l.n + r.n;
        ret.max_val = max(l.max_val, r.max_val);
        ret.total_sum = l.total_sum + r.total_sum;
        return ret;
    }

    ////// general description //////
    LazySegTree(const vector<T_NODE> &val_init) : N(val_init.size())
    {
        int N_tmp = 1;
        while (N_tmp < N) N_tmp <<= 1;
        val.assign(N_tmp * 2, zero_node);
        dval.assign(N_tmp * 2, zero_delay);
        head = N_tmp - 1;
        copy(val_init.begin(), val_init.end(), val.begin() + head);
        for (int pos = head - 1; pos >= 0; pos--)
        {
            merge_node(pos);
        }
    }

    void resolve_dval(int pos) { // posで遅延を解消して子孫に押し付ける
        reflect_delay(pos);
        if (pos < head and dval[pos] != zero_delay)
        {
            pushdown_dval(pos * 2 + 1, dval[pos]);
            pushdown_dval(pos * 2 + 2, dval[pos]);
            dval[pos] = zero_delay;
        }
    }

    void update(int begin, int end, T_DELAY delay) { update(begin, end, delay, 0, 0, head + 1); }
    void update(int begin, int end, T_DELAY delay, int pos, int l, int r)
    {
        // Operate delay to the node pos
        // After this, dval[pos] must be zero
        if (begin <= l and r <= end) { // Update whole [l, r) by delay
            pushdown_dval(pos, delay);
            resolve_dval(pos);
        }
        else if (begin < r and l < end) { // Update somewhere in [l, r)
            resolve_dval(pos);
            update(begin, end, delay, pos * 2 + 1, l, (l + r) / 2);
            update(begin, end, delay, pos * 2 + 2, (l + r) / 2, r);
            merge_node(pos);
        }
        else resolve_dval(pos);
    }

    T_RETVAL getvalue(int begin, int end) { return getvalue(begin, end, 0, 0, head + 1); }
    T_RETVAL getvalue(int begin, int end, int pos, int l, int r)
    {
        resolve_dval(pos);
        if (begin <= l and r <= end) return val[pos];
        else if (begin < r and l < end) {
            T_RETVAL vl = getvalue(begin, end, pos * 2 + 1, l, (l + r) / 2);
            T_RETVAL vr = getvalue(begin, end, pos * 2 + 2, (l + r) / 2, r);
            return merge_retval(vl, vr);
        }
        else return zero_retval;
    }
};

int main()
{
    int N, Q;
    cin >> N >> Q;
    vector<Node> A(N);
    REP(i, N)
    {
        int a;
        cin >> a;
        A[i] = Node(1, a, a, a);
    }
    LazySegTree st(A);
    REP(_, Q)
    {
        int q, l, r;
        cin >> q >> l >> r;
        l--;
        if (q == UPDATE)
        {
            lint x;
            cin >> x;
            st.update(l, r, plint(x, UPDATE));
        }
        if (q == GCD)
        {
            lint x;
            cin >> x;
            st.update(l, r, plint(x, GCD));
        }
        if (q == MAXVAL)
        {
            cout << st.getvalue(l, r).max_val << endl;
        }
        if (q == SUM)
        {
            cout << st.getvalue(l, r).total_sum << endl;
        }
    }
}
0