Cod sursa(job #3187212)

Utilizator The_SupremacySus Impostor The_Supremacy Data 28 decembrie 2023 09:03:07
Problema Ograzi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.46 kb
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>

using namespace std;
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;
	}
};
const int NMAX = 4e5;
const int VALMAX = 1e6+10;
struct numere{
    int tip,y1,y2,x,indice,indicerez;
}v[NMAX+5];
int rez[NMAX+5];
bool cmp(numere a,numere b){
    if(a.x!=b.x){
        return a.x<b.x;
    }
    return a.tip<b.tip;
}
int aib[VALMAX+5];
void update(int poz,int val){
    while(poz<=VALMAX){
        aib[poz]+=val;poz+=poz&-poz;
    }
}
int query(int poz){
    int cnt=0;
    while(poz>0){
        cnt+=aib[poz];poz-=poz&-poz;
    }
    return cnt;
}
int main()
{
    InParser fin("ograzi.in");
    ofstream fout("ograzi.out");
    int n,m,w,h,poz=0,a,b;fin>>n>>m>>w>>h;
    int lim=1e6+2;
    for(int i=1;i<=n;i++){
        fin>>a>>b;a++;b++;
        int x1=a,y1=b,x2=a+w,y2=b+h;
        if(x2>lim){x2=lim+1;}
        if(y2>lim){y2=lim+1;}
        poz++;v[poz].x=x1;v[poz].y1=y1;v[poz].y2=y2;v[poz].tip=0;v[poz].indice=poz;
        poz++;v[poz].x=x2;v[poz].y1=y1;v[poz].y2=y2;v[poz].tip=2;v[poz].indice=poz;v[poz].indicerez=poz-1;
    }
    for(int i=1;i<=m;i++){
        fin>>a>>b;a++;b++;
        poz++;v[poz].x=a;v[poz].y1=b;v[poz].tip=1;
    }
    sort(v+1,v+poz+1,cmp);
    int total=0;
    for(int i=1;i<=poz;i++){
        if(v[i].tip==1){
            update(v[i].y1,1);
        }
        else if(v[i].tip==0){
            rez[v[i].indice]=query(v[i].y2)-query(v[i].y1-1);
        }
        else if(v[i].tip==2){
            total+=query(v[i].y2)-query(v[i].y1-1)-rez[v[i].indicerez];
        }
    }
    fout<<total<<'\n';
    return 0;
}