結果
問題 | No.510 二次漸化式 |
ユーザー |
|
提出日時 | 2017-04-29 12:30:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 639 ms / 3,000 ms |
コード長 | 4,058 bytes |
コンパイル時間 | 1,483 ms |
コンパイル使用メモリ | 120,004 KB |
実行使用メモリ | 88,832 KB |
最終ジャッジ日時 | 2024-09-13 19:13:19 |
合計ジャッジ時間 | 15,341 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 34 |
ソースコード
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> using namespace std; const int MOD = 1000000007; // 行列の積 template <class T> vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y) { int a = x.size(); int b = x[0].size(); int c = y[0].size(); vector<vector<T> > z(a, vector<T>(c, 0)); for(int i=0; i<a; ++i){ for(int j=0; j<c; ++j){ for(int k=0; k<b; ++k){ z[i][j] += x[i][k] * y[k][j]; z[i][j] %= MOD; } } } return z; } // セグメント木 class SegmentTree { private: typedef vector<vector<long long> > T1; typedef vector<vector<long long> > T2; // データの初期値、以下の条件を満たすこと // uniteData(v, INIT_DATA) == v static const T1 INIT_DATA; // 前回の値がprevである要素に対して、 // パラメータxを用いた更新処理を適用した後の計算結果を返す T1 updateData(T1 prev, T2 x){ return x; } // 2つの区間の計算結果v1,v2に対して、 // その2つの区間を統合した区間における計算結果を返す T1 uniteData(T1 v1, T1 v2){ return matrixProduct(v2, v1); } int n; vector<T1> data; void updateTree(int a, int k, int l, int r, T2 x){ if(a == l && a == r){ data[k] = updateData(data[k], x); } else if(l <= a && a <= r){ updateTree(a, k*2+1, l, (l+r)/2, x); updateTree(a, k*2+2, (l+r+1)/2, r, x); data[k] = uniteData(data[k*2+1], data[k*2+2]); } } T1 getValue(int a, int b, int k, int l, int r){ if(a <= l && r <= b){ return data[k]; } else if(a <= r && l <= b){ T1 v1 = getValue(a, b, k*2+1, l, (l+r)/2); T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r); return uniteData(v1, v2); } else{ return INIT_DATA; } } public: SegmentTree(int n0){ n = 1; while(n < n0) n *= 2; data.assign(2*n-1, INIT_DATA); } SegmentTree(const vector<T1>& v) : SegmentTree((int)v.size()){ for(unsigned i=0; i<v.size(); ++i) data[n-1+i] = v[i]; for(int k=n-2; k>=0; --k) data[k] = uniteData(data[k*2+1], data[k*2+2]); } // a番目の要素にパラメータxによる更新処理を適用 void update(int a, T2 x){ updateTree(a, 0, 0, n-1, x); } // 区間[a,b]の計算結果を返す T1 get(int a, int b){ return getValue(a, b, 0, 0, n-1); } }; const vector<vector<long long> > SegmentTree::INIT_DATA = { { 1, 0, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 }, }; int main() { int n, q; cin >> n >> q; vector<long long> x(n, 0); vector<long long> y(n, 0); SegmentTree st(n+1); while(--q >= 0){ char c; int i; cin >> c >> i; if(c == 'a'){ vector<vector<long long> > mat = st.get(0, i); long long ans = accumulate(mat[0].begin(), mat[0].end(), 0LL); ans %= MOD; cout << ans << endl; continue; } int v; cin >> v; if(c == 'x') x[i] = v; else y[i] = v; vector<vector<long long> > mat = { { 1, 0, x[i], 0 }, { 0, y[i], 0, 1 }, { 0, 2*y[i]%MOD, y[i]*y[i]%MOD, 1 }, { 0, 0, 0, 1 }, }; st.update(i+1, mat); } return 0; }