Sunday, 27 May 2012

UVa 10642 Can You Solve It? Solution


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#define abs(a) a<0 ? (-a) : (a)
using namespace std;

int main()
{
    long long int xx1,yy1,a,b,x1,i,x2,y1,y2,dist,tc=1,crdist,down,up,cnt=1;


    scanf("%lld",&tc);
    while( tc--)
    {

        scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);


        a = (x1+y1);
        b = (x2+y2);

        if(b<a)
        {
            xx1 = x2;
            x2 = x1;
            x1 = xx1;

            yy1 = y2;
            y2 = y1;
            y1 = yy1;

        }


        if(a == b)
            printf("Case %lld: %lld\n",cnt++,abs(x2-x1));


        else{

            up = y1;
            down  = x2;
            dist = 0;

            long long int a1=a,b1=b;

            if(a1>b1) {a1=b;b1=a;}


            for(i = a1+1; i < b1; i++)
                dist += (i+1);

            crdist = up + down + dist + 1;
        printf("Case %lld: %lld\n",cnt++,crdist);
        }
    }
    return 0;
}

2 comments: