結果

問題 No.469 区間加算と一致検索の問題
ユーザー koba-e964
提出日時 2016-12-19 00:34:35
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 434 ms / 5,000 ms
コード長 1,800 bytes
コンパイル時間 848 ms
コンパイル使用メモリ 101,624 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-12-21 16:42:09
合計ジャッジ時間 11,637 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

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