Tju : 2346



import java.math.BigInteger;
import java.util.Scanner;

/**
 *
 * @author Md. Shahab Uddin
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner (System.in);
        
        int testCase = input.nextInt();
        int cases = 0;
        
        while ( testCase-- != 0 ) {
            BigInteger first = input.nextBigInteger();
            BigInteger second = input.nextBigInteger();
            BigInteger firstSum;
            BigInteger secondSum;

            System.out.printf ("Scenario #%d:\n", ++cases);
            
            if ( first.compareTo(BigInteger.ZERO) < 0 && second.compareTo(BigInteger.ZERO) < 0 ) {
                first = first.multiply(BigInteger.valueOf(-1));
                second = second.add(BigInteger.ONE);
                second = second.multiply(BigInteger.valueOf(-1));
                firstSum = (first.multiply(first.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
                secondSum = (second.multiply(second.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
                //System.out.println (firstSum);
                //System.out.println (secondSum);
                //System.out.println ("-");
                System.out.println (secondSum.subtract(firstSum));
            }

            else if ( first.compareTo(BigInteger.ZERO) >= 0 && second.compareTo(BigInteger.ZERO) >= 0 ) {
                if ( first.compareTo(BigInteger.ZERO) > 0 )
                    first = first.subtract(BigInteger.ONE);
                firstSum = (first.multiply(first.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
                secondSum = (second.multiply(second.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
                System.out.println (secondSum.subtract(firstSum));
                //System.out.println ("both plus");
            }

            else {
                first = first.multiply(BigInteger.valueOf(-1));
                firstSum = (first.multiply(first.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
                //firstSum = firstSum.multiply(BigInteger.valueOf(-1));
                secondSum = (second.multiply(second.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
                System.out.println (secondSum.subtract(firstSum));
            }

            System.out.println ();
        }

    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s