Cod sursa(job #979772)

Utilizator DaNutZ2UuUUBB Bora Dan DaNutZ2UuU Data 2 august 2013 18:40:47
Problema Gropi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include<cstdio>
#include<algorithm>
#define f first
#define s second
#define mp make_pair
using namespace std;
pair<int, int> v[100010];
int s[100010],n,m,c;
int caut1(int X)
{
int st=0,dr=n-1,mij;
while(st!=dr)
{
mij=(st+dr)>>1;
if(v[mij].f<=X)
st=mij+1;
else
dr=mij;
}
return st;
}
int caut2(int X)
{
int st=0,dr=n-1,mij;
while(st!=dr)
{
mij=(st+dr+1)>>1;
if(v[mij].f<X)
st=mij;
else
dr=mij-1;
}
return st;
}
int main()
{
freopen("gropi.in","r",stdin);
freopen("gropi.out","w",stdout);
scanf("%d%d",&c,&n);
int i;
for(i=0; i<n; i++)
scanf("%d%d",&v[i].s,&v[i].f);
v[n++]=mp(0,0);
v[n++]=mp(c+1,0);
sort(v,v+n);
s[0]=v[0].s!=v[1].s;
for(i=1; i<n-1; i++)
s[i]=s[i-1]+(v[i].s!=v[i+1].s);
scanf("%d",&m);
for(i=0; i<m; i++)
{
pair<int, int> a,b;
int poz1,poz2;
scanf("%d%d%d%d",&a.s,&a.f,&b.s,&b.f);
if(a>b)
swap(a,b);
poz1=caut1(a.f);
if(b.f<=v[poz1].f)
printf("%d\n",b.f-a.f+1+(a.s!=b.s));
else
{
poz2=caut2(b.f);
int pasi=s[poz2-1]-s[poz1-1];
if(a.s==v[poz1].s)
pasi++;
if(b.s==v[poz2].s)
pasi++;
pasi+=b.f-a.f+1;
printf("%d\n",pasi);
}
}
return 0;
}