当前位置: 首页 > article >正文

前缀和列题AcWing795



import java.io.*;

//前缀和:改题目要求 求一串整数数列中l~r中的数的和
//可以提前算好前n项的和s[n],然后用s[n] - s[n - 1]

public class Main {
	static int n,m,l,r;
	static int N = 100010;
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	static int[] g = new int[N];
	public static void main(String[] args) throws IOException {
		String[] init = in.readLine().split(" ");
		n = Integer.parseInt(init[0]);
		m = Integer.parseInt(init[1]);
		
		init = in.readLine().split(" ");
		for(int i = 1;i <= n;i ++) {
			g[i] = g[i - 1] + Integer.parseInt(init[i - 1]);
		
		}
		while(m-- > 0) {
			init = in.readLine().split(" ");
			l = Integer.parseInt(init[0]);
			r = Integer.parseInt(init[1]);
			out.write((g[r] - g[l - 1]) + "\n");
		}
		in.close();
		out.flush();
	}
}

前缀和例题:二维前缀和

                   二维前缀和-2 


http://www.kler.cn/news/156060.html

相关文章:

  • 本地缓存和分布式缓存
  • 冗余链路和生成树协议
  • 【Java 基础】18 I/O流
  • Spring Boot 集成 spring security 01
  • 【网络安全】下载并安装 kali 的虚拟机 版本
  • 总结1073
  • 【LangChain实战】开源模型学习(1)-ChatGLM2-6B
  • 【扫雷】C语言实现扫雷小游戏
  • Java面试题(每天10题)-------连载(41)
  • 后端返回图片流前端展示图片
  • git stash save untracked not staged
  • TCP 基本认识
  • [WP] ISCTF2023 Web 部分题解
  • Amazon CodeWhisperer 使用体验
  • 6-55.汽车类的继承
  • Cmkae外部依赖管理
  • qt5.15播放音频示例(4种方法)
  • 【开源】基于Vue.js的智慧社区业务综合平台
  • Python网络爬虫练习
  • Ubuntu系统配置深度学习环境之nvidia显卡驱动和cuda安装
  • 【算法】滑动窗口题单——5.多指针滑动窗口醒醒⭐
  • vue el-radio-group多选封装及使用
  • pytorch 中的dim 的作用范围
  • Promise自定义封装
  • react native 环境准备
  • 三极管在数字电路中的应用
  • PyQt6 QToolButton工具按钮控件
  • Nacos源码解读04——服务发现
  • linux 内核regulator
  • 记录 | linux查看文件夹大小