結果

問題 No.206 数の積集合を求めるクエリ
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-08-16 23:06:59
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,557 bytes
コンパイル時間 1,511 ms
コンパイル使用メモリ 168,052 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-04-25 08:07:43
合計ジャッジ時間 11,449 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 13 ms
5,376 KB
testcase_14 AC 18 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 WA -
testcase_18 AC 34 ms
7,040 KB
testcase_19 AC 53 ms
7,040 KB
testcase_20 AC 29 ms
7,168 KB
testcase_21 AC 38 ms
7,168 KB
testcase_22 AC 36 ms
7,040 KB
testcase_23 AC 55 ms
7,168 KB
testcase_24 WA -
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int L, M, N;
    vector<ll> A, B;
    int Q;
    void input() {
        cin >> L >> M >> N;
        A.clear(); A.resize(N, 0);
        B.clear(); B.resize(N, 0);
        for (int i = 0; i < L; i++) {
            int a; cin >> a; a--;
            A[a] = 1;
        }
        for (int i = 0; i < M; i++) {
            int b; cin >> b; b--;
            B[b] = 1;
        }
        cin >> Q;
    }

    const int X = 991;

    const ll mod = ll(1e8 + 7);

    ll pow(ll x, int n) {
        if (n == 0) return 1;
        if (n & 1) return x * pow(x, n - 1) % mod;
        return pow(x * x % mod, n / 2);
    }

    ll hash(const vector<ll>& h, int s, int t) {
        return (h[t] + mod - h[s] * pow(X, t - s) % mod) % mod;
    }

    void solve() {
        vector<ll> count_a(N + 1, 0);
        for (int i = 0; i < N; i++) {
            count_a[i + 1] = count_a[i] + (A[i] == 1);
        }

        vector<ll> hash_a(N + 1, 0);
        vector<ll> hash_b(N + 1, 0);
        for (int i = 0; i < N; i++) {
            hash_a[i + 1] = (ll(A[i]) + hash_a[i] * X % mod) % mod;
            hash_b[i + 1] = (ll(B[i]) + hash_b[i] * X % mod) % mod;
        }

        for (int q = 0; q < Q; q++) {
            //cerr << "q: " << q << endl;
            int ans = 0;
            for (int i = q; i < N; ) {
                if (A[i] == B[i - q]) {
                    int j = 0;
                    //cout <<  (hash(hash_a, i, i + (1<<j))) << " " << (hash(hash_b, i - q, i - q + (1<<j))) << endl;
                    assert (hash(hash_a, i, i + (1<<j)) == hash(hash_b, i - q, i - q + (1<<j)));
                    while (hash(hash_a, i, i + (1<<j)) == hash(hash_b, i - q, i - q + (1<<j))) {
                        j++;
                    }
                    j--;
                    ans += count_a[i + (1<<j)] - count_a[i];
                    i += (1<<j);
                } else {
                    i++;
                }
            }
            cout << ans << endl;
        }
    }
}

int main() {
    input(); solve();
    return 0;
}

0