結果

問題 No.510 二次漸化式
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-21 08:51:32
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 581 ms / 3,000 ms
コード長 3,593 bytes
コンパイル時間 734 ms
コンパイル使用メモリ 106,856 KB
実行使用メモリ 153,572 KB
最終ジャッジ日時 2023-09-03 22:48:25
合計ジャッジ時間 13,208 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 153 ms
22,396 KB
testcase_03 AC 153 ms
22,472 KB
testcase_04 AC 153 ms
24,256 KB
testcase_05 AC 151 ms
22,532 KB
testcase_06 AC 237 ms
54,524 KB
testcase_07 AC 235 ms
53,544 KB
testcase_08 AC 236 ms
53,516 KB
testcase_09 AC 232 ms
53,516 KB
testcase_10 AC 87 ms
4,380 KB
testcase_11 AC 87 ms
5,612 KB
testcase_12 AC 87 ms
4,376 KB
testcase_13 AC 87 ms
4,380 KB
testcase_14 AC 87 ms
4,380 KB
testcase_15 AC 87 ms
4,380 KB
testcase_16 AC 332 ms
121,748 KB
testcase_17 AC 333 ms
121,156 KB
testcase_18 AC 354 ms
120,472 KB
testcase_19 AC 330 ms
121,980 KB
testcase_20 AC 333 ms
121,748 KB
testcase_21 AC 329 ms
121,940 KB
testcase_22 AC 336 ms
120,932 KB
testcase_23 AC 511 ms
153,572 KB
testcase_24 AC 514 ms
152,728 KB
testcase_25 AC 517 ms
152,728 KB
testcase_26 AC 516 ms
153,008 KB
testcase_27 AC 515 ms
152,536 KB
testcase_28 AC 514 ms
152,604 KB
testcase_29 AC 514 ms
152,584 KB
testcase_30 AC 512 ms
152,740 KB
testcase_31 AC 571 ms
152,584 KB
testcase_32 AC 581 ms
152,248 KB
testcase_33 AC 568 ms
152,112 KB
testcase_34 AC 390 ms
142,880 KB
testcase_35 AC 342 ms
132,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0