結果

問題 No.749 クエリ全部盛り
ユーザー parukiparuki
提出日時 2018-11-28 07:42:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 594 ms / 3,000 ms
コード長 6,079 bytes
コンパイル時間 2,068 ms
コンパイル使用メモリ 178,468 KB
実行使用メモリ 75,812 KB
最終ジャッジ日時 2024-06-26 05:25:00
合計ジャッジ時間 6,665 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 26 ms
6,940 KB
testcase_11 AC 24 ms
6,944 KB
testcase_12 AC 25 ms
6,944 KB
testcase_13 AC 25 ms
6,944 KB
testcase_14 AC 24 ms
6,940 KB
testcase_15 AC 588 ms
75,664 KB
testcase_16 AC 594 ms
75,648 KB
testcase_17 AC 587 ms
75,812 KB
testcase_18 AC 568 ms
75,712 KB
testcase_19 AC 585 ms
75,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<int MOD>
class ModInt {
public:
    ModInt() :value(0) {}
    ModInt(long long val) :value((int)(val<0 ? MOD + val % MOD : val % MOD)) { }

    ModInt& operator+=(ModInt that) {
        value = value + that.value;
        if (value >= MOD)value -= MOD;
        return *this;
    }
    ModInt& operator-=(ModInt that) {
        value -= that.value;
        if (value<0)value += MOD;
        return *this;
    }
    ModInt& operator*=(ModInt that) {
        value = (int)((long long)value * that.value % MOD);
        return *this;
    }
    ModInt &operator/=(ModInt that) {
        return *this *= that.inverse();
    }
    ModInt operator+(ModInt that) const {
        return ModInt(*this) += that;
    }
    ModInt operator-(ModInt that) const {
        return ModInt(*this) -= that;
    }
    ModInt operator*(ModInt that) const {
        return ModInt(*this) *= that;
    }
    ModInt operator/(ModInt that) const {
        return ModInt(*this) /= that;
    }
    ModInt pow(long long k) const {
        ModInt n = *this, res = 1;
        while (k) {
            if (k & 1)res *= n;
            n *= n;
            k >>= 1;
        }
        return res;
    }
    ModInt inverse() const {
        long long a = value, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return ModInt(u);
    }
    int toi() const { return value; }

private:
    int value;
};
typedef ModInt<1000000007> mint;
ostream& operator<<(ostream& os, const mint& x) {
    os << x.toi();
    return os;
}

namespace Lz {
    struct P {
        mint a, f, c;
    };

    P EP = { 0,0,1 };

    struct Q {
        mint p, q, r;
        bool operator!=(Q x) {
            return p.toi() != x.p.toi() ||
                q.toi() != x.q.toi() ||
                r.toi() != x.r.toi();
        }
    };

    Q EQ = { 1,0,0 };

    P f(P pl, P pr) {
        return P{ pl.a + pr.a,pl.f + pr.f,pl.c + pr.c };
    }

    P g(P p, Q q) {
        return P{ p.a*q.p + p.f*q.q + p.c*q.r,p.f,p.c };
    }

    Q h(Q qa, Q qb) {
        return Q{ qa.p*qb.p, qa.q*qb.p + qb.q, qa.r*qb.p + qb.r };
    }
}

/*
遅延伝搬セグメント木
*/
struct LazySegTree {
    int dataSize;
    vector<Lz::P> value;
    vector<Lz::Q> lazy;

    LazySegTree(int n) : LazySegTree(vector<Lz::P>(n, Lz::EP)){}

    LazySegTree(vector<Lz::P> dat) {
        dataSize = 1;
        int n = (int)dat.size();
        while (dataSize < n)dataSize *= 2;
        int treeSize = 2 * dataSize;
        value = vector<Lz::P>(treeSize, Lz::EP);
        lazy = vector<Lz::Q>(treeSize, Lz::EQ);
        for (int i = 0; i < n; ++i) {
            value[dataSize+i] = dat[i];
        }
        for (int i = dataSize - 1; i >= 0; --i) {
            value[i] = Lz::f(value[i * 2], value[i * 2 + 1]);
        }
    }

    void propagate(int index, int curL, int curR) {
        if (lazy[index]!=Lz::EQ) {
            int left = index * 2, right = index * 2 + 1;
            value[index] = g(value[index], lazy[index]);
            if (curR - curL > 1) {
                lazy[left] = Lz::h(lazy[left], lazy[index]);
                lazy[right] = Lz::h(lazy[right], lazy[index]);
            }
            lazy[index] = Lz::EQ;
        }
    }

    void update(int index, int curL, int curR, int givenL, int givenR, Lz::Q x) {
        propagate(index, curL, curR);

        if (curR <= givenL || givenR <= curL)return;

        if (givenL <= curL && curR <= givenR) {
            lazy[index] = Lz::h(lazy[index], x);
            propagate(index, curL, curR);
        } else {
            int mid = (curL + curR) / 2;
            update(index * 2, curL, mid, givenL, givenR, x);
            update(index * 2 + 1, mid, curR, givenL, givenR, x);
            value[index] = Lz::f(value[index * 2], value[index * 2 + 1]);
        }
    }

    void update(int l, int r, Lz::Q x) {
        update(1, 0, dataSize, l, r, x);
    }

    Lz::P query(int l, int r) {
        return query(1, 0, dataSize, l, r);
    }

    Lz::P query(int index, int curL, int curR, int givenL, int givenR) {
        if (curR <= givenL || givenR <= curL)return Lz::EP;

        propagate(index, curL, curR);

        if (givenL <= curL && curR <= givenR) {
            return value[index];
        } else {
            int mid = (curL + curR) / 2;
            Lz::P resL = query(index * 2, curL, mid, givenL, givenR);
            Lz::P resR = query(index * 2 + 1, mid, curR, givenL, givenR);
            return Lz::f(resL, resR);
        }
    }
};

void solve() {
    int N, M;
    cin >> N >> M;

    // フィボナッチ数列は事前に計算
    vector<Lz::P> a(N, Lz::EP);
    a[1].f = 1;
    for (int i = 2; i < N; ++i)a[i].f = a[i - 2].f + a[i - 1].f;

    LazySegTree tree(a);

    rep(ITER, M) {
        int q, l, r, k;
        cin >> q >> l >> r >> k;
        r++; // [l, r)
        
        if (q == 0) {
            mint ans = tree.query(l, r).a * k;
            cout << ans << endl;
        } else if (q == 1) {
            // (p,q,r)=(0,0,k)
            tree.update(l, r, Lz::Q{ 0,0,k });
        } else if (q == 2) {
            tree.update(l, r, Lz::Q{ 1,0,k });
        } else if (q == 3) {
            tree.update(l, r, Lz::Q{ k,0,0 });
        } else { // q==4
            tree.update(l, r, Lz::Q{ 1,k,0 });
        }
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0