WA in CAKE1AM

I am getting WA. Algorithm looks correct to me. Am I missing any corner cases?

main()
{
ll tst;
cin>>tst;
while(--tst)
{
    ll x1,y1,x2,y2,X1,Y1,X2,Y2,ans=0,overlap=0,startx,endx,starty,endy,ans1,ans2;
    cin>>x1>>y1>>x2>>y2;
    cin>>X1>>Y1>>X2>>Y2;
    ans1= (x2-x1)*(y2-y1) ;
    ans2=(X2-X1)*(Y2-Y1);
    if(ans1>=0) ans+= ans1; else ans-=ans1;
    if(ans2>=0) ans+= ans2; else ans-=ans2;
        startx = (X1>x1)? X1:x1;
        endx = (x2<X2)? x2:X2;
        starty = (Y1>y1)? Y1:y1;
        endy = (y2<Y2)? y2:Y2;
        if(startx<endx && endy>starty)
        ans-= (endx-startx)*(endy-starty);



    cout<<ans<<endl;
}

}

Your algo may not work fine for every case that is possible.

Check this editorial for different cases that could be possible.

editorial_cake1am

also this is getting wrong in your code

while(--t)

change it to while(t--)

because in the earlier case condition goes to zero before checking the last case of the test cases.
For example, if t=1, while loop doesn’t gets executed.

1 Like