結果

問題 No.2395 区間二次変換一点取得
ユーザー erbowlerbowl
提出日時 2023-07-30 19:30:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 4,095 bytes
コンパイル時間 2,089 ms
コンパイル使用メモリ 210,988 KB
実行使用メモリ 17,664 KB
最終ジャッジ日時 2024-04-17 20:33:18
合計ジャッジ時間 6,952 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 5 ms
6,940 KB
testcase_12 AC 34 ms
6,940 KB
testcase_13 AC 380 ms
17,488 KB
testcase_14 AC 384 ms
17,664 KB
testcase_15 AC 376 ms
17,536 KB
testcase_16 AC 404 ms
17,664 KB
testcase_17 AC 373 ms
17,664 KB
testcase_18 AC 345 ms
17,664 KB
testcase_19 AC 326 ms
17,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
typedef long double ld;

#include <bits/stdc++.h>
using namespace std;
#define int long long

// Segment Tree
template<class Monoid, class Action> struct SegTree {
    using FuncMonoid = function< Monoid(Monoid, Monoid) >;
    using FuncAction = function< void(Monoid&, Action) >;
    using FuncComposition = function< void(Action&, Action) >;
    FuncMonoid FM;
    FuncAction FA;
    FuncComposition FC;
    Monoid IDENTITY_MONOID;
    Action IDENTITY_LAZY;
    int SIZE, HEIGHT;
    vector<Monoid> dat;
    vector<Action> lazy;
    
    SegTree() {}
    SegTree(int n, const FuncMonoid fm, const FuncAction fa,
            const FuncComposition fc,
            const Monoid &identity_monoid, const Action &identity_lazy)
    : FM(fm), FA(fa), FC(fc), 
      IDENTITY_MONOID(identity_monoid), IDENTITY_LAZY(identity_lazy) {
        SIZE = 1, HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, IDENTITY_MONOID);
        lazy.assign(SIZE * 2, IDENTITY_LAZY);
    }
    void init(int n, const FuncMonoid fm, const FuncAction fa,
              const FuncComposition fc,
              const Monoid &identity_monoid, const Action &identity_lazy) {
        FM = fm, FA = fa, FC = fc;
        IDENTITY_MONOID = identity_monoid, IDENTITY_LAZY = identity_lazy;
        SIZE = 1, HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, IDENTITY_MONOID);
        lazy.assign(SIZE * 2, IDENTITY_LAZY);
    }
    
    // set, a is 0-indexed
    void set(int a, const Monoid &v) { dat[a + SIZE] = v; }
    void build() {
        for (int k = SIZE - 1; k > 0; --k)
            dat[k] = FM(dat[k*2], dat[k*2+1]);
    }
    
    // update [a, b)
    inline void evaluate(int k) {
        if (lazy[k] == IDENTITY_LAZY) return;
        if (k < SIZE) FC(lazy[k*2], lazy[k]), FC(lazy[k*2+1], lazy[k]);
        FA(dat[k], lazy[k]);
        lazy[k] = IDENTITY_LAZY;
    }
    inline void update(int a, int b, const Action &v, int k, int l, int r) {
        evaluate(k);
        if (a <= l && r <= b) FC(lazy[k], v), evaluate(k);
        else if (a < r && l < b) {
            update(a, b, v, k*2, l, (l+r)>>1);
            update(a, b, v, k*2+1, (l+r)>>1, r);
            dat[k] = FM(dat[k*2], dat[k*2+1]);
        }
    }
    inline void update(int a, int b, const Action &v) { 
        update(a, b, v, 1, 0, SIZE);
    }
    
    // get [a, b)
    inline Monoid get(int a, int b, int k, int l, int r) {
        evaluate(k);
        if (a <= l && r <= b)
            return dat[k];
        else if (a < r && l < b)
            return FM(get(a, b, k*2, l, (l+r)>>1), 
                      get(a, b, k*2+1, (l+r)>>1, r));
        else
            return IDENTITY_MONOID;
    }
    inline Monoid get(int a, int b) { 
        return get(a, b, 1, 0, SIZE);
    }
    inline Monoid operator [] (int a) {
        return get(a, a + 1);
    }
    
    // debug
    void print() {
        for (int i = 0; i < SIZE; ++i) {
            if (i) cout << ",";
            cout << get(i, i+1);
        }
        cout << endl;
    }
};

signed main(){
    ll n,b,q;
    std::cin >> n>>b>>q;
    
    
    vector<tuple<ll,ll,ll>> memo(q+1);
    vector<bool> done(q+1);
    memo[0] = {1%b,1%b,1%b};
    done[0] = true;
    
    
    function<tuple<ll,ll,ll>(ll)> dfs = [&](ll cnt){
        if(done[cnt]){
            return memo[cnt];
        }
        done[cnt]=true;
        auto [x,y,z] = dfs(cnt-1);
        tuple<ll,ll,ll> tmp = {(x+1)%b, (3*y+2*(x+1)*z)%b, 3*z%b};
        return memo[cnt] = tmp;
    };
    dfs(q);
    
    // define segtree
    auto fm = [&](ll a, ll b) {
        return a+b;
    };
    auto fa = [&](ll &a, int d) {
        if (d == 0) return;
        a += d;
    };
    auto fc = [&](int &d, int e) {
        d += e;
    };
    SegTree<ll, ll> seg(n+1, fm, fa, fc, 0, 0);
    for (int i = 0; i < q; i++) {
        ll l,m,r;
        std::cin >> l>>m>>r;
        seg.update(l,r+1, 1);
        ll val = seg.get(m, m+1);
        auto [x,y,z] = memo[val];
        std::cout << x<<" "<<y<<" "<<z << std::endl;    
    }
};
0