結果

問題 No.510 二次漸化式
ユーザー FF256grhyFF256grhy
提出日時 2017-05-08 12:18:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 179 ms / 3,000 ms
コード長 4,493 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 148,864 KB
実行使用メモリ 36,304 KB
最終ジャッジ日時 2023-10-12 18:51:52
合計ジャッジ時間 6,820 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 69 ms
4,352 KB
testcase_03 AC 68 ms
4,348 KB
testcase_04 AC 69 ms
4,348 KB
testcase_05 AC 69 ms
4,348 KB
testcase_06 AC 96 ms
7,464 KB
testcase_07 AC 97 ms
7,464 KB
testcase_08 AC 96 ms
7,500 KB
testcase_09 AC 96 ms
7,756 KB
testcase_10 AC 40 ms
4,352 KB
testcase_11 AC 40 ms
4,352 KB
testcase_12 AC 40 ms
4,352 KB
testcase_13 AC 41 ms
4,348 KB
testcase_14 AC 41 ms
4,348 KB
testcase_15 AC 41 ms
4,348 KB
testcase_16 AC 110 ms
36,172 KB
testcase_17 AC 109 ms
36,132 KB
testcase_18 AC 110 ms
36,196 KB
testcase_19 AC 110 ms
36,300 KB
testcase_20 AC 110 ms
36,176 KB
testcase_21 AC 110 ms
36,144 KB
testcase_22 AC 110 ms
36,136 KB
testcase_23 AC 164 ms
36,136 KB
testcase_24 AC 164 ms
36,132 KB
testcase_25 AC 167 ms
36,204 KB
testcase_26 AC 164 ms
36,136 KB
testcase_27 AC 165 ms
36,124 KB
testcase_28 AC 164 ms
36,304 KB
testcase_29 AC 165 ms
36,148 KB
testcase_30 AC 167 ms
36,300 KB
testcase_31 AC 177 ms
36,176 KB
testcase_32 AC 178 ms
36,128 KB
testcase_33 AC 179 ms
36,200 KB
testcase_34 AC 146 ms
36,188 KB
testcase_35 AC 130 ms
36,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define UB upper_bound
#define LB lower_bound
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename T> class SegTree {
private:
	T * a = NULL;
	int M, S;
	T (*F)(T, T);
	T I;

public:
	// [0, size) が使用可能になる、func が演算、id は単位元
	void init(int size, T (*func)(T, T), T id) {
		assert(size > 0);
		M = size;
		F = func;
		I = id;
		S = 1;
		while(S < size) { S *= 2; }
		delete[] a;
		a = new T[S * 2];
		inc(i, S * 2) { a[i] = I; }
	}
	
	// 参照(値を変更した後は calc() を呼ぶこと)
	T & operator[](int p) {
		assert(inID(p, 0, M));
		p += S;
		return a[p];
	}
	
	// テーブルの再計算
	void calc() {
		decID(i, 1, S) {
			a[i] = F(a[i * 2], a[i * 2 + 1]);
		}
	}
	
	// a[p] を v に変更し再計算
	void update(int p, T v) {
		assert(inID(p, 0, M));
		p += S;
		a[p] = v;
		while(p != 1) {
			p /= 2;
			a[p] = F(a[p * 2], a[p * 2 + 1]);
		}
	}
	
	// [l, r) に f を適用した結果を返す
	T range(int l, int r) {
		assert(inII(l, 0, M));
		assert(inII(r, 0, M));
		assert(l <= r);
		l += S;
		r += S;
		
		T v = I, w = I;
		while(l < r) {
			if(l + 1 == r) { v = F(v, a[l]); break; }
			
			if(l % 2 == 1) { v = F(v, a[l]); }
			if(r % 2 == 1) { w = F(a[r - 1], w); }
			
			l = (l + 1) / 2;
			r = r / 2;
		}
		
		return F(v, w);
	}
};

template<typename T> T rev_prod(T x, T y) { return y * x; }

// ---- ---- ---- ----

const LL MOD = 1000000007;

// 正直微妙
template<int N, typename T> class Matrix {
public:
	T a[N][N];
	
	Matrix() {
		inc(i, N) {
		inc(j, N) {
			a[i][j] = 0;
		}
		}
	}
	
	Matrix(T l[N][N]) {
		inc(i, N) {
		inc(j, N) {
			a[i][j] = l[i][j];
		}
		}
	}
	
	Matrix operator*(const Matrix & m) const {
		Matrix x;
		inc(i, N) {
		inc(j, N) {
		inc(k, N) {
			x.a[i][j] += a[i][k] * m.a[k][j];
		}
		}
		}
		return x;
	}
};

template<int N, typename T> Matrix<N, T> unit() {
	Matrix<N, T> m;
	inc(i, N) { m.a[i][i] = 1; }
	return m;
}

// 適当過ぎる
struct MINT {
	LL v;
	MINT() {}
	MINT(LL val) { v = val; }
	MINT operator+(const MINT & x) const { return MINT((v + x.v) % MOD); }
	MINT operator*(const MINT & x) const { return MINT((v * x.v) % MOD); }
	MINT operator+=(const MINT & x) { (v += x.v) %= MOD; return *this; }
	operator LL() const { return v; }
};

int main() {
	int n, q;
	cin >> n >> q;
	
	// 眠い
	typedef Matrix<4, MINT> MM;
	SegTree<MM> st;
	st.init(n, rev_prod, unit<4, MINT>());
	
	MINT a[4][4] = {
		{ 1, 0, 0, 0 }, 
		{ 0, 1, 0, 0 }, 
		{ 1, 0, 0, 0 }, 
		{ 1, 0, 0, 0 }, 
	};
	MM m(a);
	inc(i, n) { st[i] = m; }
	st.calc();
	
	inc(qq, q) {
		char c; LL i, v;
		cin >> c;
		
		if(c == 'x') {
			cin >> i >> v;
			
			MM m = st[i];
			m.a[1][3] = v;
			st.update(i, m);
		}
		if(c == 'y') {
			cin >> i >> v;
			
			MM m = st[i];
			m.a[2][2] = v;
			m.a[3][2] = 2 * v;
			m.a[3][3] = (v * v) % MOD;
			st.update(i, m);
		}
		if(c == 'a') {
			cin >> i;
			
			MM m = st.range(0, i);
			MINT ans(0);
			inc(j, 4) { ans += m.a[1][j]; }
			
			cout << ans << "\n";
		}
	}
	
	return 0;
}
0