結果

問題 No.469 区間加算と一致検索の問題
ユーザー koba-e964koba-e964
提出日時 2016-12-19 00:38:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 469 ms / 5,000 ms
コード長 1,324 bytes
コンパイル時間 1,076 ms
コンパイル使用メモリ 76,624 KB
実行使用メモリ 18,636 KB
最終ジャッジ日時 2023-08-23 13:21:00
合計ジャッジ時間 13,129 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 14 ms
4,380 KB
testcase_04 AC 18 ms
4,380 KB
testcase_05 AC 20 ms
4,376 KB
testcase_06 AC 24 ms
4,508 KB
testcase_07 AC 25 ms
4,588 KB
testcase_08 AC 28 ms
4,512 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 21 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 28 ms
4,612 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 25 ms
4,436 KB
testcase_17 AC 22 ms
4,380 KB
testcase_18 AC 24 ms
4,544 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 27 ms
4,636 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 192 ms
10,736 KB
testcase_24 AC 190 ms
10,984 KB
testcase_25 AC 190 ms
10,768 KB
testcase_26 AC 191 ms
10,964 KB
testcase_27 AC 194 ms
10,668 KB
testcase_28 AC 191 ms
10,776 KB
testcase_29 AC 193 ms
10,656 KB
testcase_30 AC 191 ms
10,756 KB
testcase_31 AC 192 ms
10,828 KB
testcase_32 AC 189 ms
10,760 KB
testcase_33 AC 462 ms
18,272 KB
testcase_34 AC 464 ms
18,488 KB
testcase_35 AC 465 ms
18,636 KB
testcase_36 AC 469 ms
18,432 KB
testcase_37 AC 465 ms
18,368 KB
testcase_38 AC 437 ms
16,012 KB
testcase_39 AC 439 ms
16,108 KB
testcase_40 AC 441 ms
15,676 KB
testcase_41 AC 439 ms
15,908 KB
testcase_42 AC 439 ms
15,948 KB
testcase_43 AC 439 ms
15,684 KB
testcase_44 AC 436 ms
15,784 KB
testcase_45 AC 435 ms
15,776 KB
testcase_46 AC 439 ms
16,244 KB
testcase_47 AC 433 ms
16,116 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 436 ms
16,032 KB
testcase_51 AC 439 ms
15,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <map>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<ll> VL;

ll powmod(ll x, ll e, ll mod) {
  ll sum = 1 % mod;
  ll cur = x % mod;
  while (e > 0) {
    if (e % 2 == 1) {
      sum = sum * cur % mod;
    }
    cur = cur * cur % mod;
    e /= 2;
  }
  return sum;
}

ll calc(int left, int rr, ll b, ll mod) {
  // TODO naive
  // (b^r-b^l) / (b-1)
  ll sum = powmod(b, rr, mod) - powmod(b, left, mod) + mod;
  sum %= mod;
  sum *= powmod(b, mod - 2, mod);
  sum %= mod;
  return sum;
}

int main(void){
  int n, q;
  cin >> n >> q;
  ll mods[] = {1000000007, 1000000009};
  int nm = sizeof(mods) / sizeof(mods[0]);
  vector<VL> hash(q + 1, VL(nm, 0));
  map<VL, int> res;
  res[hash[0]] = 0;
  REP(i, 0, q) {
    char tt;
    cin >> tt;
    if (tt == '?') {
      REP(j, 0, nm) {
	hash[i + 1][j] = hash[i][j];
      }
    } else {
      int l, r;
      ll k;
      cin >> l >> r >> k;
      REP(j, 0, nm) {
	ll h = calc(l, r, 457, mods[j]);
	h *= mods[j] + k;
	h %= mods[j];
	hash[i + 1][j] = (hash[i][j] + h) % mods[j];
      }
    }
    if (res.count(hash[i + 1]) == 0) {
      res[hash[i + 1]] = i + 1;
    }
    if (tt == '?') {
      cout << res[hash[i + 1]] << endl;
    }
  }
}
0