Cod sursa(job #3165245)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 5 noiembrie 2023 18:28:19
Problema Zoo Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.36 kb
#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const char sp = ' ';
const int inf = 0x3f3f3f3f;
const long long INF = 1000000000000000000;
const int mod = 1e9 + 7;
const char out[2][4]{ "NO", "YES" };
#define all(A) A.begin(), A.end()
using ll = long long;
ifstream fin("zoo.in");
ofstream fout("zoo.out");

#define variableName(var) #var
#define __debug(var) cout << #var << " = " << var << '\n'
inline int rint(int a, int b) { return uniform_int_distribution<int>(a, b)(rng); }

const int nmax = 16000;
int n, m;
pair<int, int> v[nmax + 5];
vector<vector<int>> sums;

bool normalize(map<int, int>& norm, int& l, int& r) {
    if (r < norm.begin()->first || l > norm.rbegin()->first) {
        return true;
    }
    l = norm.lower_bound(l)->second;
    r = prev(norm.upper_bound(r))->second;
    return false;
}

int sum(int lx, int ly, int rx, int ry) {
    return sums[rx][ry] + sums[lx - 1][ly - 1] - sums[lx - 1][ry] - sums[rx][ly - 1];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    fin >> n;
    map<int, int> normx, normy;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i].first >> v[i].second;
        normx[v[i].first] = 0;
        normy[v[i].second] = 0;
    }
    for (auto& p : normx) {
        static int cnt = 0;
        p.second = ++cnt;
    }
    for (auto& p : normy) {
        static int cnt = 0;
        p.second = ++cnt;
    }
    int h = normx.rbegin()->second;
    int w = normy.rbegin()->second;
    sums.assign(h + 1, vector<int>(w + 1));
    for (int i = 1; i <= n; ++i) {
        v[i].first = normx[v[i].first];
        v[i].second = normy[v[i].second];
        sums[v[i].first][v[i].second]++;
    }
    for (int i = 1; i <= h; ++i) {
        for (int j = 1; j <= w; ++j) {
            sums[i][j] += sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1];
        }
    }
    fin >> m;
    while (m--) {
        pair<int, int> p, q;
        fin >> p.first >> p.second >> q.first >> q.second;
        // normalizam capetele chenarului
        if (normalize(normx, p.first, q.first) || normalize(normy, p.second, q.second)) {
            fout << 0 << nl;
            continue;
        }
        fout << sum(p.first, p.second, q.first, q.second) << nl;
    }
}