Pagini recente » Cod sursa (job #1857532) | Cod sursa (job #2020004) | Cod sursa (job #2568239) | Cod sursa (job #159246) | Cod sursa (job #1035525)
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_N = 930000;
const int buff_len = 1024*8;
const int lines = 276997;
typedef long long LL;
LL v[MAX_N];
char buff_query[buff_len];
char buff_numeros[buff_len];
char *at_query = buff_query;
char *at_nums = buff_numeros + buff_len - 1;
FILE * query = fopen("dtcsu.in", "r");
FILE * numeros = fopen("dtcsu.in", "r");
int query_begin;
void get_query_start(){
int cnt = 0, i;
int last = 0;
char * buff = buff_query;
while(cnt < lines){
memset(buff, 0, buff_len);
fread(buff, 1, buff_len, query);
for(i = 0 ; i < buff_len && buff[i] && cnt < lines ; ++i){
if(buff[i] == '\n')
++cnt;
if(cnt == lines)
break;
}
if(cnt < lines)
last = ftell(query);
}
rewind(query);
fseek(query, last, SEEK_SET);
fread(buff_query, 1, i + 1, query);
query_begin = ftell(query);
}
inline void next_char(char * &at){
++at;
if(at - buff_query == buff_len){
fread(buff_query, 1, buff_len, query);
at = buff_query;
} else if(at - buff_numeros == buff_len){
fread(buff_numeros, 1, buff_len, numeros);
at = buff_numeros;
}
}
inline LL const next_int(char * &at){
if(!(*at))
next_char(at);
while(*at && (*at > '9' || *at < '0'))
next_char(at);
LL ret = 0;
while(*at >= '0' && *at <= '9'){
ret = ret * 10 + *at - 48;
next_char(at);
}
return ret;
}
int n_len;
int Q;
int bsearch(const LL &val){
int step,i = 0;
for(step = 1 ; step <= n_len ; step <<= 1);
for(; step ; step >>= 1){
if(i + step < n_len && v[i + step] <= val)
i += step;
}
return v[i] == val;
}
int check(){
rewind(query);
fseek(query, query_begin, SEEK_SET);
fread(buff_query, 1, buff_len, query);
at_query = buff_query;
int ret = 0;
const int Q = next_int(at_query);
for(int i = 0 ; i < Q ; ++i){
LL const now = next_int(at_query);
ret += bsearch(now);
}
return ret;
}
int main(){
get_query_start();
int i,j;
int total = 0;
for(i = 0 ; i < lines ; i += j){
for(j = 0 ; j < MAX_N && i + j < lines ; ++j)
v[j] = next_int(at_nums);
sort(v, v + j);
n_len = j;
total += check();
}
FILE * out = fopen("dtcsu.out", "r");
fprintf(out, "%d\n", total);
printf("%d\n", total);
fclose(out);
fclose(query);
fclose(numeros);
return 0;
}