Cod sursa(job #1239785)

Utilizator sebinechitasebi nechita sebinechita Data 9 octombrie 2014 19:55:22
Problema Zoo Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.15 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("zoo.in");
ofstream fout("zoo.out");
#define p pair <int, int>
#define MAX 16010
#define pb push_back
int s=0, n, l, r, v1, v2, st2, dr2;
p b[MAX];

vector< int > a[4*MAX], x;
vector< p > poz[4*MAX];

bool cmp1(p a, p b)
{
    if(a.first==b.first)
        return a.second<b.second;
    return a.first<b.first;
}

void update(int nod, int st, int dr)
{
    unsigned int i, j;
    if(st==dr)
    {
        a[nod].pb(b[st].second);
        return;
    }
    int mij=(st+dr)>>1;

    update(nod<<1, st, mij);
    update((nod<<1)+1, mij+1, dr);
    i=j=0;
    while(i<a[(nod<<1)].size() && j<a[(nod<<1)+1].size())
    {
        if(a[(nod<<1)][i]<a[(nod<<1)+1][j])
        {
            poz[nod].pb(make_pair(i, j-1));
            a[nod].pb(a[(nod<<1)][i++]);
        }
        else
        {
            poz[nod].pb(make_pair(i-1, j));
            a[nod].pb(a[(nod<<1)+1][j++]);
        }
    }
    while(i<a[(nod<<1)].size())
    {
        poz[nod].pb(make_pair(i, j-1));
        a[nod].pb(a[(nod<<1)][i++]);
    }
    while(j<a[(nod<<1)+1].size())
    {
        poz[nod].pb(make_pair(i-1, j));
        a[nod].pb(a[(nod<<1)+1][j++]);
    }
}


void query(int nod, int st, int dr, int ps, int pf)
{
    if(l <= st && dr <= r)
    {

        if(ps>=0)if(a[nod][ps]<v1)
            ps++;
        if(ps<0)
            ps++;
        if(ps<=pf)
        {
            s+=(pf-ps)+1;
        }
        return;
    }

    int mij=(st+dr)>>1, ps1, pf1;
    if(l<=mij)
    {
        if(ps<0)
        {
            ps1=ps;
        }
        else
        {
            ps1=poz[nod][ps].first;
        }
        if(pf<0)
        {
            pf1=pf;
        }
        else
        {
            pf1=poz[nod][pf].first;
        }
        query((1<<nod), st, mij, ps1, pf1);
    }
    if(r>=mij+1)
    {
        if(ps<0)
        {
            ps1=ps;
        }
        else
        {
            ps1=poz[nod][ps].second;
        }
        if(pf<0)
        {
            pf1=pf;
        }
        else
        {
            pf1=poz[nod][pf].second;
        }
        query((nod<<1)+1, mij+1, dr, ps1, pf1);
    }
}

void Q(int st, int dr)
{
    l=st;
    r=dr;
    query(1, 1, n, st2, dr2);
}

unsigned const maxb=8192;
char buf[maxb];
unsigned ptr=maxb-1;

int main()
{
    int st, dr, nod, i, m, x1, y1, y2, x2;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>b[i].first;
        fin>>b[i].second;
    }
    sort(b+1, b+n+1, cmp1);
    update(1, 1, n);
    for(i=1;i<=n;i++)
    {
        x.push_back(b[i].first);
    }
    fin>>m;
    while(m--)
    {
        s=0;
        fin>>x1;
        fin>>y1;
        fin>>x2;
        fin>>y2;
        st=lower_bound(x.begin(), x.end(), x1)-x.begin()+1;
        dr=upper_bound(x.begin(), x.end(), x2)-x.begin();
        st2=lower_bound(a[1].begin(), a[1].end(), y1)-a[1].begin();
        dr2=upper_bound(a[1].begin(), a[1].end(), y2)-1-a[1].begin();
      // if(st<=dr)
       //    Q(st, dr);
        fout<<s<<"\n";
    }

}