F. Feed Cats
题目大意
- 给一长度为的数轴,个区间
- 在数轴上选取一些点作为特殊点
- 在满足个区间中,每个区间内只能有一个特殊点
- 问最多能选多少个特殊点
解题思路
- 对于每个点有放或不放两种状态
- 考虑表示位置可能放或不放的最优结果
- 若不放,
- 若放,则
- 两者取
- 表示有多少个区间含,用树状数组统计
- ,在包含的区间中最靠左的端点为 ,则不影响位置的选取,可以由转移过来
- 最靠左的端点为 ,预处理出来,按区间左端点由小到大填数轴
- 已经被填过的位置上值一定是比当前值小的,不用更新
package Tx;
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;public class Tx1{static long md=(long)998244353;static long Linf=Long.MAX_VALUE/2;static int inf=Integer.MAX_VALUE/2;staticclass Node{int l;int r;public Node(int L,int R) {l=L;r=R;}}staticclass BIT{int size;int[] tr;public BIT(int n) {size=n;tr=new int[n+5];}int lowbit(int x) {return x&-x;}void update(int x,int k) {for(int i=x;i<=size;i+=lowbit(i)) {tr[i]+=k;}}int query(int x) {int res=0;for(int i=x;i>0;i-=lowbit(i)) {res+=tr[i];}return res;}}public static void main(String[] args) throws IOException{AReader input=new AReader();PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));int T=input.nextInt();while(T>0){int n=input.nextInt();int m=input.nextInt();Node[] a=new Node[m+1];BIT Tp=new BIT(n);for(int i=1;i<=m;++i) {int x=input.nextInt();int y=input.nextInt();a[i]=new Node(x, y);//初始所有区间Tp.update(x, 1);Tp.update(y+1, -1);}Arrays.sort(a,1,m+1,(o1,o2)->{if(o1.l==o2.l)return o2.r-o1.r;else return o1.l-o2.l;});Node[] b=new Node[n+1];//只保留左端点能包含的最长区间int cnt=0;a[0]=new Node(0, 0);for(int i=1;i<=m;++i) {if(a[i].l==a[i-1].l) continue;else {cnt++;b[cnt]=new Node(a[i].l, a[i].r);}}int[] c=new int[n+1];//数轴上每一位能到的最左端点int lhs=-1;//维护已经处理过的区间,处理过的位置不会再被更新int rhs=-1;for(int i=1;i<=cnt;++i) {if(lhs==-1&&rhs==-1) {lhs=b[i].l;rhs=b[i].r;for(int j=lhs;j<=rhs;++j)c[j]=b[i].l;}else {if(b[i].l<lhs) {for(int j=b[i].l;j<lhs;++j) {c[j]=b[i].l;}lhs=b[i].l;}if(b[i].r>rhs) {for(int j=rhs+1;j<=b[i].r;++j) {c[j]=b[i].l;}rhs=b[i].r;}}}long[] f=new long[n+1];f[1]=Tp.query(1);for(int i=2;i<=n;++i) {f[i]=f[i-1];int t=Tp.query(i);if(t==0)continue; f[i]=Math.max(f[i], f[c[i]-1]+t);}out.println(f[n]);T--;}out.flush();out.close();}//System.out.println();//out.println();staticclass AReader{BufferedReader bf;StringTokenizer st;BufferedWriter bw;public AReader(){bf=new BufferedReader(new InputStreamReader(System.in));st=new StringTokenizer("");bw=new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException{return bf.readLine();}public String next() throws IOException{while(!st.hasMoreTokens()){st=new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException{//确定下一个token只有一个字符的时候再用return next().charAt(0);}public int nextInt() throws IOException{return Integer.parseInt(next());}public long nextLong() throws IOException{return Long.parseLong(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public float nextFloat() throws IOException{return Float.parseFloat(next());}public byte nextByte() throws IOException{return Byte.parseByte(next());}public short nextShort() throws IOException{return Short.parseShort(next());}public BigInteger nextBigInteger() throws IOException{return new BigInteger(next());}public void println() throws IOException {bw.newLine();}public void println(int[] arr) throws IOException{for (int value : arr) {bw.write(value + " ");}println();}public void println(int l, int r, int[] arr) throws IOException{for (int i = l; i <= r; i ++) {bw.write(arr[i] + " ");}println();}public void println(int a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{bw.write(a);bw.newLine();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}}
}