Cod sursa(job #3161122)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 25 octombrie 2023 19:45:28
Problema Zoo Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 6.71 kb
#include <bits/stdc++.h>

#define file_name "zoo"

#define file_name_input file_name".in"

#define file_name_output file_name".out"

#define fast_read 1

//#undef fast_read

#define local_user 1

#undef local_user


class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            assert(fread(buff, 1, 4096, fin));
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

};


class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

    #if local_user

        #if fast_read

            InParser fin("file.in");
            OutParser fout("file.out");

        #else

            std :: ifstream fin("file.in");
            std :: ofstream fout("file.out");

        #endif // fast_read

    #else

        #if fast_read

            InParser fin(file_name_input);
            OutParser fout(file_name_output);

        #else

            std :: ifstream fin(file_name_input);
            std :: ofstream fout(file_name_output);

        #endif // fast_read

    #endif // file_is_good

void __attribute__((constructor)) read();

void read(){

    #if fast_read
        std :: ios_base::sync_with_stdio(false);
        std :: cin.tie(nullptr);
        std :: cout.tie(nullptr);
    #else

        std :: ios :: sync_with_stdio(false);
        std :: ios_base::sync_with_stdio(false);
        std :: cin.tie(nullptr);
        std :: cout.tie(nullptr);
        fin.tie(nullptr);
        fout.tie(nullptr);

    #endif // fast_read

}

#include <bits/stdc++.h>
#define DIM 500001
#define time ieuhrfueirf

using namespace std;

struct q{

    int x1, y1, x2, y2, index;


};

q Query[DIM];

int bit[3 * DIM], solution[DIM];

pair <int, int> point[DIM];

map <int, bool> ok;

map <int, bool> :: iterator it;

unordered_map <int, int> answer;

int n, i, j, time, index_points, f, Q, bucket;

static inline bool comp_pair(const pair <int, int> &a, const pair <int, int> &b){

        if(a.first == b.first)
            return (a.second < b.second);

        else return (a.first < b.first);

}

static inline bool comp_query(const q &a, const q &b){

    if(a.x1 / bucket == b.x1 / bucket)
        return (a.x2 < b.x2);

    else return (a.x1 / bucket < b.x1 / bucket);

}

int lsb(int n){

    return (n & -n);

}

void Update(int i, int val){

    for(; i <= 2 * (n + Q) ; i += lsb(i))
        bit[i] += val;

}

int query(int i){

    int answer = 0;

    for(; i ; i -= lsb(i))
        answer += bit[i];

    return answer;

}

void Add(int pos){

    Update(point[pos].second, 1);

}

void Remove(int pos){

    Update(point[pos].second, -1);

}

void Batog(){

    int pos_L = 1, pos_R = 0;

    for(i=1;i<=Q;i++){

        if(point[pos_R + 1].first <= Query[i].x2 && pos_R < n)

            while(point[pos_R + 1].first <= Query[i].x2 && pos_R < n)
                pos_R++, Add(pos_R);

        else

            while(point[pos_R].first > Query[i].x2 && pos_R > 1)
                Remove(pos_R), pos_R--;

        if(point[pos_L - 1].first >= Query[i].x1 && pos_L > 1)

            while(point[pos_L - 1].first >= Query[i].x1 && pos_L > 1)
                pos_L--, Add(pos_L);
        else

            while(point[pos_L].first < Query[i].x1 && pos_L < n)
                Remove(pos_L), pos_L++;

        solution[Query[i].index] = query(Query[i].y2) - query(Query[i].y1 - 1);

    }

}

int main(){

    fin >> n;

    for(i=1;i<=n;i++){

        fin >> point[i].first >> point[i].second;

        ok[point[i].first] = 1;

        ok[point[i].second] = 1;

    }

    fin >> Q;

    for(i=1;i<=Q;i++){

        fin >> Query[i].x1 >> Query[i].y1 >> Query[i].x2 >> Query[i].y2;

        Query[i].index = i;

        ok[Query[i].x1] = 1;

        ok[Query[i].y1] = 1;

        ok[Query[i].x2] = 1;

        ok[Query[i].y2] = 1;

    }

    for(it = ok.begin(); it != ok.end(); it++)
        answer[it -> first] = ++f;

    for(i=1;i<=n;i++){

        point[i].first = answer[point[i].first];

        point[i].second = answer[point[i].second];

    }

    for(i=1;i<=Q;i++){

        Query[i].x1 = answer[Query[i].x1];

        Query[i].x2 = answer[Query[i].x2];

        Query[i].y1 = answer[Query[i].y1];

        Query[i].y2 = answer[Query[i].y2];

    }

    bucket = sqrt(n);

    sort(point + 1, point + n + 1, comp_pair);

    sort(Query + 1, Query + Q + 1, comp_query);

    Batog();

    for(i=1;i<=Q;i++)
        fout << solution[i] << "\n";


}