結果

問題 No.469 区間加算と一致検索の問題
ユーザー koba-e964koba-e964
提出日時 2016-12-19 00:34:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 464 ms / 5,000 ms
コード長 1,800 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 102,100 KB
実行使用メモリ 20,320 KB
最終ジャッジ日時 2023-08-23 13:20:00
合計ジャッジ時間 12,716 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 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,380 KB
testcase_06 AC 23 ms
4,636 KB
testcase_07 AC 25 ms
4,580 KB
testcase_08 AC 27 ms
4,776 KB
testcase_09 AC 12 ms
4,380 KB
testcase_10 AC 21 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 28 ms
4,904 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 25 ms
4,564 KB
testcase_17 AC 23 ms
4,424 KB
testcase_18 AC 24 ms
4,624 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 28 ms
5,020 KB
testcase_21 AC 7 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 188 ms
11,656 KB
testcase_24 AC 188 ms
11,816 KB
testcase_25 AC 189 ms
11,876 KB
testcase_26 AC 187 ms
11,764 KB
testcase_27 AC 190 ms
11,740 KB
testcase_28 AC 190 ms
11,824 KB
testcase_29 AC 190 ms
11,812 KB
testcase_30 AC 191 ms
11,656 KB
testcase_31 AC 189 ms
11,816 KB
testcase_32 AC 188 ms
11,876 KB
testcase_33 AC 454 ms
20,092 KB
testcase_34 AC 458 ms
20,192 KB
testcase_35 AC 455 ms
20,212 KB
testcase_36 AC 450 ms
20,224 KB
testcase_37 AC 464 ms
20,320 KB
testcase_38 AC 435 ms
17,900 KB
testcase_39 AC 434 ms
17,752 KB
testcase_40 AC 435 ms
17,792 KB
testcase_41 AC 443 ms
17,888 KB
testcase_42 AC 434 ms
17,728 KB
testcase_43 AC 437 ms
17,900 KB
testcase_44 AC 438 ms
17,900 KB
testcase_45 AC 433 ms
17,948 KB
testcase_46 AC 435 ms
17,884 KB
testcase_47 AC 436 ms
17,728 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 435 ms
17,720 KB
testcase_51 AC 433 ms
17,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#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<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;

const int Q = 100199;
int t[Q];
int l[Q], r[Q];
ll k[Q];

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 == '?') {
      t[i] = 1;
      REP(j, 0, nm) {
	hash[i + 1][j] = hash[i][j];
      }
    } else {
      t[i] = 0;
      cin >> l[i] >> r[i] >> k[i];
      REP(j, 0, nm) {
	ll h = calc(l[i], r[i], 457, mods[j]);
	h *= mods[j] + k[i];
	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