結果
問題 | No.469 区間加算と一致検索の問題 |
ユーザー |
|
提出日時 | 2016-12-19 00:20:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 103 ms / 5,000 ms |
コード長 | 1,629 bytes |
コンパイル時間 | 1,982 ms |
コンパイル使用メモリ | 181,068 KB |
実行使用メモリ | 20,828 KB |
最終ジャッジ日時 | 2024-12-21 16:39:48 |
合計ジャッジ時間 | 5,343 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 49 |
ソースコード
#include <bits/stdc++.h>using namespace std;const int mod = 1e9 + 7;int add(int x, int y) {return (x += y) >= mod ? x - mod : x;}int sub(int x, int y) {return add(x, mod - y);}int mul(int x, int y) {return 1LL * x * y % mod;}int main() {int n, q;cin >> n >> q;const int mod = 1e9 + 7;srand(time(NULL));int base1 = rand() + 2;int base2 = rand() + 2;int base3 = rand() + 2;vector<int> ws1(n + 1);vector<int> ws2(n + 1);vector<int> ws3(n + 1);int p1 = 1;int p2 = 1;int p3 = 1;for (int i = 1; i <= n; i++) {ws1[i] = add(ws1[i - 1], p1);ws2[i] = add(ws2[i - 1], p2);ws3[i] = add(ws3[i - 1], p3);p1 = mul(p1, base1);p2 = mul(p2, base2);p3 = mul(p3, base3);}int rh1 = 0;int rh2 = 0;int rh3 = 0;map<tuple<int, int, int>, int> mp;mp[make_tuple(0, 0, 0)] = 0;for (int i = 1; i <= q; i++) {char c;scanf(" %c", &c);if (c == '!') {int l, r, v;scanf("%d %d %d", &l, &r, &v);v %= mod;if (v < 0) {v += mod;}rh1 = add(rh1, mul(v, sub(ws1[r], ws1[l])));rh2 = add(rh2, mul(v, sub(ws2[r], ws2[l])));rh3 = add(rh3, mul(v, sub(ws3[r], ws3[l])));tuple<int, int, int> curr(rh1, rh2, rh3);if (mp.count(curr) == 0) {mp[curr] = i;}} else {tuple<int, int, int> curr(rh1, rh2, rh3);printf("%d\n", mp[curr]);}}}