Cod sursa(job #226895)

Utilizator zbarniZajzon Barna zbarni Data 3 decembrie 2008 00:08:51
Problema Gropi Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include<fstream.h>
#include<stdlib.h>
#define g 100
int N,M,C;
struct GROPI
 {
  int x,y,xx,yy;
 };
GROPI gr[g],h[g];

int compare (const void *a, const void *b)
 {
  return (((GROPI*)a)->y-((GROPI*)b)->y);
 }

ifstream be ("gropi.in");
ofstream ki ("gropi.out");

void solve()
 {
  int s,x1,y1,x2,y2,i,j,sz,p;
  be>>M;
  for (i=1;i<=M;i++)
    be>>h[i].x>>h[i].y>>h[i].xx>>h[i].yy;
  for (i=1;i<=M;i++)
   {
    x1=h[i].x;y1=h[i].y;
    x2=h[i].xx; y2=h[i].yy;
    if (y2<y1)
     {
      s=y2;
      y2=y1;
      y1=s;
      s=x2;
      x2=x1;
      x1=s;
     }
    s=x1;
    sz=1;
    p=1;
    while (y1<y2)
     {
      while ((gr[p].x!=s || gr[p].y<y1) && gr[p].y<=y2)
	p++;
      if (gr[p].y<=y2)
       {
	sz+=gr[p].y-y1;
	y1=gr[p].y-1;
	if (s==1)
	  s=2;
	else
	  s=1;
       }
      else
       {
	sz+=y2-y1;
	y1=y2;
	if (s!=x2)
	  sz++;
       }
     }
    ki<<sz<<'\n';
   }
  ki.close();
 }

int main()
 {
  be>>C>>N;
  int i;
  for (i=1;i<=N;i++)
   be>>gr[i].x>>gr[i].y;
  qsort (gr,N+1,sizeof(GROPI),compare);
  solve();
  return 0;
 }