第一题
package ccf202203; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] x = new int[k ]; int[] y = new int[k ]; int count = 0; Set<Integer> set = new HashSet<>(); for (int i = 0; i < k; i++) { x[i] = sc.nextInt(); y[i] = sc.nextInt(); if (!set.contains(y[i])&&y[i]!=0) { count++; } set.add(x[i]); } System.out.println(count); } }
第二题(差分前缀和)
package ccf202203; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class p2 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] temp = bf.readLine().split(" "); int n = Integer.parseInt(temp[0]); int m = Integer.parseInt(temp[1]); int k = Integer.parseInt(temp[2]); long[] t = new long[n]; long[] c = new long[n]; long[] q = new long[m]; long[] num = new long[300005]; for (int i = 0; i < n; i++) { temp = bf.readLine().split(" "); t[i] = Integer.parseInt(temp[0]); c[i] = Integer.parseInt(temp[1]); num[(int) Math.max(1, t[i] - k - c[i] + 1)]++; num[(int) Math.max(1, t[i] - k + 1)]--; //相当于在[t[i] - k - c[i] + 1, t[i] - k]范围内加1,由于t[i] - k - c[i] + 1, t[i] - k都有可能小于0,设置左边界为1 } for (int i = 1; i < num.length; i++) { num[i] += num[i - 1]; } for (int i = 0; i < m; i++) { q[i] = Integer.parseInt(bf.readLine()); System.out.println(num[(int) q[i]]); } } }