結果

問題 No.510 二次漸化式
ユーザー tossytossy
提出日時 2017-05-06 19:35:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 346 ms / 3,000 ms
コード長 2,855 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 180,532 KB
実行使用メモリ 88,936 KB
最終ジャッジ日時 2023-10-12 15:12:14
合計ジャッジ時間 11,034 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 92 ms
4,348 KB
testcase_03 AC 93 ms
4,348 KB
testcase_04 AC 91 ms
4,348 KB
testcase_05 AC 89 ms
4,348 KB
testcase_06 AC 154 ms
13,688 KB
testcase_07 AC 154 ms
13,540 KB
testcase_08 AC 156 ms
13,688 KB
testcase_09 AC 154 ms
13,528 KB
testcase_10 AC 48 ms
4,352 KB
testcase_11 AC 47 ms
4,352 KB
testcase_12 AC 46 ms
4,348 KB
testcase_13 AC 47 ms
4,348 KB
testcase_14 AC 47 ms
4,356 KB
testcase_15 AC 47 ms
4,352 KB
testcase_16 AC 235 ms
88,684 KB
testcase_17 AC 241 ms
88,428 KB
testcase_18 AC 235 ms
88,412 KB
testcase_19 AC 234 ms
88,776 KB
testcase_20 AC 228 ms
88,780 KB
testcase_21 AC 249 ms
88,428 KB
testcase_22 AC 237 ms
88,792 KB
testcase_23 AC 338 ms
88,788 KB
testcase_24 AC 346 ms
88,444 KB
testcase_25 AC 344 ms
88,796 KB
testcase_26 AC 339 ms
88,936 KB
testcase_27 AC 343 ms
88,736 KB
testcase_28 AC 339 ms
88,908 KB
testcase_29 AC 341 ms
88,776 KB
testcase_30 AC 336 ms
88,404 KB
testcase_31 AC 330 ms
88,732 KB
testcase_32 AC 332 ms
88,364 KB
testcase_33 AC 332 ms
88,776 KB
testcase_34 AC 306 ms
88,432 KB
testcase_35 AC 262 ms
88,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600
#define MOD 1000000007

typedef vector<vector<long>> mat;
mat mat_mul(const mat &A, const mat &B){
  int n=A.size(), m=B[0].size(), l=B.size();
  mat ret(n, vector<long>(m, 0));
  rep(i,n) rep(k,l) if(A[i][k]!=0) rep(j,m){
    (ret[i][j] += A[i][k] * B[k][j]) %= MOD;
  }
  return ret;
}

mat newmat(long x, long y){
  return mat({{1,0,x,0},{0,y,0,1},{0,2*y,y*y%MOD,1},{0,0,0,1}});
}

class SegTree {
public:
  int n;
  vector<mat> data;
  mat ei;

  SegTree(int n_){
    n=1;
    while(n<n_) n*=2;
    data.resize(2*n-1);

    ei = mat(4, vector<long>(4,0));
    rep(i,4) ei[i][i] = 1;

    mat zeromat = newmat(0,0);
    repl(i,n-1, 2*n-1) data[i] = zeromat;
    for(int i=n-2; i>=0; i--) data[i] = mat_mul(data[i*2+2], data[i*2+1]);
  }

  mat _query(int a, int b, int k, int l, int r){
    if(r<=a || b<=l) return ei;
    if(a<=l && r<=b) return data[k];
    mat vl=_query(a,b,k*2+1,l,(l+r)/2);
    mat vr=_query(a,b,k*2+2,(l+r)/2,r);
    return mat_mul(vr, vl);
  }
  mat query(int a, int b){return _query(a,b,0,0,n);}

  void update(int k, int x, int y){
    k += n-1;
    data[k] = newmat(x,y);
    while(k>0){
      k = (k-1)/2;
      data[k] = mat_mul(data[k*2+2], data[k*2+1]);
    }
  }
};

int main(){
  int n,q;
  cin>>n>>q;
  vector<long> x(n,0), y(n,0);
  SegTree st(n);

  rep(_,q){
    char c; scanf(" %c", &c);
    if(c=='x'){
      int i,v;
      cin>>i>>v;
      st.update(i, v, y[i]);
      x[i] = v;
    } else if(c=='y'){
      int i,v;
      cin>>i>>v;
      st.update(i, x[i], v);
      y[i] = v;
    } else if(c=='a'){
      int i;
      cin>>i;
      mat m = st.query(0,i);
      long ans = m[0][0] + m[0][1] + m[0][2] + m[0][3];
      cout << ans%MOD << endl;
    } else {
      assert(false);
    }
    // cout << "a:";
    // rep(i,n+1){
    //   mat m = st.query(0,i);
    //   long ans = m[0][0] + m[0][1] + m[0][2] + m[0][3];
    //   cout << ans << ",";
    // }
    // cout << endl;
    // cout << "b:";
    // rep(i,n+1){
    //   mat m = st.query(0,i);
    //   long ans = m[1][0] + m[1][1] + m[1][2] + m[1][3];
    //   cout << ans << ",";
    //   long bb = m[2][0] + m[2][1] + m[2][2] + m[2][3];
    //   assert(ans*ans%MOD == bb %MOD);
    // }
    // cout << endl;
  }

  return 0;
}
0