Cod sursa(job #197648)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 5 iulie 2008 13:14:10
Problema Gropi Scor 0
Compilator c Status done
Runda Junior Challenge 2008 Marime 3.76 kb
#include <stdio.h>
#include <stdlib.h>
#define fin "gropi.in"
#define fout "gropi.out"
#define N 100010
typedef struct groapa groapa;
struct groapa{
       int x,y;
};
groapa gr[N];
int c,n,m;
void scan(void){
     int i;
     freopen(fin,"r",stdin);
     freopen(fout,"w",stdout);
     scanf("%d%d",&c,&n);
     for (i=1;i<=n;++i)
         scanf("%d%d",&gr[i].x,&gr[i].y);
     scanf("%d",&m);
     gr[++n]=(groapa){1,c+1};
     gr[++n]=(groapa){2,c+1};
}
inline void swap(int a,int b){
     groapa aux;
     aux=gr[a];
     gr[a]=gr[b];
     gr[b]=aux;
}
inline int cmp(groapa a,groapa b){
       if (a.y>b.y)
          return 1;
       if (a.y<b.y)
          return -1;
       if (a.x>b.x)
          return 1;
       if (a.x<b.x)
          return -1;
       return 1;
}
inline void down_heap(int i,int n){
     int max=i;
     if (2*i<=n && cmp(gr[max],gr[2*i])<0)
       max=2*i;
     if (2*i+1<=n && cmp(gr[max],gr[2*i+1])<0)
        max=2*i+1;
     if (i!=max){
        swap(i,max);
        down_heap(max,n);
     }
}
void sort(void){
     int i;
     for (i=n/2;i;--i)
         down_heap(i,n);
     for (i=n;i>1;--i){
         swap(1,i);
         down_heap(1,i-1);
     }
}
int cat1[N],cat2[N]; 
int ce1[N],ce2[N];
void pre(void){
     int i,last=1;
     for (i=1;i<=N;++i){
         ce1[i]=last;
         if (last == gr[i].x){
            cat1[i]=cat1[i-1]+1;
            if (last==1)
               last=2;
            else
                last=1;
         }
         else
             cat1[i]=cat1[i-1];
     }
     last=2;
     for (i=1;i<=N;++i){
         ce2[i]=last;
         if (last == gr[i].x){
            cat2[i]=cat2[i-1]+1;
            if (last==1)
               last=2;
            else
                last=1;
         }
         else
             cat2[i]=cat2[i-1];
     }
}    
int b_search(int p,int u,groapa key){
     int m=(p+u)/2;
     while (p<u){
           if (cmp(gr[m],key)<0)
              p=m+1;
           else
               u=m;
           m=(p+u)/2;
     }
     if (cmp(gr[p],key)>=0)
        --p;
     if (cmp(gr[p],gr[n])>0)
        ++p;
     return p;
}
/*int mat1[2][N],mat2[N];
#define mat1 mat1-1
#define mat2 mat2-1
void pre_2(void){
     for (i=1;i<=n;++i)
}*/
int langa(groapa x,groapa start,int a){
    if (a>n)
       return 0;
    if (start.x==1){
       if (ce1[a]==x.x)
          return 0;
       return 1;
    }
    if (ce2[a]==x.x)
       return 0;
    return 1;
}
int langa_st(groapa fi,groapa st,int a){
     int i;
     if (ce2[a]==ce1[a] && ce1[a]!=st.x)
        return 1;
     return 0;
}
//int deasupra(groapa x,int i){
//}
void solve(void){
     int i,x,y,pos1,pos2,sol;
     groapa aux,st,fi;
     pre();
     //cat1[n+1]=cat1[n];
     //cat2[n+1]=cat2[n];
     //ce1[n+1]=ce1[n];
     //ce2[n+1]=ce2[n];
     while (m--){
           pos1=0;pos2=0;
           scanf("%d%d%d%d",&st.x,&st.y,&fi.x,&fi.y);
           if (cmp(st,fi)>0){
                aux=st;
                st=fi;
                fi=aux;
           }
           x=b_search(1,n,st);
           y=b_search(1,n,fi);
           //if (deasupra(st,x))
              //++pos1;
           if (langa_st(fi,st,x+1))
              ++pos1;
           if (langa(fi,st,y+1))
              ++pos2;
           
           sol=fi.y-st.y+1;
           //printf("s%d ",sol);
           if (st.x==1)
              sol+=cat1[y]-cat1[x];
           else
               sol+=cat2[y]-cat2[x];
           //printf("s%d ",sol);
           sol+=pos2+pos1;
           //printf("%d %d %d %d %d\n",pos1,pos2,x,y,sol);
           printf("%d\n",sol);
     }             
}
void print(void){
}
int main(void){
    scan();
    sort();
    solve();
    print();
    return 0;
}