結果
問題 | No.206 数の積集合を求めるクエリ |
ユーザー | startcpp |
提出日時 | 2015-03-30 17:36:01 |
言語 | C++11 (gcc 11.4.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,422 bytes |
コンパイル時間 | 605 ms |
コンパイル使用メモリ | 64,992 KB |
実行使用メモリ | 5,504 KB |
最終ジャッジ日時 | 2024-07-03 22:53:49 |
合計ジャッジ時間 | 5,048 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | WA | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | AC | 5 ms
5,376 KB |
testcase_16 | AC | 4 ms
5,376 KB |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | AC | 14 ms
5,376 KB |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:156:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 156 | scanf("%d%d%d", &L, &M, &N); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:157:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 157 | for(int i = 0; i < L; i++ ) scanf("%d", A+i); | ~~~~~^~~~~~~~~~~ main.cpp:158:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 158 | for(int i = 0; i < M; i++ ) scanf("%d", B+i); | ~~~~~^~~~~~~~~~~ main.cpp:159:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 159 | scanf("%d", &Q); | ~~~~~^~~~~~~~~~
ソースコード
#include<iostream> #include<algorithm> #include<stdio.h> #include<vector> #define DB(x) using namespace std; class AandBProblem { private: int L, M, N; int A[100000], B[100000]; int Q; public: void input(int l, int m, int n, int *a, int *b, int q) { L = l; M = m; N = n; for(int i = 0; i < l; i++) A[i] = a[i]; for(int i = 0; i < m; i++) B[i] = b[i]; Q = q; } //O(N(L+M)Q) vector<int> solve1() { int i, j, k; vector<int> ret; ret.resize(Q); for( i = 0; i < Q; i++ ) { ret[i] = 0; for( j = 1; j <= N; j++ ) { for( k = 0; k < L; k++ ) if ( A[k] == j ) break; if ( k == L ) continue; for( k = 0; k < M; k++ ) if ( B[k] + i == j ) break; if ( k == M ) continue; ret[i]++; } } return ret; } //O(NQ)の定数1なやつ。 vector<int> solve2() { int i, j; vector<int> ret; ret.resize(Q); for( i = 0; i < Q; i++ ) { ret[i] = 0; vector<int> paket; paket.resize(N+1+i); for( j = 0; j < N+1+i; j++ ) paket[j] = 0; for( j = 0; j < L; j++ ) { paket[A[j]]++; } for( j = 0; j < M; j++ ) { paket[B[j]+i]++; } for( j = 0; j < N+1; j++ ) { if ( paket[j] == 2 ) ret[i]++; } } return ret; } //O(LM)の定数ちょっと速いやつ vector<int> solve3() { int i, j; int ans[100001] = {0}; sort(A, A+L); sort(B, B+M); for( i = 0; i < L; i++ ) { for( j = 0; j < M; j++ ) { if ( B[j] > A[i] ) break; ans[ A[i] - B[j] ]++; } } vector<int> ret; for( i = 0; i < Q; i++ ) ret.push_back(ans[i]); return ret; } //想定解:ビットで畳み込んで定数高速化したO(NQ)解法 vector<int> solve4() { unsigned short int uA[8000] = {0}, uB[8000] = {0}; short int bcnt[65536] = {0}; for(int i = 0; i < L; i++ ) uA[A[i]/16] += (1 << (A[i]%16)); for(int i = 0; i < M; i++ ) uB[B[i]/16] += (1 << (B[i]%16)); for(int i = 0; i < 65536; i++ ) { for(int j = 0; j < 16; j++ ) bcnt[i] += (i>>j)&1; } short int n = N/16 + 3; vector<int> ans; ans.resize(Q, 0); for(int i = 0; i < Q; i++ ) { for( short int j = 0; j < n; j++ ) { ans[i] += bcnt[uA[j]&uB[j]]; } for( short int j = n - 1; j >= 0; j-- ) { uB[j+1] += (uB[j] >> 15); uB[j] = (uB[j] << 1); } } return ans; } //想定解:ビットで畳み込んで定数高速化したO(NQ)解法 vector<int> solve5() { unsigned int uA[4000] = {0}, uB[4000] = {0}; int bcnt[65536] = {0}; for(int i = 0; i < L; i++ ) uA[A[i]/32] += (1 << (A[i]%32)); for(int i = 0; i < M; i++ ) uB[B[i]/32] += (1 << (B[i]%32)); for(int i = 0; i < 65536; i++ ) { for(int j = 0; j < 16; j++ ) bcnt[i] += (i>>j)&1; } short int n = N/32 + 3; vector<int> ans; ans.resize(Q, 0); for(int i = 0; i < Q; i++ ) { for( short int j = 0; j < n; j++ ) { ans[i] += bcnt[((uA[j]&uB[j])>>16) + ((uA[j]&uB[j])&(65535))]; } for( int j = n - 1; j >= 0; j-- ) { uB[j+1] += (uB[j] >> 31); uB[j] = (uB[j] << 1); } } return ans; } }test; int L, M, N; int A[100000], B[100000]; int Q; int main() { scanf("%d%d%d", &L, &M, &N); for(int i = 0; i < L; i++ ) scanf("%d", A+i); for(int i = 0; i < M; i++ ) scanf("%d", B+i); scanf("%d", &Q); test.input(L, M, N, A, B, Q); //printf("Test\n"); //vector<int> res2 = test.solve2(); vector<int> res5 = test.solve5(); for(int i = 0; i < Q; i++ ) printf("%d\n", res5[i]); return 0; }