返回列表 發帖
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;

  3. public class P1 {

  4.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  5.         String str, raw[];
  6.         int n, m, sum[];
  7.         P1() throws Exception
  8.         {
  9.                 while((str=br.readLine())!=null)
  10.                 {
  11.                         raw=str.split(" ");
  12.                         n=Integer.parseInt(raw[0]);
  13.                         m=Integer.parseInt(raw[1]);
  14.                         sum=new int[n+1];
  15.                         raw=br.readLine().split(" ");
  16.                         for(int i=1; i<=n; i++)
  17.                         {
  18.                                 int t=Integer.parseInt(raw[i-1]);
  19.                                 sum[i]=sum[i-1]+t;
  20.                         }
  21.                         while(m>0)
  22.                         {
  23.                                 raw=br.readLine().split(" ");
  24.                                 int l=Integer.parseInt(raw[0]);
  25.                                 int r=Integer.parseInt(raw[1]);
  26.                                 System.out.println(getSum(l, r));
  27.                                 m--;
  28.                         }
  29.                 }
  30.         }

  31.         int getSum(int l, int r)
  32.         {
  33.                 return sum[r]-sum[l-1];
  34.         }

  35.         public static void main(String[] args) throws Exception{
  36.                 new P1();
  37.         }
  38. }
複製代碼

TOP

返回列表