結果
問題 | No.469 区間加算と一致検索の問題 |
ユーザー |
|
提出日時 | 2016-12-19 00:38:45 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 445 ms / 5,000 ms |
コード長 | 1,324 bytes |
コンパイル時間 | 665 ms |
コンパイル使用メモリ | 78,352 KB |
実行使用メモリ | 18,560 KB |
最終ジャッジ日時 | 2024-12-21 16:43:08 |
合計ジャッジ時間 | 11,404 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 49 |
ソースコード
#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; } } }