Cod sursa(job #3161103)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 25 octombrie 2023 19:30:06
Problema Zoo Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.06 kb
#include <bits/stdc++.h>
#define DIM 500001
#define time ieuhrfueirf

using namespace std;

ifstream fin("zoo.in");
ofstream fout("zoo.out");

struct q{

    int x1, y1, x2, y2, index;


};

q Query[DIM];

int bit[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 <= n ; 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)

            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)

            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<=n;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<=n;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);

    return 0;

    Batog();

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


}