Cod sursa(job #2443077)

Utilizator DavidLDavid Lauran DavidL Data 26 iulie 2019 13:16:39
Problema Ograzi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <bits/stdc++.h>
using namespace std;
/* 
ifstream fi("ograzi.in");
ofstream fo("ograzi.out");
#define cin fi
#define cout fo*/

const int NMAX = 1e6 + 10;

int n, m, w, h;
vector <int> dr[NMAX], oi[NMAX];
int aib[NMAX];

inline int lsb(int x)
{
    return (x & (-x));
}

void update(int poz, int val)
{
    for (int i = poz; i < NMAX; i += lsb(i))
        aib[i] += val;
}

int getSum(int poz)
{
    int ret = 0;
    for (int i = poz; i; i -= lsb(i))
        ret += aib[i];
    return ret;
}

int query(int st, int dr)
{
    if (st == 0)
        return getSum(dr);
    return getSum(dr) - getSum(st - 1);
}

int main()
{
    cin >> n >> m >> w >> h;
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        dr[y].push_back(x);
    }

    for (int i = 1; i <= m; i++)
    {
        int x, y;
        cin >> x >> y;
        oi[y].push_back(x);
    }

    int rez = 0;

    // liniile 0...h
    for (int i = 0; i <= h; i++)
    {
        for (auto x: oi[i])
        {
            // oaie pe (i, y)
            update(x, 1);
        }
    }
    for (auto da: dr[0])
    {
        rez += query(da, da + w);
    }

    for (int i = h + 1; i <= 1e6 + 5; i++)
    {
        // bag linia i, scot linia i-h-1
        for (auto x: oi[i])
            update(x, 1);
        for (auto x: oi[i - h - 1])
            update(x, -1);

        for (auto da: dr[i - h])
        {
            rez += max(0, query(da, da + w));
        }
    }

    for (int i = 1e6 + 6; i <= 2e6 + 5; i++)
        for (auto da: dr[i - h])
        {
            rez += max(0, query(da, da + w));
        }

    cout << rez << "\n";

    cout << (sizeof(dr) + sizeof(oi) + sizeof(aib)) / 1000;

    return 0;
}