Cod sursa(job #2867821)

Utilizator rares89_Dumitriu Rares rares89_ Data 10 martie 2022 16:18:38
Problema Zoo Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <bits/stdc++.h>

using namespace std;

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

pair<int, int> puncte[16005];
vector<int> tree[16005 * 4 + 1];
vector<int> v[16005];

void build(int nod, int st, int dr) {
    if(st == dr) {
        swap(tree[nod], v[st]);
        sort(tree[nod].begin(), tree[nod].end());
    } else {
        int mid = (st + dr) / 2;
        build(2 * nod, st, mid);
        build(2 * nod + 1, mid + 1, dr);
        merge(tree[2 * nod].begin(), tree[2 * nod].end(), 
              tree[2 * nod + 1].begin(), tree[2 * nod + 1].end(),
              back_inserter(tree[nod]));
    }
}

int query(int nod, int st, int dr, int xSt, int ySt, int xDr, int yDr) {
    if(st >= xSt && dr <= xDr) {
        return upper_bound(tree[nod].begin(), tree[nod].end(), yDr) - 
               lower_bound(tree[nod].begin(), tree[nod].end(), ySt);
    } else {
        int mid = (st + dr) / 2;
        if(mid >= xDr) {
            return query(2 * nod, st, mid, xSt, ySt, xDr, yDr);
        }
        if(xSt >= mid + 1) {
            return query(2 * nod + 1, mid + 1, dr, xSt, ySt, xDr, yDr);
        }
        int querySt = query(2 * nod, st, mid, xSt, ySt, xDr, yDr);
        int queryDr = query(2 * nod + 1, mid + 1, dr, xSt, ySt, xDr, yDr);
        return querySt + queryDr;
    }
}

vector<int> coord;
int n, q;

int main() {
    fin >> n;
    for(int i = 1; i <= n; i++) {
        fin >> puncte[i].first >> puncte[i].second;
        coord.push_back(puncte[i].first);
    }
    sort(coord.begin(), coord.end());
    coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
    for(int i = 1; i <= n; i++) {
        puncte[i].first = lower_bound(coord.begin(), coord.end(), puncte[i].first) - coord.begin() + 1;
        v[puncte[i].first].push_back(puncte[i].second);
    }
    build(1, 1, (int) coord.size());
    fin >> q;
    while(q > 0) {
        int xSt, ySt, xDr, yDr;
        fin >> xSt >> ySt >> xDr >> yDr;
        xSt = lower_bound(coord.begin(), coord.end(), xSt) - coord.begin() + 1;
        xDr = upper_bound(coord.begin(), coord.end(), xDr) - coord.begin();
        if(xSt > xDr) {
            fout << "0\n";
        } else {
            fout << query(1, 1, (int) coord.size(), xSt, ySt, xDr, yDr) << "\n";
        }
        q--;
    }
    fin.close();
    return 0;
}