competitive-programing

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Astral-23/competitive-programing

:heavy_check_mark: verify/Datastructure_compress.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_composite_large_array"
#include "../Utility/template.hpp"
#include "../Utility/modint.hpp"
#include "../Datastructure/compress.hpp"
#include "../Datastructure/segtree.hpp"

using mint = modint998244353;


struct S{
    mint a, b;
    S(){}
    S(mint d, mint t) : a(d), b(t){}
};

S op(S l, S r) {
	S res;
    res.a = l.a * r.a;
    res.b = r.a * l.b + r.b;
    return res;
}

S e() {
	return S(1, 0);
}

int main() {
    ll n, q;
    cin >> n >> q;
    compress<int> cm;
    using P = array<ll, 4>;
    vector<P> qs;
    rep(qi, 0, q) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        if (a == 0) {
            b--;
        } else {
            b--, c--;
        }
        qs.push_back({a, b, c, d});
        cm.add(b);
    }
    cm.build();

    segtree<S, op, e> seg(cm.size());
    rep(qi, 0, q) {
        auto [t, p, c, d] = qs[qi];

        if (t == 0) {
            p = cm.get(p);
            seg.set(p, {c, d});
        } else {
            p = cm(p);
            c = cm(c);
            auto [a, b] = seg.prod(p, c);
            cout << a * d + b << '\n';
        }
    }
}
#line 1 "verify/Datastructure_compress.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_composite_large_array"
#line 1 "Utility/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)

#define TT template <typename T>
TT using vec = vector<T>;
template <class T1, class T2> bool chmin(T1 &x, T2 y) {
    return x > y ? (x = y, true) : false;
}
template <class T1, class T2> bool chmax(T1 &x, T2 y) {
    return x < y ? (x = y, true) : false;
}
struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

/*
@brief verify用テンプレート
*/
#line 1 "Utility/modint.hpp"

// 動的mod : template<int mod> を消して、上の方で変数modを宣言
template <uint32_t mod> struct modint {
    using mm = modint;
    uint32_t x;
    modint() : x(0) {}
    TT modint(T a = 0) : x((ll(a) % mod + mod)) {
        if (x >= mod) x -= mod;
    }

    friend mm operator+(mm a, mm b) {
        a.x += b.x;
        if (a.x >= mod) a.x -= mod;
        return a;
    }
    friend mm operator-(mm a, mm b) {
        a.x -= b.x;
        if (a.x >= mod) a.x += mod;
        return a;
    }

    mm operator-() const { return mod - x; }

    //+と-だけで十分な場合、以下は省略して良いです。

    friend mm operator*(mm a, mm b) { return (uint64_t)(a.x) * b.x; }
    friend mm operator/(mm a, mm b) { return a * b.inv(); }
    friend mm &operator+=(mm &a, mm b) { return a = a + b; }
    friend mm &operator-=(mm &a, mm b) { return a = a - b; }
    friend mm &operator*=(mm &a, mm b) { return a = a * b; }
    friend mm &operator/=(mm &a, mm b) { return a = a * b.inv(); }

    mm inv() const {
        assert(x != 0);
        return pow(mod - 2);
    }
    mm pow(ll y) const {
        mm res = 1;
        mm v = *this;
        while (y) {
            if (y & 1) res *= v;
            v *= v;
            y /= 2;
        }
        return res;
    }

    friend istream &operator>>(istream &is, mm &a) {
        ll t;
        cin >> t;
        a = mm(t);
        return is;
    }

    friend ostream &operator<<(ostream &os, mm a) { return os << a.x; }

    bool operator==(mm a) { return x == a.x; }
    bool operator!=(mm a) { return x != a.x; }

    bool operator<(const mm &a) const { return x < a.x; }
};
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1'000'000'007>;
/*
@brief modint
*/
#line 1 "Datastructure/compress.hpp"
template <typename T> struct compress {
    vector<T> vs;
    bool built = false;
    compress(vector<T> const &vs = {}) : vs(vs) {
    }
    void add(T const &v) {
        assert(built == false);
        vs.push_back(v);
    }
    void build() {
        assert(built == false);
        built = true;
        sort(vs.begin(), vs.end());
        vs.erase(unique(vs.begin(), vs.end()), vs.end());
    }

    ll operator()(T const &v) const {
        assert(built);
        return lower_bound(vs.begin(), vs.end(), v) - vs.begin();
    }

    ll get(T const &v) const {
        assert(built);
        ll ret = (*this)(v);
        assert(0 <= ret && ret < ll(vs.size()));
        assert(vs[ret] == v);
        return ret;
    }

    ll size() const {
        assert(built);
        return vs.size();
    }
};
#line 1 "Datastructure/segtree.hpp"
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
    int n;
    int sz;
    vector<S> d;

    segtree(int n) : segtree(vector<S>(n, e())) {}

    segtree(const vector<S> &v) : n((int)v.size()), sz(1) {
        while(sz < n) sz <<= 1;
        d.resize(2*sz, e());
        rep(i, 0, n) {
            d[sz+i] = v[i];
        }
        rrep(i, 1, sz) d[i] = op(d[i<<1], d[i<<1|1]);
    }

    void set(int p, S x) {
        assert(0 <= p && p < n);
        p += sz;
        d[p] = x;
        while(p > 1) {
            p >>= 1;
            d[p] = op(d[p<<1], d[p<<1|1]);
        }
    }

    S get(int p) const {
        assert(0 <= p && p < n);
        return d[p + sz];
    }

    S prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= n);
        S sml = e(), smr = e();
        l += sz;
        r += sz;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= n);
        assert(f(e()));
        if (l == n) return n;
        l += sz;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < sz) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - sz;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= n);
        assert(f(e()));
        if (r == 0) return 0;
        r += sz;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < sz) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - sz;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
    
};
/*
@brief segtree
@docs doc/segtree.md
*/
#line 6 "verify/Datastructure_compress.test.cpp"

using mint = modint998244353;


struct S{
    mint a, b;
    S(){}
    S(mint d, mint t) : a(d), b(t){}
};

S op(S l, S r) {
	S res;
    res.a = l.a * r.a;
    res.b = r.a * l.b + r.b;
    return res;
}

S e() {
	return S(1, 0);
}

int main() {
    ll n, q;
    cin >> n >> q;
    compress<int> cm;
    using P = array<ll, 4>;
    vector<P> qs;
    rep(qi, 0, q) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        if (a == 0) {
            b--;
        } else {
            b--, c--;
        }
        qs.push_back({a, b, c, d});
        cm.add(b);
    }
    cm.build();

    segtree<S, op, e> seg(cm.size());
    rep(qi, 0, q) {
        auto [t, p, c, d] = qs[qi];

        if (t == 0) {
            p = cm.get(p);
            seg.set(p, {c, d});
        } else {
            p = cm(p);
            c = cm(c);
            auto [a, b] = seg.prod(p, c);
            cout << a * d + b << '\n';
        }
    }
}
Back to top page