搜狗后端笔试第二场

第二题,题目是先给一组长度为N的全0数组,然后输入M行,每行两个数字L,R,将LR区间内(包含LR)的所有数修改为i(1<=i<=M),最后将这数组中数字按照0*a[0]+1*a[1]+……N-1*a[N-1]求和,再与100000009做模运算,得出结果,有没有大佬帮忙看看为啥我这代码提交0ac。。。。
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        int i = 1;
        int result = 0;
        List<Integer[]> lr = new ArrayList<>(M);
        Map<Integer,Integer> finished = new HashMap<>(M);
        while(i<=M){
            Integer[] tmp = new Integer[2];
            tmp[0] = in.nextInt();
            tmp[1] = in.nextInt();
            lr.add(tmp);
            i++;
        }
        //最后一轮修改的值逆着往回倒,已修改过的即为最终值,加入map中,之后不再修改
        for(i = M-1;i>=0;i--){
            int L = lr.get(i)[0];
            int R = lr.get(i)[1];
            for(int j=L;j<=R;j++){
                if(!finished.containsKey(j)){
                    result += (i+1)*j;
                    finished.put(j,0);
                }
            }
        }
        result = result % 100000009;
        System.out.println(result);
    }
}


#搜狗##笔试题目#
全部评论
result改成long,能过30
点赞
送花
回复
分享
发布于 2019-09-16 21:04

相关推荐

点赞 4 评论
分享
牛客网
牛客企业服务