Cod sursa(job #3160607)

Utilizator divadddDavid Curca divaddd Data 24 octombrie 2023 17:55:07
Problema Zoo Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.23 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3,Ofast")
#define y1 yy1
#define norm normalizare
#define pii pair<int, int>
#define tiii tuple<int, int, int>
using namespace std;
const int NMAX = 16e3+2;
const int MMAX = 1e5+2;
const int VMAX = 1e6+2;
int n,m,sol[MMAX],aib[VMAX];
vector<int> vals;
vector<int> events[VMAX];
vector<tiii> queries[VMAX];

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

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			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;
	}
} fin("zoo.in");
ofstream fout("zoo.out");

struct Point{
    int x, y;
} p[NMAX], qst[MMAX], qdr[MMAX];

void update(int pos, int val){
    pos++;
    for(int i = pos; i < VMAX; i += (i&-i)){
        aib[i] += val;
    }
}

int query(int pos){
    pos++;
    int ans = 0;
    for(int i = pos; i > 0; i -= (i&-i)){
        ans += aib[i];
    }
    return ans;
}

int norm(int x){
    int st = 0, dr = vals.size()-1;
    while(st <= dr){
        int mid = (st+dr)/2;
        if(vals[mid] == x){
            return mid+1;
        }
        if(vals[mid] > x){
            dr = mid-1;
        }else{
            st = mid+1;
        }
    }
}

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++){
        fin >> p[i].x >> p[i].y;
        vals.push_back(p[i].x);
        vals.push_back(p[i].y);
    }
    fin >> m;
    for(int i = 1; i <= m; i++){
        fin >> qst[i].x >> qst[i].y;
        fin >> qdr[i].x >> qdr[i].y;
        vals.push_back(qst[i].x);
        vals.push_back(qst[i].x);
        vals.push_back(qdr[i].x);
        vals.push_back(qdr[i].x);
    }
    sort(vals.begin(), vals.end());
    for(int i = 1; i <= n; i++){
        p[i].x = norm(p[i].x);
        p[i].y = norm(p[i].y);
        events[p[i].y].push_back(p[i].x);
    }
    for(int i = 1; i <= m; i++){
        qst[i].x = norm(qst[i].x);
        qst[i].y = norm(qst[i].y);
        qdr[i].x = norm(qdr[i].x);
        qdr[i].y = norm(qdr[i].y);

        queries[qdr[i].y].push_back({i, qdr[i].x, 1});
        queries[qst[i].y-1].push_back({i, qdr[i].x, -1});
        queries[qdr[i].y].push_back({i, qst[i].x-1, -1});
        queries[qst[i].y-1].push_back({i, qst[i].x-1, 1});
    }
    for(int i = 1; i < VMAX; i++){
        for(int it: events[i]){
            update(it, 1);
        }
        for(auto [idx, pos, coef]: queries[i]){
            sol[idx] += coef*query(pos);
        }
    }
    for(int i = 1; i <= m; i++){
        fout << sol[i] << "\n";
    }
    return 0;
}