結果
| 問題 |
No.426 往復漸化式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-13 08:12:01 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1,158 ms / 5,000 ms |
| コード長 | 3,494 bytes |
| コンパイル時間 | 1,239 ms |
| コンパイル使用メモリ | 86,116 KB |
| 実行使用メモリ | 181,632 KB |
| 最終ジャッジ日時 | 2024-11-22 03:38:07 |
| 合計ジャッジ時間 | 20,127 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 22 |
ソースコード
#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
#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<ll> VL;
const ll mod = 1e9 + 7;
template<class I, class BiOp>
class SegTree {
int n;
std::vector<I> dat;
BiOp op;
I e;
public:
SegTree(BiOp op, I e) : op(op), e(e), n(1 << 17) {
dat.resize(2 * n);
for (int i = 0; i < 2 * n - 1; i++) {
dat[i] = e;
}
}
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_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]);
}
}
}
I querySub(int a, int b, int k, int l, int r) const {
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);
}
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) {
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) {
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;
}
struct dat{VVL a, b, s;};
dat elem_mul(const dat &abs1, const dat &abs2) {
VVL a = mul(abs2.a, abs1.a);
VVL b = mul(abs1.b, abs2.b);
VVL s = add(abs1.s, mul(mul(abs1.b, abs2.s), abs1.a));
return dat{a, b, s};
}
VVL mat_s(int i) {
VVL s(2, VL(3, 0));
REP(k,0,6)s[k/3][k%3]=6*i+k;
return s;
}
int main(void){
int n, q;
cin >> n;
VVL a(3, {0}), b(2, {0});
REP(i, 0, 3) {
cin >> a[i][0];
}
REP(i, 0, 2) {
cin >> b[i][0];
}
VVL u3(3), u2(2);
REP(i, 0, 3) {
VL t(3);
t[i] = 1;
u3[i] = t;
}
REP(i, 0, 2) {
VL t(2);
t[i] = 1;
u2[i] = t;
}
SegTree<dat, dat (*) (const dat &, const dat &) > st(elem_mul, dat{u3, u2, VVL(2, VL(3))});
{
vector<dat> ary(n + 1);
REP(i, 0, n + 1) {
ary[i] = dat{u3, u2, mat_s(i)};
}
st.update_all(&ary[0], n + 1);
}
cin >> q;
REP(z, 0, q) {
string qty;
int i;
cin >> qty >> i;
if (qty == "a") {
REP(k, 0, 3) {
REP(j, 0, 3) {
cin >> u3[k][j];
}
}
dat cur = st.query(i, i);
st.update(i, dat{u3, cur.b, mat_s(i)});
}
if (qty == "b") {
REP(k, 0, 2) {
REP(j, 0, 2) {
cin >> u2[k][j];
}
}
dat cur = st.query(i, i);
st.update(i, dat{cur.a, u2, mat_s(i)});
}
if (qty == "ga") {
VVL res = mul(st.query(0, i - 1).a, a);
cout << res[0][0] << " " << res[1][0] << " " << res[2][0] << "\n";
}
if (qty == "gb") {
dat tmp = st.query(i + 1, n);
VVL res = add(mul(tmp.b, b), mul(tmp.s, mul(st.query(0, i).a, a)));
cout << res[0][0] << " " << res[1][0] << "\n";
}
}
}