主要利用优先队列实现 //ZOJ 4120 Tokens on the Segments #include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=100010; struct node { int st,en; }s; bool operator <(node a,node b) { if(a.st!=b.st) return a.st>b.st; return a.en>b.en; } priority_queue<node>q; int main() { int t; scanf("%d",&t); while(t--) { int n,se,en; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&s.st,&s.en); q.push(s); } int mmax=0; int ans=0; while(!q.empty()) { node now=q.top(); q.pop(); if(now.st<=mmax&&now.st+1<=now.en) { now.st++; q.push(now); } else if(now.st>mmax) { ans ++; mmax=now.st; } } printf("%d\n",ans); } return 0; }