結果

問題 No.749 クエリ全部盛り
ユーザー parukiparuki
提出日時 2018-11-27 07:49:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 576 ms / 3,000 ms
コード長 6,607 bytes
コンパイル時間 2,129 ms
コンパイル使用メモリ 176,664 KB
実行使用メモリ 75,660 KB
最終ジャッジ日時 2023-09-05 05:30:21
合計ジャッジ時間 7,467 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 26 ms
4,380 KB
testcase_11 AC 26 ms
4,380 KB
testcase_12 AC 25 ms
4,380 KB
testcase_13 AC 25 ms
4,380 KB
testcase_14 AC 26 ms
4,384 KB
testcase_15 AC 574 ms
75,656 KB
testcase_16 AC 573 ms
75,660 KB
testcase_17 AC 576 ms
75,596 KB
testcase_18 AC 561 ms
75,580 KB
testcase_19 AC 568 ms
75,624 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;
}

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

const P EP = P{ 0,0,1 };

struct Q {
    mint p, q, r;
};

bool operator==(Q qa, Q qb) {
    return qa.p.toi() == qb.p.toi()
        &&qa.q.toi() == qb.q.toi()
        &&qa.r.toi() == qb.r.toi();
}
const Q EQ = Q{ 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 };
}

// qaのあとにqb 適用
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<P> value;
    vector<Q> lazy;
    LazySegTree(int n) {
        dataSize = 1;
        while (dataSize < n)dataSize *= 2;
        int treeSize = 2 * dataSize;
        value = vector<P>(treeSize, EP);
        lazy = vector<Q>(treeSize, EQ);
        // value = lazy = vector<long long>(treeSize);
    }

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

    void propagate(int index, int curL, int curR) {
        // 自分の値をlazyにもとづいて書き換えて自分のlazyをクリア。
        if (!(lazy[index]==EQ)) {
            int left = index * 2, right = index * 2 + 1;
            value[index] = g(value[index], lazy[index]);
            // 子のlazyを書き換える。
            if (curR - curL > 1) {
                lazy[left] = h(lazy[left], lazy[index]);
                lazy[right] = h(lazy[right], lazy[index]);
            }
            lazy[index] = EQ;
        }
    }

    // [curL, curR) を評価する
    void update(int index, int curL, int curR, int givenL, int givenR, Q x) {
        // 範囲外であろうとpropagateは必ず呼ぶ。そうでないと、親がうまく評価されない
        propagate(index, curL, curR);

        // 範囲外
        if (curR <= givenL || givenR <= curL)return;

        if (givenL <= curL && curR <= givenR) {
            // 直接書き換えないでindexのlazyを書き換えてpropagateを呼ぶ
            lazy[index] = 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] = f(value[index * 2], value[index * 2 + 1]);
        }
    }

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

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

    P query(int index, int curL, int curR, int givenL, int givenR) {
        // 範囲外
        if (curR <= givenL || givenR <= curL)return EP;

        propagate(index, curL, curR);

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

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

    // フィボナッチ数列は事前に計算
    vector<P> a(N, 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)

        // rep(i, N)cout << tree.query(i, i + 1).a << ' ';
        // cout << endl;
        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, Q{ 0,0,k });
        } else if (q == 2) {
            tree.update(l, r, Q{ 1,0,k });
        } else if (q == 3) {
            tree.update(l, r, Q{ k,0,0 });
        } else { // q==4
            tree.update(l, r, Q{ 1,k,0 });
        }
    }
}

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