結果

問題 No.426 往復漸化式
ユーザー koba-e964koba-e964
提出日時 2016-10-13 07:39:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3,376 ms / 5,000 ms
コード長 4,640 bytes
コンパイル時間 1,524 ms
コンパイル使用メモリ 99,468 KB
実行使用メモリ 140,476 KB
最終ジャッジ日時 2023-08-14 08:29:21
合計ジャッジ時間 53,550 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 5 ms
4,384 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 51 ms
4,528 KB
testcase_04 AC 51 ms
4,516 KB
testcase_05 AC 468 ms
20,144 KB
testcase_06 AC 472 ms
20,308 KB
testcase_07 AC 2,828 ms
140,228 KB
testcase_08 AC 2,855 ms
140,272 KB
testcase_09 AC 3,185 ms
140,176 KB
testcase_10 AC 3,169 ms
140,340 KB
testcase_11 AC 2,843 ms
140,168 KB
testcase_12 AC 3,192 ms
140,128 KB
testcase_13 AC 3,325 ms
140,336 KB
testcase_14 AC 3,203 ms
140,336 KB
testcase_15 AC 2,887 ms
140,128 KB
testcase_16 AC 3,330 ms
140,280 KB
testcase_17 AC 3,376 ms
140,320 KB
testcase_18 AC 3,306 ms
140,320 KB
testcase_19 AC 2,757 ms
140,476 KB
testcase_20 AC 3,023 ms
140,120 KB
testcase_21 AC 3,256 ms
140,212 KB
testcase_22 AC 3,027 ms
140,124 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    }
  }
}
0