結果
| 問題 |
No.206 数の積集合を求めるクエリ
|
| コンテスト | |
| ユーザー |
izuru_matsuura
|
| 提出日時 | 2016-08-16 23:07:48 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,557 bytes |
| コンパイル時間 | 1,836 ms |
| コンパイル使用メモリ | 167,440 KB |
| 実行使用メモリ | 12,416 KB |
| 最終ジャッジ日時 | 2024-11-07 18:30:45 |
| 合計ジャッジ時間 | 11,866 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 20 WA * 2 TLE * 1 -- * 5 |
ソースコード
#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(1e9 + 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;
}
izuru_matsuura