解法:若区间覆盖的最小值大于等于2,则有他没他没影响,反之则不能算。所以一开始差分数组预处理区间加,然后ST表查最小值即可。
package com.sky;
import java.util.Scanner;
import java.util.Arrays;
public class Test1 {
static final int maxn = 200005;
static int n, m;
static int[][] st = new int[maxn][30];
static int[] d = new int[maxn];
static int[] a = new int[maxn], b = new int[maxn];
public static int query(int l, int r) {
int k = (int)(Math.log(r - l + 1) / Math.log(2));
return Math.min(st[l][k], st[r - (1 << k) + 1][k]);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= m; ++i) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
d[a[i]]++;
d[b[i] + 1]--;
}
for (int i = 1; i <= n; ++i) {
d[i] += d[i - 1];
st[i][0] = d[i];
}
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
st[i][j] = Math.min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
int res = 0;
for (int i = 1; i <= m; ++i) {
if (query(a[i], b[i]) >= 2) res++;
}
System.out.println(res);
sc.close();
}
}