Cod sursa(job #2496182)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 20 noiembrie 2019 13:01:25
Problema Ograzi Scor 0
Compilator cpp-64 Status done
Runda casiaiziscanudaisimulareprimaora Marime 2.48 kb
#include <iostream>
#include <vector>
using namespace std;
const int maxval = 100010;
const int maxn = 50005;
vector <int> query_lin[maxval];
vector <int> gigi[maxval];
int aint[maxval * 4];


#define DIM 10000
char buff[DIM];
int poz=0;

void cit(int &numar)
{
     numar = 0;
     //cat timp caracterul din buffer nu e cifra ignor
     while (buff[poz] < '0' || buff[poz] > '9')
          //daca am "golit" bufferul atunci il umplu
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     //cat timp dau de o cifra recalculez numarul
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

void update(int nod, int st, int dr, int poz, int val)
{
    if(poz < st || poz > dr)
        return;
    if(st == dr)
    {
        aint[nod] += val;
        return;
    }
    int mij = (st + dr) / 2;
    update(nod * 2, st, mij, poz, val);
    update(nod * 2 + 1, mij + 1, dr, poz, val);
    aint[nod] = aint[nod * 2] + aint[nod * 2 + 1];
}

int query(int nod, int st, int dr, int x, int y)
{
    if(st > y || dr < x)
        return 0;
    if(x <= st && dr <= y)
        return aint[nod];
    int mij = (st + dr) / 2;
    int aux1 = query(nod * 2, st, mij, x, y);
    int aux2 = query(nod * 2 + 1, mij + 1, dr, x, y);
    return aux1 + aux2;
}

int main()
{
    freopen("ograzi.in", "r", stdin);
    freopen("ograzi.out", "w", stdout);
    int n, m, w, h;
    cit(n);
    cit(m);
    cit(w);
    cit(h);
    int maxcoord = 0;
    for(int i = 1; i <= n; i++)
    {
        int x, y;
        cit(x);
        cit(y);
        swap(x, y);
        query_lin[x].push_back(y);
        maxcoord = max(maxcoord, x);
        maxcoord = max(maxcoord, y);
    }
    for(int i = 1; i <= m; i++)
    {
        int x, y;
        cit(x);
        cit(y);
        swap(x, y);
        gigi[x].push_back(y);
        maxcoord = max(maxcoord, x);
        maxcoord = max(maxcoord, y);
    }
    int nr = 0;
    for(int i = 0; i <= maxcoord; i++)
    {
        for(auto it : gigi[i])
            update(1, 1, maxcoord, it, 1);
        if(i >= h)
            for(auto it : gigi[i - h])
                update(1, 1, maxcoord, it, -1);
        if(i - h + 1 >= 0)
            for(auto it : query_lin[i - h + 1])
                nr = nr + query(1, 1, maxcoord, it, it + w - 1);
    }
    printf("%d\n", nr);
    return 0;
}