結果

問題 No.510 二次漸化式
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-21 08:50:10
言語 D
(dmd 2.105.2)
結果
RE  
実行時間 -
コード長 3,559 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 106,948 KB
実行使用メモリ 43,156 KB
最終ジャッジ日時 2023-09-03 22:48:06
合計ジャッジ時間 12,150 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

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],
      ];
    }
    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