Cod sursa(job #3160591)

Utilizator divadddDavid Curca divaddd Data 24 octombrie 2023 17:41:48
Problema Zoo Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.12 kb
#include <bits/stdc++.h>
#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];
map<int, int> norm;
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");
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;
    }
} 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 main()
{
    fin >> n;
    for(int i = 1; i <= n; i++){
        fin >> p[i].x >> p[i].y;
        norm[p[i].x] = norm[p[i].y] = i;
    }
    fin >> m;
    for(int i = 1; i <= m; i++){
        fin >> qst[i].x >> qst[i].y;
        fin >> qdr[i].x >> qdr[i].y;
        norm[qst[i].x] = norm[qst[i].y] = i;
        norm[qdr[i].x] = norm[qdr[i].y] = i;
    }
    int cnt = 0;
    for(auto &it: norm){
        it.second = ++cnt;
    }
    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;
}