Cod sursa(job #2039993)

Utilizator mari2001Maria Ionescu mari2001 Data 15 octombrie 2017 11:43:59
Problema Gropi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include<cstdio>
#include<algorithm>
using namespace std;
struct zona{
int x,y,tip,last;
}v[100005];
struct trecere{
int tip,x;
}g[100005];
int cost(int nr,int z){
int st=1,dr=z,mij;
while(st<=dr){
mij=(st+dr)/2;
if (v[mij].x<=nr && v[mij].y>=nr)
return mij;
if (v[mij].x>nr)
dr=mij-1;
if (v[mij].y<nr)
st=mij+1;}
return -1;}
int cost2(int nr,int n){
int st=1,dr=n,mij,last=-1;
while(st<=dr){
mij=(st+dr)/2;
if (g[mij].x<=nr){
last=mij;
st=mij+1;}
else
dr=mij-1;}
return last;}
int cmp(trecere a,trecere b){
return a.x<b.x;}
int main(){
freopen("gropi.in","r",stdin);
freopen("gropi.out","w",stdout);
int c,n,m,i,z=0,x,y,t1,t2,gr1,gr2;
long long rasp=0;
scanf("%d%d",&c,&n);
for(i=1;i<=n;i++)
scanf("%d%d",&g[i].tip,&g[i].x);
sort(g+1,g+n+1,cmp);
v[++z].x=1;
v[z].tip=g[1].tip;
for(i=1;i<=n;i++)
if (g[i].tip!=v[z].tip){
v[z].y=g[i].x-1;
v[z].last=i-1;
v[++z].x=g[i].x;
v[z].tip=g[i].tip;}
v[z].y=c;
v[z].last=n;
scanf("%d",&m);
for(i=1;i<=m;i++){
scanf("%d%d%d%d",&t1,&x,&t2,&y);
if (x>y){
swap(x,y);
swap(t1,t2);}
int z1=cost(x,z),z2=cost(y,z);
if (x==y){
if (t1==t2)
printf("1\n");
else
printf("2\n");
continue;}
rasp=1LL*(y-x+1);
if (z1==z2){
if (t1==t2 && t1==v[z1].tip){
gr1=cost2(x,n);
gr2=cost2(y,n);
if (gr1==gr2)
;
else
rasp=rasp+1LL*2;}
else{
if (t1==v[z1].tip)
rasp++;
if (t2==v[z2].tip)
rasp++;}}
else{
if (t1==v[z1].tip){
if (x>g[v[z1].last].x)
rasp--;
else
rasp++;}
if (t2==v[z2].tip)
rasp++;
rasp=rasp+1LL*(z2-z1);}
printf("%lld\n",rasp);}
return 0;}