- 데이터가 너무 많다!
버퍼를 사용한, 저수준 입출력 사용.
#include <iostream>
#include <algorithm>
using namespace std;
using int32 = int32_t;
using int64 = int64_t;
#include <iostream>
#include <cstring>
using namespace std;
const int BUF_SIZE = 1 << 20; // 메모리 초과 가능, 적당히 조절할 것.
char input_buf[BUF_SIZE], output_buf[BUF_SIZE];
int input_buf_pos, output_buf_pos;
enum io_mode {READ, WRITE};
inline void char_io(io_mode mode, char &target)
{
if(mode == READ) {
if (input_buf_pos == BUF_SIZE)
{
fread(input_buf, sizeof(char), BUF_SIZE, stdin);
input_buf_pos = 0;
}
target = input_buf[input_buf_pos++];
}
else {
if(output_buf_pos == BUF_SIZE || target == EOF)
{
fwrite(output_buf, sizeof(char), output_buf_pos, stdout);
output_buf_pos = 0;
}
output_buf[output_buf_pos++] = target;
}
}
inline void char_io(io_mode mode, char &&target){
char_io(mode, target);
}
inline void int_io(io_mode mode, int &target)
{
char c;
if(mode == READ){
bool negative = false;
int ret = 0;
while(true)
{
char_io(READ, c);
if(c == '\n' || c == ' ') break;
if(c == '-') {
negative = true;
continue;
}
ret = ret * 10 + (c & 0b1111);
}
target = negative ? -ret : ret;
}
else {
int abs = target;
int div = 1'000'000'000;
char quotient = 0;
if(abs < 0) {
char_io(WRITE, '-');
abs *= -1;
}
while(abs < div && div != 1) { div /= 10; }
while(div != 0){
quotient = '0' + (char)((abs / div)%10);
char_io(WRITE, quotient);
div /= 10;
}
}
}
inline void int_io(io_mode mode, int &&target){
int_io(mode, target);
}
int32 N, M;
int32 val[500'000];
int32 valToFind;
bool lowerBoundTest(int32 idx){
return valToFind <= val[idx];
}
bool upperBoundTest(int32 idx){
return valToFind < val[idx];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int_io(READ, N);
for(int32 i=0; i<N; i++) int_io(READ, val[i]);
sort(val, val+N);
int_io(READ, M);
int32 srt, end, mid;
int32 retSrt, retEnd;
while(M--){
retSrt = -1;
retEnd = N;
int_io(READ, valToFind);
//! check upper_bound
srt = 0;
end = N;
while(srt < end){
mid = (srt + end) / 2;
if(upperBoundTest(mid)){
retEnd = mid;
end = mid;
}
else srt = mid+1;
}
//! check lower_bound
srt = 0;
end = N;
while(srt < end){
mid = (srt + end) / 2;
if(lowerBoundTest(mid)){
retSrt = mid;
end = mid;
}
else srt = mid+1;
}
//! check valid;
if(val[retSrt] != valToFind) int_io(WRITE, 0);
else int_io(WRITE, retEnd - retSrt);
char_io(WRITE, ' ');
}
char_io(WRITE, EOF);
}