也是区间更新,但这道题数据范围太大,要离散化
离散化即把涉及到的数字都记录下来,重新排过序后再用记录下来的下标作为其值,当使用其值进行比较时就可以根据下标找到对应的值
这里用了set判重,set是用insert插入的,而queue和stack都是用push插入值
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
#define N 10005
struct Node{
int l,r,flag,lazy;
}t[N*4*2];
int n;
int x[N*2],s[N],e[N];
void build(int p,int l,int r){
t[p].l=l;
t[p].r=r;
t[p].flag=0;
if(l==r) return;
int mid=(l+r)>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
}
void pushdown(int p){
if(t[p].lazy) {
t[p*2].flag=t[p].lazy;
t[p*2+1].flag=t[p].lazy;
t[p*2].lazy=t[p].lazy;
t[p*2+1].lazy=t[p].lazy;
t[p].lazy=0;
}
}
void update(int p,int l,int r,int num){
if(x[t[p].r]<l||x[t[p].l]>r) return;
if(x[t[p].r]<=r&&l<=x[t[p].l]) {
t[p].flag=num;
t[p].lazy=num;
return;
}
pushdown(p);
update(p*2,l,r,num);
update(p*2+1,l,r,num);
}
set<int> se;
void query(int p){
if(t[p].l==t[p].r) {
se.insert(t[p].flag);
return;
}
pushdown(p);
query(p*2);
query(p*2+1);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t,tmp1,tmp2;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d%d",&s[i],&e[i]);
x[i*2-1]=s[i]; x[i*2]=e[i];
}
sort(x+1,x+1+2*n);
build(1,1,2*n);
for(int i=1;i<=n;i++) update(1,s[i],e[i],i);
se.clear();
query(1);
printf("%d\n",se.size());
}
}