結果

問題 No.469 区間加算と一致検索の問題
ユーザー pekempeypekempey
提出日時 2016-12-19 00:20:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 5,000 ms
コード長 1,629 bytes
コンパイル時間 1,642 ms
コンパイル使用メモリ 179,692 KB
実行使用メモリ 20,624 KB
最終ジャッジ日時 2023-08-23 13:18:05
合計ジャッジ時間 6,185 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 41 ms
6,512 KB
testcase_24 AC 40 ms
6,128 KB
testcase_25 AC 41 ms
6,416 KB
testcase_26 AC 40 ms
6,176 KB
testcase_27 AC 41 ms
6,416 KB
testcase_28 AC 41 ms
6,472 KB
testcase_29 AC 41 ms
6,672 KB
testcase_30 AC 42 ms
6,300 KB
testcase_31 AC 41 ms
6,128 KB
testcase_32 AC 41 ms
6,108 KB
testcase_33 AC 115 ms
20,432 KB
testcase_34 AC 119 ms
20,624 KB
testcase_35 AC 120 ms
20,512 KB
testcase_36 AC 118 ms
20,504 KB
testcase_37 AC 118 ms
20,496 KB
testcase_38 AC 104 ms
19,184 KB
testcase_39 AC 106 ms
19,120 KB
testcase_40 AC 104 ms
19,176 KB
testcase_41 AC 101 ms
19,188 KB
testcase_42 AC 100 ms
19,120 KB
testcase_43 AC 102 ms
19,116 KB
testcase_44 AC 101 ms
19,012 KB
testcase_45 AC 102 ms
19,188 KB
testcase_46 AC 102 ms
19,068 KB
testcase_47 AC 101 ms
19,108 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 100 ms
19,012 KB
testcase_51 AC 100 ms
18,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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]);
        }
    }
}
0