题解 | #【模板】前缀和#

【模板】前缀和

https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf

//package 牛客.practice.p3;

import java.util.Scanner;

/**
 * @BelongsProject: homework
 * @Author: Ling xi_Li
 * @CreateTime: 2023-12-22 08-19
 * @Description: TODO 前缀和
 */

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();//数据个数
        int m = in.nextInt();//查询次数
		//数组不能是int不然结果不正确
        long[] arr = new long[n + 1];

        for (int i = 1; i <= n; i++) {
            arr[i] = in.nextLong() + arr[i - 1];
        }

        for (int i = 0; i < m; i++) {
            int begin = in.nextInt();
            int end = in.nextInt();
            System.out.println(arr[end] - arr[begin - 1]);
        }
    }
}

全部评论

相关推荐

2024-12-26 20:46
复旦大学 C++
国棉17厂丶小王:拿了offer的那个周末晚上去网吧通宵,去网吧不知道玩什么刷了lc的每日一题,然后试着第一次打开了三角洲行动,从此少了一个已经刷了700道题的lc用户,但是烽火地带多了一只🐭🐭
点赞 评论 收藏
分享
昨天 10:35
已编辑
西安科技大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务