結果

問題 No.426 往復漸化式
ユーザー koba-e964
提出日時 2016-10-13 07:39:51
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3,451 ms / 5,000 ms
コード長 4,640 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 90,836 KB
実行使用メモリ 140,416 KB
最終ジャッジ日時 2024-11-22 03:30:41
合計ジャッジ時間 53,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <algorithm>
#include <cassert>
#include <iostream>
#include <list>
#include <string>
#include <utility>
#include <vector>
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;
/**
* Segment Tree. This data structure is useful for fast folding on intervals of an array
* whose elements are elements of monoid M. Note that constructing this tree requires the identity
* element of M and the operation of M.
* Header requirement: vector, algorithm
* Verified by AtCoder ABC017-D (http://abc017.contest.atcoder.jp/submissions/660402)
*/
template<class I, class BiOp = I (*) (I, I)>
class SegTree {
int n;
std::vector<I> dat;
BiOp op;
I e;
public:
SegTree(int n_, BiOp op, I e) : op(op), e(e) {
n = 1;
while (n < n_) { n *= 2; } // n is a power of 2
dat.resize(2 * n);
for (int i = 0; i < 2 * n - 1; i++) {
dat[i] = e;
}
}
/* ary[k] <- v */
void update(int k, I v) {
k += n - 1;
dat[k] = v;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = op(dat[2 * k + 1], dat[2 * k + 2]);
}
}
void update_array(int k, int len, const I *vals) {
for (int i = 0; i < len; ++i) {
update(k + i, vals[i]);
}
}
/*
Updates all elements. O(n)
*/
void update_all(const I *vals, int len) {
for (int k = 0; k < std::min(n, len); ++k) {
dat[k + n - 1] = vals[k];
}
for (int k = std::min(n, len); k < n; ++k) {
dat[k + n - 1] = e;
}
for (int b = n / 2; b >= 1; b /= 2) {
for (int k = 0; k < b; ++k) {
dat[k + b - 1] = op(dat[k * 2 + b * 2 - 1], dat[k * 2 + b * 2]);
}
}
}
/* l,r are for simplicity */
I querySub(int a, int b, int k, int l, int r) const {
// [a,b) and [l,r) intersects?
if (r <= a || b <= l) return e;
if (a <= l && r <= b) return dat[k];
I vl = querySub(a, b, 2 * k + 1, l, (l + r) / 2);
I vr = querySub(a, b, 2 * k + 2, (l + r) / 2, r);
return op(vl, vr);
}
/* [a, b] (note: inclusive) */
I query(int a, int b) const {
return querySub(a, b + 1, 0, 0, n);
}
};
typedef vector<VL> VVL;
VVL add(const VVL &a, const VVL &b) {
assert (a.size() == b.size());
int n = a.size();
int m = a[0].size();
VVL ret(n, VL(m, 0));
REP(i, 0, n) {
REP(j, 0, m) {
ret[i][j] = (a[i][j] + b[i][j]) % mod;
}
}
return ret;
}
VVL mul(const VVL &a, const VVL &b) {
assert (a[0].size() == b.size());
int n = a.size();
int m = b.size();
int l = b[0].size();
VVL ret(n, VL(l, 0));
REP(i, 0, n) {
REP(j, 0, m) {
REP(k, 0, l) {
ret[i][k] += a[i][j] * b[j][k];
ret[i][k] %= mod;
}
}
}
return ret;
}
vector<VVL> elem_mul(const vector<VVL> &abs1, const vector<VVL> &abs2) {
VVL a = mul(abs2[0], abs1[0]);
VVL b = mul(abs1[1], abs2[1]);
VVL s = add(abs1[2], mul(mul(abs1[1], abs2[2]), abs1[0]));
return {a, b, s};
}
VVL mat_s(int i) {
VVL s(2, VL(3, 0));
s[0][0] = 6 * i;
s[0][1] = 6 * i + 1;
s[0][2] = 6 * i + 2;
s[1][0] = 6 * i + 3;
s[1][1] = 6 * i + 4;
s[1][2] = 6 * i + 5;
return s;
}
int main(void){
int n, q;
cin >> n;
VVL a(3, VL(1)), b(2, VL(1));
REP(i, 0, 3) {
cin >> a[i][0];
}
REP(i, 0, 2) {
cin >> b[i][0];
}
VVL unit3(3), unit2(2), dummy23(2, VL(3));
REP(i, 0, 3) {
VL t(3);
t[i] = 1;
unit3[i] = t;
}
REP(i, 0, 2) {
VL t(2);
t[i] = 1;
unit2[i] = t;
}
SegTree<vector<VVL>, vector<VVL> (*) (const vector<VVL> &, const vector<VVL> &) > st(n + 1, elem_mul, vector<VVL>{unit3, unit2, dummy23});
REP(i, 0, n + 1) {
st.update(i, vector<VVL>{unit3, unit2, mat_s(i)});
}
cin >> q;
REP(loop_cnt, 0, q) {
string qty;
int i;
cin >> qty;
if (qty == "a") {
cin >> i;
REP(k, 0, 3) {
REP(j, 0, 3) {
cin >> unit3[k][j];
}
}
vector<VVL> cur = st.query(i, i);
st.update(i, vector<VVL>{unit3, cur[1], mat_s(i)});
}
if (qty == "b") {
cin >> i;
REP(k, 0, 2) {
REP(j, 0, 2) {
cin >> unit2[k][j];
}
}
vector<VVL> cur = st.query(i, i);
st.update(i, vector<VVL>{cur[0], unit2, mat_s(i)});
}
if (qty == "ga") {
cin >> i;
VVL res = mul(st.query(0, i - 1)[0], a);
cout << res[0][0] << " " << res[1][0] << " " << res[2][0] << endl;
}
if (qty == "gb") {
cin >> i;
vector<VVL> tmp = st.query(i + 1, n);
VVL res = add(mul(tmp[1], b),
mul(mul(tmp[2], st.query(0, i)[0]), a));
cout << res[0][0] << " " << res[1][0] << endl;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0