
AcWing 802. 区间和
发布日期:2021-05-07 14:08:23
浏览次数:15
分类:原创文章
本文共 2815 字,大约阅读时间需要 9 分钟。
假定有一个无限长的数轴,数轴上每个坐标上的数都是0。
现在,我们首先进行 n 次操作,每次操作将某一位置x上的数加c。
接下来,进行 m 次询问,每个询问包含两个整数l和r,你需要求出在区间[l, r]之间的所有数的和。
输入格式
第一行包含两个整数n和m。
接下来 n 行,每行包含两个整数x和c。
再接下里 m 行,每行包含两个整数l和r。
输出格式
共m行,每行输出一个询问中所求的区间内数字和。
数据范围
−109≤x≤109−109≤x≤109,
1≤n,m≤1051≤n,m≤105,
−109≤l≤r≤109−109≤l≤r≤109,
−10000≤c≤10000−10000≤c≤10000
输入样例:
3 31 23 67 51 34 67 8
输出样例:
805
思想:
存储所有出现的坐标值,然后排序,离散化到新数组,将坐标对应的值存入前缀和数组,根据lr在离散化数组中找到下标,然后根据前缀和计算差值。
1、利用TreeMap将所有的(x,c)存储起来平均O(nlogn)
2、区间l,r将他们作为键值存储到TreeMap;平均O(mlogm),他们所对应的值不变,并且将区间(l,r)作为查询键值对存储到集合O(m)
3、根据map的value值集合构造前缀和数组O(n + m)
4、根据map的key键集合构造查询数组O(n + m)
5、根据集合的键值对在查询数组中查找对应的离散化下标O(mlog(n+m))
6、根据前缀和进行计算
总的时间复杂度O(nlogn) + O(nlogn) + O(m) + 2O(n + m) + O(mlog(n+m)) 抽象为 O(nlogn)
//离散化import java.io.*;import java.lang.Integer;import java.util.*;class Node{//键值对相当于c++的pair<int, int> public int first; public int second; public Node(int first, int second){ this.first = first; this.second = second; }}class Main{ static int N = 300010; static int[] sum = new int[N];//离散数组,存储前缀和 static TreeMap<Integer, Integer> map = new TreeMap<>();//存储坐标和长度 static ArrayList<Node> query = new ArrayList<>();//用于查询 相当于c++的vector<pair<int,int>> static int binserySearch(Integer[] index, int x){//二分查找 int l = 0, r = index.length - 1; while(l < r){ int mid = l + r >> 1; if(index[mid] < x)l = mid + 1; else r = mid; } return l; } public static void main(String[] args)throws Exception{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); String[] params = buf.readLine().split(" "); int n = Integer.valueOf(params[0]); int m = Integer.valueOf(params[1]); for(int i = 0; i < n; ++i){ String[] xc = buf.readLine().split(" "); int x = Integer.valueOf(xc[0]);//构造排序键值 int c = Integer.valueOf(xc[1]); map.put(x, map.getOrDefault(x, 0) + c); } for(int i = 0; i < m; ++i){ String[] lr = buf.readLine().split(" "); int l = Integer.valueOf(lr[0]); int r = Integer.valueOf(lr[1]); map.put(l, map.getOrDefault(l, 0)); map.put(r, map.getOrDefault(r, 0)); query.add(new Node(l, r));//区间读入集合 } int k = 1; for(int num : map.values()){//构造前缀和数组 sum[k] = sum[k - 1] + num; k++; } Object[] obj = map.keySet().toArray(); Integer[] index = Arrays.copyOfRange(obj, 0, obj.length, Integer[].class); for(Node node : query){ int x1 = binserySearch(index, node.first);//查找离散化坐标 int x2 = binserySearch(index, node.second); int res = sum[x2 + 1] - sum[x1];//计算差值 System.out.printf("%d\n", res); } } }
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年03月19日 02时06分40秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python学习:字符串
2019-03-04
Python学习:继承
2019-03-04
Python学习:类、类对象和实例对象
2019-03-04
数据库系统概论:ER图设计
2019-03-04
AC自动机 - Word Puzzles - POJ - 1204
2019-03-04
DIJ - 昂贵的聘礼 - POJ 1062
2019-03-04
计算几何(旁切圆) - Ex-circles - UVA 11731
2019-03-04
DP - Tickets - HDU - 1260
2019-03-04
phpStudy for Linux (lnmp+lamp一键安装包)
2019-03-04
【安卓学习笔记】JAVA基础Lesson9-对象的转型
2019-03-04
JS保留字和关键字
2019-03-04
本校暑假训练营11_Python数据分析入门7-网络1
2019-03-04
本校暑假训练营12_Python数据分析入门7-网络2
2019-03-04
网络安全学习篇50_第四阶段_SSRF
2019-03-04
数据库SQL实战3_获取所有非manager的员工emp_no
2019-03-04
LeetCode7_数组双指针_有序数组元素去重、数组移除指定元素
2019-03-04
LeetCode11_二叉树的层序遍历_BFS迭代、DFS递归、拓展BFS的使用场景
2019-03-04