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

23、区间和

区间和

题目描述

假定有一个无限长的数轴,数轴上每个坐标上的数都是0。

现在,我们首先进行 n 次操作,每次操作将某一位置x上的数加c。

接下来,进行 m 次询问,每个询问包含两个整数l和r,你需要求出在区间[l, r]之间的所有数的和。

输入格式

第一行包含两个整数n和m。

接下来 n 行,每行包含两个整数x和c。

再接下里 m 行,每行包含两个整数l和r。

输出格式

共m行,每行输出一个询问中所求的区间内数字和。

数据范围

$ −109≤x≤109,$
$ 1≤n,m≤10^5,$
$ −109≤l≤r≤109,$
$ −10000≤c≤10000$

输入样例:3 3
1 2
3 6
7 5
1 3
4 6
7 8输出样例:8
0
5

Solution

  1. 用题目中会用到的数字最多有 3 * 10^5,可以用来离散化表示 10^9
  2. alls 存所有用到的下标,adds 存所有添加操作,querys 存所有查询操作
  3. a 存执行添加操作之后的下标的值,q 存前缀和

用二维数组代替 Pairs,用数组代替 List,速度快了一倍

import java.util.*;
import java.io.*;class Main{static final int N = 100010;static int[] a = new int[3 * N];static int[] q = new int[3 * N];static int[][] adds = new int[N][2];static int[][] querys = new int[N][2];static int[] alls = new int[3 * N];public static int unique(int[] alls, int n){// 双指针int j = 0;for(int i = 0; i < n; i++){if(i == 0 || alls[i] != alls[i - 1]){alls[j] = alls[i];j++;}}return j;}public static int bsearch(int[] alls, int n, int x){int low = 0, high = n - 1;while(low <= high){int mid = low + high >> 1;if(alls[mid] > x) high = mid - 1;else if(alls[mid] < x) low = mid + 1;else return mid + 1;}return low;}public static void main(String[] args) throws IOException{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));String[] s = br.readLine().split(" ");int n = Integer.parseInt(s[0]);int m = Integer.parseInt(s[1]);// 下标int idx = 0, idx1 = 0;// 存插入for(int i = 0; i < n; i++){s = br.readLine().split(" ");int x = Integer.parseInt(s[0]);int c = Integer.parseInt(s[1]);alls[idx++] = x;adds[i][0] = x;adds[i][1] = c;}for(int i = 0; i < m; i++){s = br.readLine().split(" ");int l = Integer.parseInt(s[0]);int r = Integer.parseInt(s[1]);alls[idx++] = l;alls[idx++] = r;querys[i][0] = l;querys[i][1] = r;}// alls(0, idx - 1) 排序Arrays.sort(alls, 0, idx);// 去重int end = unique(alls, idx);// 添加操作for(int i = 0; i < n; i++){// 二分查找找到 x 在 alls 数组中的下标int t = bsearch(alls, end, adds[i][0]);a[t] += adds[i][1];}// 计算前缀和for(int i = 1; i <= end; i++){q[i] = q[i - 1] + a[i];}for(int i = 0; i < m; i++){int l = bsearch(alls, end, querys[i][0]);int r = bsearch(alls, end, querys[i][1]);bw.write(q[r] - q[l - 1] + "\n");}bw.close();br.close();}
}

用了 List 和 Pairs,效率不高

import java.util.*;
import java.io.*;public class Main{static class Pairs{int first, second;public Pairs(int first, int second){this.first = first;this.second = second;}}public static void main(String[] args) throws IOException{BufferedReader in = new BufferedReader(new InputStreamReader(System.in));BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));String[] s = in.readLine().split(" ");int n = Integer.parseInt(s[0]);int m = Integer.parseInt(s[1]);// 数据范围为 10 的 9 次方,如果直接一个数组来存,空间就会超出限制// 但是操作数和查询数只有 10 的 5 次方// 想到用离散化的方式,用 10 的 5 次方的长度表示 10 的 9 次方的量级// 申请的数组长度 n + m + m + 10 就可以,加个 10 防止边界问题int N = n + m + m + 10;// a[] 去重,离散化之后的数组int[] a = new int[N];// p[] a的前缀和int[] p = new int[N];// adds 记录添加操作List<Pairs> adds = new ArrayList<>();// querys 记录查询操作List<Pairs> querys = new ArrayList<>();// alls 记录所有在数轴上出现过的坐标List<Integer> alls = new ArrayList<>();for(int i = 0; i < n; i++){s = in.readLine().split(" ");int x = Integer.parseInt(s[0]);int c = Integer.parseInt(s[1]);adds.add(new Pairs(x, c));alls.add(x);}for(int i = 0; i < m; i++){s = in.readLine().split(" ");int l = Integer.parseInt(s[0]);int r = Integer.parseInt(s[1]);querys.add(new Pairs(l, r));alls.add(l);alls.add(r);}// 排序Collections.sort(alls);// 去重int end = unique(alls);alls = alls.subList(0, end);// 离散化,就是找到要插入或者查询的数字在 alls 的位置// 可以用二分查找// 插入// 返回值以 1 开始计数for(int i = 0; i < n; i++){int index = bsearch(adds.get(i).first, alls);a[index] += adds.get(i).second;}// 计算前缀和for(int i = 1; i < N; i++){p[i] = p[i - 1] + a[i];}// 开始查询输出for(int i = 0; i < m; i++){int l = bsearch(querys.get(i).first, alls);int r = bsearch(querys.get(i).second, alls);int res = p[r] - p[l - 1];out.write(res + "\n");out.flush();}}public static int unique(List<Integer> list){int j = 0;for(int i = 0; i < list.size(); i++){if(i == 0 || list.get(i) != list.get(i - 1)){list.set(j, list.get(i));j++;}}return j;}public static int bsearch(int x, List<Integer> list){int l = 0, r = list.size() - 1;while(l <= r){int mid = l + r >> 1;if(list.get(mid) > x) r = mid - 1;else if(list.get(mid) < x) l = mid + 1;else return mid + 1;}return 1;}
}

yxc

  1. 离散化

在这里插入图片描述

http://www.lryc.cn/news/336214.html

相关文章:

  • Python零基础从小白打怪升级中~~~~~~~文件和文件夹的操作 (1)
  • Qt plugin 开发UI界面插件
  • Android查看SO库的依赖
  • 麒麟KOS删除鼠标右键新建菜单里不需要的选项
  • DPDK系列之四十二DPDK应用网络编程UDP编程
  • 金三银四面试题(十九):MySQL中的锁
  • 【JavaScript】原型链/作用域/this指针/闭包
  • Python的MATLAB使用
  • 文件输入/输出流(I/O)
  • docker,schedule job和environment variables三者的含义与区别
  • 90天玩转Python—16—基础知识篇:面向对象知识详解
  • python 标准库之openpyxl的常规操作
  • 90天玩转Python—12—基础知识篇:Python自动化操作Email:发送邮件、收邮件与邮箱客户端操作全解析
  • 利用lidar_align来进行lidar和imu标定
  • 牛客NC93 设计LRU缓存结构【hard 链表,Map Java】
  • 机器学习和深度学习 -- 李宏毅(笔记与个人理解1-6)
  • 低功耗全极霍尔开关芯片 D02,磁性开关点精确,对工艺和温度变化不敏感
  • 初识--数据结构
  • 人工智能前沿成科技竞争新高地
  • 【算法刷题day23】Leetcode:669. 修剪二叉搜索树、108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树
  • 设计一个会议管理系统100问?
  • 一文搞懂BI、ERP、MES、SCM、PLM、CRM、WMS、APS、SCADA、QMS
  • 全量知识系统 程序详细设计 之 先验逻辑-实现:从“平凡”回到“平凡” (QA 百度搜索)
  • 注解(Annotation) --java学习笔记
  • uniapp 小程序获取WiFi列表
  • 数据可视化-ECharts Html项目实战(11)
  • 【MySQL数据库 | 第二十四篇】Limit语句的性能问题和调优策略
  • 【数据结构】两两交换链表 复制带随机指针的链表
  • 网络安全流量平台_优缺点分析
  • 【c语言】自定义类型:结构体详解