結果

問題 No.510 二次漸化式
ユーザー pekempeypekempey
提出日時 2017-04-30 18:41:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 3,000 ms
コード長 2,122 bytes
コンパイル時間 737 ms
コンパイル使用メモリ 74,644 KB
実行使用メモリ 20,448 KB
最終ジャッジ日時 2023-10-11 23:54:31
合計ジャッジ時間 2,865 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 7 ms
4,348 KB
testcase_03 AC 6 ms
4,352 KB
testcase_04 AC 6 ms
4,352 KB
testcase_05 AC 6 ms
4,348 KB
testcase_06 AC 11 ms
5,132 KB
testcase_07 AC 10 ms
5,072 KB
testcase_08 AC 11 ms
5,044 KB
testcase_09 AC 10 ms
5,136 KB
testcase_10 AC 3 ms
4,352 KB
testcase_11 AC 4 ms
4,356 KB
testcase_12 AC 4 ms
4,352 KB
testcase_13 AC 4 ms
4,352 KB
testcase_14 AC 4 ms
4,352 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 14 ms
20,300 KB
testcase_17 AC 14 ms
20,236 KB
testcase_18 AC 14 ms
20,300 KB
testcase_19 AC 14 ms
20,180 KB
testcase_20 AC 14 ms
20,132 KB
testcase_21 AC 15 ms
20,180 KB
testcase_22 AC 14 ms
20,200 KB
testcase_23 AC 20 ms
20,316 KB
testcase_24 AC 20 ms
20,312 KB
testcase_25 AC 22 ms
20,324 KB
testcase_26 AC 20 ms
20,132 KB
testcase_27 AC 21 ms
20,200 KB
testcase_28 AC 20 ms
20,260 KB
testcase_29 AC 21 ms
20,304 KB
testcase_30 AC 21 ms
20,184 KB
testcase_31 AC 22 ms
20,448 KB
testcase_32 AC 24 ms
20,204 KB
testcase_33 AC 22 ms
20,132 KB
testcase_34 AC 15 ms
20,128 KB
testcase_35 AC 10 ms
20,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <array>
#include <vector>

const int mod = 1e9 + 7;

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

char readChar() {
	int c;
	while ((c = getchar()) < 'a');
	return c;
}

int readInt() {
	int n, c;
	while ((c = getchar()) < '0');
	n = c - '0';
	while ((c = getchar()) >= '0') {
		n = n * 10 + c - '0';
	}
	return n;
}

void writeInt(int n) {
	int d[11];
	int i = 0;
	do {
		d[i++] = n % 10;
		n /= 10;
	} while (n > 0);
	while (i > 0) {
		putchar(d[--i] + '0');
	}
	putchar('\n');
}

// stupid...
struct foo {
	long long a, b, c, d, e, f, g, h;
};

foo operator*(const foo &x, const foo &y) {
	foo ret;
	ret.a = (y.a + x.a * y.d + x.b * y.f) % mod;
	ret.b = (y.b + x.b * y.g) % mod;
	ret.c = (x.c + y.c + x.a * y.e + x.b * y.h) % mod;
	ret.d = x.d * y.d % mod;
	ret.e = (x.e + x.d * y.e) % mod;
	ret.f = (y.d * x.f + y.f * x.g) % mod;
	ret.g = x.g * y.g % mod;
	ret.h = (y.e * x.f + x.h + x.g * y.h) % mod;
	return ret;
}

foo getmat(long long x, long long y) {
	foo c;
	c.a = 0;
	c.b = x;
	c.c = 0;
	c.d = y;
	c.e = 1;
	c.f = 2 * y % mod;
	c.g = y * y % mod;
	c.h = 1;
	return c;
}

int main() {
	int n = readInt();
	int q = readInt();

	std::vector<int> xs(n);
	std::vector<int> ys(n);

	int N = 1;
	while (N < n) {
		N *= 2;
	}
	std::vector<foo> seg(N * 2, getmat(0, 0));

	auto setval = [&](int k, foo v) {
		k += N;
		seg[k] = v;
		while (k > 1) {
			k /= 2;
			seg[k] = seg[k * 2 + 1] * seg[k * 2];
		}
	};

	auto getval = [&](int k) {
		if (k == N) {
			return seg[1];
		}
		k += N;
		foo ret = {};
		ret.d = 1;
		ret.g = 1;
		while (k > 1) {
			if (k & 1) {
				ret = ret * seg[k - 1];
			}
			k /= 2;
		}
		return ret;
	};

	while (q--) {
		char c = readChar();
		if (c == 'x') {
			int i = readInt();
			int v = readInt();
			xs[i] = v;
			setval(i, getmat(xs[i], ys[i]));
		} else if (c == 'y') {
			int i = readInt();
			int v = readInt();
			ys[i] = v;
			setval(i, getmat(xs[i], ys[i]));
		} else {
			int i = readInt();
			auto m = getval(i);
			writeInt((m.a + m.b + m.c + 1) % mod);
		}
	}
}
0