acwing 802. 区间和-java详细解析版

题目所属分类

离散化的知识
在这里插入图片描述

原题链接

假定有一个无限长的数轴,数轴上每个坐标上的数都是 0
现在,我们首先进行 n次操作,每次操作将某一位置 x 上的数加 c

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

  • 输入格式
    第一行包含两个整数 n和 m
    接下来 n行,每行包含两个整数 x 和 c
    再接下来 m行,每行包含两个整数 l 和 r
  • 输出格式共 m行,每行输出一个询问中所求的区间内数字和。

代码案例:在这里插入图片描述

题解

主要分为5大步:
1.读输入。将每次读入的x c 以Pair这样的形式add到adds中,将每次读入的位置x add到alls中,将每次读入的l r add到query中 同时也add到alls。
2.排序、去重。去重使用的是hashSet
3.通过遍历adds,完成对应的离散化的数组映射到的a数组 进而形成遍历alls中的s[i] 然后遍历query查找到对应的l和r (用到find函数——返回的是输入的坐标的离散化下标)
4.初始化s数组。
5.通过遍历query,完成求区间[l,r]的和,并输出结果。
在这里插入图片描述在这里插入图片描述

关于为什么要去重?
因为所有的坐标都是公用的,将重复的去掉之后,两个list所存的操作,

List<Pair> add = new ArrayList<>();//用来存n次操作
List<Pair> query = new ArrayList<>();//用来存m次询问

映射是一 一对应,去掉重复的点之后,然后将之前存的这两个list里面的操作出来寻找,如果不是一 一对应的话,你就不能操作

自己Java版本的写法

关于去重 排序 用的是Hashset方法去重 也可以用下面的unique函数去重

import java.util.*;
public class Main {
    static int N = 300010;//一个N +两个M 所以要开30万

    static List<Integer> alls = new ArrayList<>();//存放的是离散化后的list 实际上就是用来存所有的下标x,l,r;
    static List<Pair> query = new ArrayList<>(), adds = new ArrayList<Pair>();//用来存n次操作 和 m次查询
    static int a[] = new int[N], s[] = new int[N];//a是存放的数值数组  s是前缀和的数组
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();//n次操作
        int m = scan.nextInt();//m次询问
         //输入n次操作,每次操作存入add集合中,然后将下标x存入alls集合中
        for(int i = 0 ; i < n ; i++){
            int x = scan.nextInt();
            int c = scan.nextInt();
            alls.add(x);
            adds.add(new Pair(x, c));
        }
        //输入m次询问,每次询问存入query集合中,因为l,r是求和的下标区间和,所以l,r都存入alls集合中。
         for(int i = 0 ; i < m ; i ++ ){
            int l = scan.nextInt();
            int r = scan.nextInt();
            query.add(new Pair(l,r));
            alls.add(l);
            alls.add(r);
        }
          
        // Java的离散化操作
        alls = new ArrayList<>(new HashSet<>(alls));//通过hashset去重
        Collections.sort(alls); //排序,现在alls集合中存的是x,l,r所有值
        //增强for循环 for(元素类型 变量名 : 数组或者集合) 缺点:无下标,简单。
        for(Pair item : adds){
            int index = find(item.first);//
            a[index] += item.second;//
        }

        for(int i = 1 ; i <= alls.size() ; i ++ ) s[i] = s[i-1] + a[i]; //这是前缀和公式代码

        for(Pair item : query){
            int l = find(item.first); // 
            int r = find(item.second); // 
            System.out.println(s[r] - s[l-1]); // 
        }


 
        
    }
    
    //二分查找(find函数中是使用了二分来查找x在alls中的下标+1,因为需要符合我们要用的前缀和公式,
    //要让下标不是从0输出,最低的下标是1,符合前缀和的从1开始,所以输出的值加1)
   static int find(int x) {//寻找第一个大于等于x的数 ||第一个小于等于x的数
        int l = 0, r = alls.size() - 1;
        while(l < r) {
            int mid = l + r + 1 >> 1;
            if(alls.get(mid) <= x)
                l = mid;
            else r = mid - 1;
        }
        return l + 1;
    }
}   
//这是一个Pair类,用来存操作的类
   class Pair{
        int first;
        int second;
        public Pair(int x,int c){
            this.first = x;
            this.second = c;
        }
    }

关于Pair类的写法 还是简便一点好

static class CII {
        int x1, x2;
        public CII(int a, int b) {
            x1 = a; x2 = b;
        }
    }

中规中矩的写法 符合y总的C++代码

在这里插入图片描述

  • 为什么unique 函数返回的是 return a.begin() + j;
    从数组a的开头往后数j个数,都是去重后的数
  • subList() 方法用于截取并返回动态数组中的一部分。

subList() 方法的语法为:arraylist.subList(int fromIndex, int toIndex)
和subString一样 只不过这个是动态数组中的截取 也是左闭右开区间

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();//n次操作
        int m = scan.nextInt();//m次询问
        int N = 300010;//表示需要用到的下标数量,因为一开始可能重复,所以按照最大可能开最大的数组;
        int[] a =  new int[N]; //用来存值,从一开始的值,因为要用到前缀和,所以0不操作;
        int[] s = new int[N];//用来存前缀和,从一开始进行记录a数组;
        List<Integer> alls = new ArrayList<>();//用来存所有的下标,x,l,r;
        //因为可能会重复乱序,所以需要进行去重,排序;
        List<Pair> add = new ArrayList<>();//用来存n次操作
        List<Pair> query = new ArrayList<>();//用来存m次询问
        //输入n次操作,每次操作存入add集合中,然后将下标x存入alls集合中
        for(int i = 0 ; i < n ; i ++ ){
            int x = scan.nextInt();
            int c = scan.nextInt();
            add.add(new Pair(x,c));
            alls.add(x);
        }
        //输入m次询问,每次询问存入query集合中,因为l,r是求和的下标区间和,所以l,r都存入alls集合中。
        for(int i = 0 ; i < m ; i ++ ){
            int l = scan.nextInt();
            int r = scan.nextInt();
            query.add(new Pair(l,r));
            alls.add(l);
            alls.add(r);
        }

        Collections.sort(alls);   //排序,现在alls集合中存的是x,l,r所有值
        int unique = unique(alls);  //这一步是去重,因为l,x,r中可能有重复的数;
        alls = alls.subList(0,unique);  //将去重之后的alls的长度范围中的值重新赋值给alls集合中。

        //增强for循环 for(元素类型 变量名 : 数组或者集合) 缺点:无下标,简单。
        for(Pair item : add){
            int index = find(item.first,alls);//
            a[index] += item.second;//
        }

        for(int i = 1 ; i <= alls.size() ; i ++ ) s[i] = s[i-1] + a[i]; //这是前缀和公式代码

        for(Pair item : query){
            int l = find(item.first,alls); // 
            int r = find(item.second,alls); // 
            System.out.println(s[r] - s[l-1]); // 
        }

    }
    //去重(只要符合是第一个数或者后面一个数不等于前面一个数就是不重复的数)
    public static int unique(List<Integer> list){
        int j = 0;
        for(int i = 0 ; i <= list.size() - 1; i ++ ){
            if(i == 0 || list.get(i) != list.get(i-1)){
                list.set(j,list.get(i)); //将不重复之后的数一个一个重新存入list中。
                j ++ ;
            }
        }
        return j;
    }
 //二分查找(在集合中查找你现在的下标是在什么位置,因为需要符合我们要用的前缀和公式,要让下标不是从0输出,最低的下标是1,符合前缀和的从1开始,所以输出的值加1)
    public static int find(int x ,List<Integer> list){
        int l  = 0;
        int r = list.size() - 1;
        while(l < r){
            int mid = ( l + r )/ 2;
            if(list.get(mid) >= x) r = mid;
            else l = mid + 1;
        }
        return l + 1;
    }
}   
//这是一个Pair类,用来存操作的类
   class Pair{
        int first;
        int second;
        public Pair(int x,int c){
            this.first = x;
            this.second = c;
        }
    }


 

总结

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依嘫_吃代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值