Pagini recente » Diferente pentru warm-up-2019/solutii/shoturi intre reviziile 94 si 95 | Cod sursa (job #2006134) | Cod sursa (job #428768) | Cod sursa (job #1878239) | Cod sursa (job #207046)
Cod sursa(job #207046)
#include <stdio.h>
#include <bitset>
using namespace std;
bitset <131072> line;
long col[131072],ind[131072],v[131072],s[131071];
int n,m,g,T,i,l,c,x,y,aux,sol;
int comp(const void * n1, const void * n2){
return col[*((long*)n1)]-col[*((long*)n2)];
}
int search(int x){
int low=1,high=m+1,mid;
while (low<high){
mid=(low+high)/2;
if (v[mid]<x)
low=mid+1;
else high=mid;
}
return low;
}
int bs2(int x){
int low=1,high=g+1,mid;
while (low<high){
mid=(low+high)/2;
if (col[ind[mid]]<x)
low=mid+1;
else high=mid;
}
return low;
}
int main(){
freopen("gropi.in","r",stdin);
freopen("gropi.out","w",stdout);
scanf("%ld %ld",&n,&g);
for (i=1;i<=g;++i){
scanf("%ld %ld",&l,col+i);
line[i]=l-1;
ind[i]=i;
}
//determinare numar de schimbari de linie
qsort(ind+1,g,sizeof(long),comp);
s[0]=0;l=0;
for (i=2;i<=g;++i)
if (line[ind[i-1]]!=line[ind[i]]){++l;s[l]=s[l-1]+1;v[l]=col[ind[i]];}
m=l;
scanf("%ld",&T);
for (;T;--T){
scanf("%ld %ld %ld %ld",&l,&c,&x,&y); l--;x--;
if (c>y){aux=c;c=y;y=aux;aux=l;l=x;x=aux;}
aux=search(c);
if (v[aux]!=c)
sol=-s[aux-1];
else sol=-s[aux];
aux=bs2(c);
if (col[ind[aux]]==c)aux++;
if (l==line[ind[aux]])sol++;
aux=search(y);
sol+=s[aux-1];
aux=bs2(y);aux--;
if (x==line[ind[aux]])sol++;
printf("%ld\n",sol+y-c+1);
}
return 0;
}