結果
| 問題 |
No.510 二次漸化式
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2020-06-21 14:45:28 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 510 ms / 3,000 ms |
| コード長 | 3,423 bytes |
| コンパイル時間 | 1,069 ms |
| コンパイル使用メモリ | 90,848 KB |
| 実行使用メモリ | 36,992 KB |
| 最終ジャッジ日時 | 2024-07-03 18:00:48 |
| 合計ジャッジ時間 | 13,681 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 34 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
struct node{
ll cnt[4][4];
};
void print(node m){
for(int j = 0; j < 4; j++){
for(int k = 0; k < 4; k++){
cout << m.cnt[j][k] << ' ';
}
cout << endl;
}
}
node calc_node(node a, node b){
node ans;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
ans.cnt[i][j] = 0;
for(int k = 0; k < 4; k++){
ans.cnt[i][j] += (b.cnt[i][k]*a.cnt[k][j])%MOD;
ans.cnt[i][j] %=MOD;
}
}
}
return ans;
}
template <typename T>
struct segtree{
int n;
T UNIT;
vector<T> dat;
segtree(int n_, T unit){
UNIT = unit;
n = 1;
while(n < n_) n *= 2;
dat = vector<T>(2*n);
for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
}
T calc(T a, T b){
T ans;
ans = calc_node(a, b);
return ans;
}
void insert(int k, T a){
dat[k+n-1] = a;
}
void update_all(){
for(int i = n-2; i >= 0; i--){
dat[i] = calc(dat[i*2+1], dat[i*2+2]);
}
}
//k番目の値(0-indexed)をaに変更
void update(int k, T a){
k += n-1;
dat[k] = a;
while(k > 0){
k = (k-1)/2;
dat[k] = calc(dat[k*2+1], dat[k*2+2]);
}
}
//[a, b)
//区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
T query(int a, int b, int k=0, int l=0, int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l) return UNIT;
if(a <= l && r <= b) return dat[k];
else{
T vl = query(a, b, k*2+1, l, (l+r)/2);
T vr = query(a, b, k*2+2, (l+r)/2, r);
return calc(vl, vr);
}
}
};
int n, q;
int x[200000], y[200000];
node create_node(ll x, ll y){
node mat;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
mat.cnt[i][j] = 0;
}
}
mat.cnt[0][0] = 1; mat.cnt[0][1] = x;
mat.cnt[1][1] = (y*y)%MOD; mat.cnt[1][2] = 2*y; mat.cnt[1][3] = 1;
mat.cnt[2][2] = y; mat.cnt[2][3] = 1;
mat.cnt[3][3] = 1;
return mat;
}
node create_unit(){
node mat;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
mat.cnt[i][j] = 0;
}
}
mat.cnt[0][0] = 1; mat.cnt[1][1] = 1;
mat.cnt[2][2] = 1; mat.cnt[3][3] = 1;
return mat;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> n >> q;
node unit_node = create_unit();
segtree<node> sgt(n, unit_node);
node node_ = create_node(0, 0);
for(int i = 0; i < n; i++){
sgt.update(i, node_);
}
for(int t = 0; t < q; t++){
char c; cin >> c;
if(c == 'x'){
int i, v; cin >> i >> v;
x[i] = v;
sgt.update(i, create_node(x[i], y[i]));
}
if(c == 'y'){
int i, v; cin >> i >> v;
y[i] = v;
sgt.update(i, create_node(x[i], y[i]));
}
if(c == 'a'){
int i;
cin >> i;
auto m = sgt.query(0, i);
cout << (m.cnt[0][0]+m.cnt[0][1]+m.cnt[0][2]+m.cnt[0][3])%MOD << endl;
}
}
}
milanis48663220