結果
| 問題 |
No.510 二次漸化式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-01-21 08:51:32 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 603 ms / 3,000 ms |
| コード長 | 3,593 bytes |
| コンパイル時間 | 1,118 ms |
| コンパイル使用メモリ | 122,880 KB |
| 実行使用メモリ | 152,928 KB |
| 最終ジャッジ日時 | 2024-06-13 03:26:27 |
| 合計ジャッジ時間 | 14,030 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 34 |
ソースコード
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }
/*
(a, b^2, b, 1)
*/
enum MO = 10L^^9 + 7;
enum SIZE = 4;
enum IDENTITY = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
];
long[][] mul(long[][] a, long[][] b) {
auto ret = new long[][](SIZE, SIZE);
foreach (i; 0 .. SIZE) foreach (k; 0 .. SIZE) foreach (j; 0 .. SIZE) {
ret[i][j] += a[i][k] * b[k][j];
}
foreach (i; 0 .. SIZE) foreach (j; 0 .. SIZE) {
ret[i][j] %= MO;
}
return ret;
}
class SegmentTree {
int n;
long[][][] prod;
this(int n_) {
for (n = 1; n < n_; n <<= 1) {}
prod = new long[][][n << 1];
foreach (i; 0 .. n_) {
prod[n + i] = [
[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 1, 1],
];
}
prod[n + n_ .. $] = IDENTITY;
foreach_reverse (a; 1 .. n) {
prod[a] = mul(prod[a << 1], prod[a << 1 | 1]);
}
}
void update(int a, long[][] val) {
prod[a += n] = val;
for (a >>= 1; a; a >>= 1) {
prod[a] = mul(prod[a << 1], prod[a << 1 | 1]);
}
}
long[][] rangeProd(int a, int b) {
long[][] retL = IDENTITY, retR = IDENTITY;
for (a += n, b += n; a <= b; a >>= 1, b >>= 1) {
if ( a & 1) retL = mul(retL, prod[a++]);
if (~b & 1) retR = mul(prod[b--], retR);
}
return mul(retL, retR);
}
}
int N;
int Q;
string[] T;
int[] I;
long[] V;
void main() {
try {
for (; ; ) {
N = readInt();
Q = readInt();
T = new string[Q];
I = new int[Q];
V = new long[Q];
foreach (q; 0 .. Q) {
T[q] = readToken();
I[q] = readInt();
if (T[q] != "a") {
V[q] = readLong();
}
}
auto x = new long[N];
auto y = new long[N];
auto seg = new SegmentTree(N);
foreach (q; 0 .. Q) {
if (T[q] == "a") {
const res = seg.rangeProd(0, I[q] - 1);
long ans = res[0][0] + res[1][0] + res[2][0] + res[3][0];
ans %= MO;
writeln(ans);
} else {
switch (T[q]) {
case "x": {
x[I[q]] = V[q];
} break;
case "y": {
y[I[q]] = V[q];
} break;
default: assert(false);
}
seg.update(I[q], [
[1L , 0L , 0L , 0L],
[x[I[q]], (y[I[q]] * y[I[q]]) % MO, 0L , 0L],
[0L , (2L * y[I[q]]) % MO , y[I[q]], 0L],
[0L , 1L , 1L , 1L],
]);
}
}
}
} catch (EOFException e) {
}
}