Your code has runtime error when* N *is *1*

Removing the following two lines (and also change the start index of the 
two loops from 2 to 1) should fix your problem:


*a1[1]=-1*a[1];*
*a2[1]=-2*a[1];*

They will cause out of bound error when N is 1

A full passing solution is attached for your reference as well.
---------------

*import java.io.*;*
*import java.math.*;*
*import java.security.*;*
*import java.text.*;*
*import java.util.*;*
*import java.util.concurrent.*;*
*import java.util.function.*;*
*import java.util.regex.*;*
*import java.util.stream.*;*
*import static java.util.stream.Collectors.joining;*
*import static java.util.stream.Collectors.toList;*

*public class Solution*
*{*

*    static long getSum(int index, long[] BITree)*
*    {*
*        long sum = 0; index = index + 1;*
*        while (index > 0)*
*        {*
*            sum += BITree[index];*
*            index -= index & (-index);*
*        }*
*        return sum;*
*    }*
*    public static void updateBIT(int n, int index, long val, long[] 
BITree)*
*    {*
*        index = index + 1;*
*        while (index <= n)*
*        {*
*            BITree[index] += val;*
*            index += index & (-index);*
*        }*
*    }*
*    static void constructBITree(long arr[], int n, long[] BITree)*
*    {*
*        for (int i = 1; i <= n; i++)*
*            BITree[i] = 0;*

*        for (int i = 0; i < n; i++)*
*            updateBIT(n, i, arr[i], BITree);*
*    }*

*    public static void main(String[] args)*
*    {*
*        Scanner in = new Scanner(System.in);*
*        int t = in.nextInt();*
*        int test = 1;*
*        while (t > 0)*
*        {*
*            t--;*
*            System.out.print("Case #" + test + ": ");*
*            test++;*
*            int n = in.nextInt();*
*            int q = in.nextInt();*
*            long[]a = new long[n];*
*            for (int i = 0; i < n; i++)*
*                a[i] = in.nextLong();*
*            long[] a1 = new long[n];*
*            long[] a2 = new long[n];*
*            long[] BITree1 = new long[n + 5];*
*            long[] BITree2 = new long[n + 5];*
*            a1[0] = a[0];*
*            for (int i = 1; i < n; i++)*
*            {*
*                int c = ((i & 1) == 1) ? -1 : 1;*
*                a1[i] = c * (long)a[i];*
*            }*
*            constructBITree(a1, n, BITree1);*
*            a2[0] = a[0];*
*            for (int i = 1; i < n; i++)*
*            {*
*                int c = ((i & 1) == 1) ? -1 : 1;*
*                a2[i] = (i + 1)*c*(long)a[i];*
*            }*
*            constructBITree(a2, n, BITree2);*
*            BigInteger ans = new BigInteger("0");*
*            while (q > 0)*
*            {*
*                q--;*
*                char c = (in.next()).charAt(0);*
*                if (c == 'U')*
*                {*
*                    int x = in.nextInt();*
*                    long v = in.nextLong();*
*                    x--;*
*                    long val1 = 0, val2 = 0;*
*                    if ((x & 1) == 1)*
*                    {*
*                        val1 = -1 * v;*
*                        val2 = -(long)(x + 1)*v;*
*                    }*
*                    else*
*                    {*
*                        val1 = v;*
*                        val2 = (long)(x + 1)*v;*
*                    }*
*                    updateBIT(n, x, val1 - a1[x], BITree1);*
*                    updateBIT(n, x, val2 - a2[x], BITree2);*
*                    a1[x] = val1;*
*                    a2[x] = val2;*
*                }*
*                else*
*                {*
*                    int l = in.nextInt();*
*                    int r = in.nextInt();*
*                    l--;*
*                    r--;*
*                    long p1 = (l == 0) ? 0 : getSum(l - 1, BITree2);*
*                    long p = (l == 0) ? 0 : getSum(l - 1, BITree1);*
*                    long sum1 = getSum(r, BITree2) - p1;*
*                    long sum = getSum(r, BITree1) - p;*
*                    //System.out.println(sum+" "+sum1);*
*                    if ((l & 1) == 1)*
*                    {*
*                        sum *= -1L;*
*                        sum1 *= -1L;*
*                    }*
*                    ans = ans.add(BigInteger.valueOf(sum1 - l * sum));*
*                    //System.out.println(ans);*
*                }*
*            }*
*            System.out.println(ans);*
*        }*
*    }*
*}*


-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/079ab37d-71a8-4f56-93ed-f51993b58398%40googlegroups.com.

Reply via email to