#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N, M, num;
vector<int> vec;
//vector<int> answer;
bool cmp(int a, int b)
{
return a < b;
}
/*
int leftidx(int idx)
{
int start = 0;
int end = N - 1;
while (end - start > 0)
{
int mid = (start + end) / 2;
if (vec[mid] >= idx)
{
end = mid;
}
else start = mid + 1;
}
return end;
}
int rightidx(int idx)
{
int start = 0;
int end = N - 1;
while (end - start > 0)
{
int mid = (start + end) / 2;
if (vec[mid] > idx)
{
end = mid;
}
else start = mid + 1;
}
return end;
}
*/
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> num;
vec.push_back(num);
}
sort(vec.begin(), vec.end(), cmp);
cin >> M;
for (int i = 0; i < M; i++)
{
cin >> num;
int result = upper_bound(vec.begin(), vec.end(), num) - lower_bound(vec.begin(), vec.end(), num);
cout << result << " ";
/*
int l = leftidx(num);
int r = rightidx(num);
if (vec[l] != num && vec[r] != num)
{
answer.push_back(0);
}
else if (vec[l] == num && vec[r] == num)
{
answer.push_back(r - l + 1);
}
else
{
answer.push_back(r - l);
}
*/
}
/*for (int i = 0; i < answer.size(); i++)
{
cout << answer[i] << " ";
}*/
cout << "\n";
return 0;
}
이분 탐색 관련 문제였다. upper_bound 개념과 lower_bound 개념이 사용되었다.
Baekjoon_Link 👈 Click here