Cod sursa(job #1981587)

Utilizator georgerapeanuRapeanu George georgerapeanu Data 16 mai 2017 09:37:29
Problema Gropi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <cstdio>
#include <algorithm>
#define x first
#define y second
using namespace std;
FILE *f=fopen("gropi.in","r");
FILE *g=fopen("gropi.out","w");
struct zona{
    int lin,ugroapa,st,dr,pgroapa;
};
zona V[100005];
pair<int,int> G[100005];
int C,N,M;
int len=0;
int X,Y,XX,YY;
bool cmp(pair<int,int> a,pair<int,int> b)
{
    return a.y<b.y;
}
int bs(int Y)
{
    int st=1,dr=len;
    while(st<dr)
    {
        int mid=(st+dr+1)/2;
        if(V[mid].st>Y)dr=mid-1;
        else st=mid;
    }
    return st;
}
int main()
{
    fscanf(f,"%d %d",&C,&N);
    for(int i=1;i<=N;i++)
    {
        fscanf(f,"%d %d",&G[i].x,&G[i].y);
    }
    sort(G+1,G+1+N,cmp);
    for(int i=1;i<=N;i++)
    {
        if(G[i].x!=V[len].lin)
        {
            V[len].dr=G[i].y-1;
            ++len;
            V[len].st=G[i].y;
            V[len].lin=G[i].x;
            V[len].pgroapa=i;
        }
        V[len].ugroapa=i;
    }
    V[len].dr=N;
    fscanf(f,"%d",&M);
    for(int i=1;i<=M;i++)
    {
        fscanf(f,"%d%d%d%d",&X,&Y,&XX,&YY);
        if(Y>YY)
        {
            swap(Y,YY);
            swap(X,XX);
        }
        int st,dr;
        st=bs(Y);
        dr=bs(YY);
        int cost=0;
        if(X==V[st].lin)
        {
            if(V[st].ugroapa>X)cost++;
            else cost--;
        }
        if(XX==V[dr].lin)
        {
            if(V[dr].pgroapa<=XX)cost++;
            else cost--;
        }
        fprintf(g,"%d\n",(YY-Y+1)+(dr-st)+cost);
    }
    fclose(f);
    fclose(g);
    return 0;
}