rgoewedky's blog

By rgoewedky, history, 4 years ago, In English

I am getting TLE in Range Sum Query 1 of CSES problem set. Although, I pre-compute prefix sum and time complexity is also under constraints. Here is my code . Please Help. THANKS in ADVANCE

import java.io.*;
import java.util.*;

public class Main{      
	public static void main(String[] args)throws Exception{
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         
          StringTokenizer st;
         
        st=new StringTokenizer(br.readLine()); 
        int n=Integer.parseInt(st.nextToken());
        int q=Integer.parseInt(st.nextToken());

        long prefix[]=new long[n+1];
        prefix[0]=0;

        st=new StringTokenizer(br.readLine()); 
        for (int i=1;i<=n;i++) {
          int x=Integer.parseInt(st.nextToken());
          prefix[i]=prefix[i-1]+x;
        }

    while(q-->0){
      
       st=new StringTokenizer(br.readLine()); 
      int l=Integer.parseInt(st.nextToken());
      int r=Integer.parseInt(st.nextToken());
       System.out.println(prefix[r]-prefix[l-1]);
	}
}
}


  • Vote: I like it
  • -1
  • Vote: I do not like it

| Write comment?