题目链接:http://poj.org/problem?id=2528
题意:每次覆盖一段区间,问你最后还能看到多少区间
思路:总长度很大,1e7,线段树开不来这么大,所以就离散一下,n不大,一共也就20000个点,最后一样的成段更新
代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int t,n;
int l[maxn],r[maxn];
int x[maxn];
int Hash[10000000+5];
int co[maxn<<2];
int lazy[maxn<<2];
void push_up(int rt){
if (co[rt<<1]==co[rt<<1|1]&&co[rt<<1]!=-1) co[rt]=co[rt<<1];
else co[rt]=-1;
}
void push_down(int rt){
if (lazy[rt]!=-1){
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
co[rt<<1]=lazy[rt];
co[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
}
void build(int l,int r,int rt){
lazy[rt]=-1;
if (l==r){
co[rt]=-1;
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void update(int L,int R,int to,int l,int r,int rt){
if (L<=l&&R>=r){
co[rt]=to;
lazy[rt]=to;
return;
}
push_down(rt);
int m=(l+r)>>1;
if (L<=m) update(L,R,to,lson);
if (R>m) update(L,R,to,rson);
push_up(rt);
}
int ans[maxn];
void query(int l,int r,int rt){
if (l==r) {
if (co[rt]==-1) return;
ans[co[rt]]++;
return;
}
push_down(rt);
if (co[rt]!=-1){
ans[co[rt]]++;
return;
}
int m=(l+r)>>1;
query(lson);
query(rson);
}
int main () {
scanf ("%d",&t);
while (t--){
scanf ("%d",&n);
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(x,0,sizeof(x));
memset(Hash,0,sizeof(Hash));
memset(ans,0,sizeof(ans));
int cnt=0;
for (int i=0;i<n;i++){
scanf ("%d%d",&l[i],&r[i]);
x[cnt++]=l[i];
x[cnt++]=r[i];
}
sort(x,x+cnt);
int num=unique(x,x+cnt)-x;
int key=1;
for (int i=0;i<num;i++){
Hash[x[i]]=key;
if (i<num-1){
if (x[i+1]-x[i]==1) key++;
else key+=2;
}
}
build(1,key,1);
for (int i=0;i<n;i++){
update(Hash[l[i]],Hash[r[i]],i+1,1,key,1);
}
query(1,key,1);
int Ans=0;
for (int i=1;i<=n;i++){
if (ans[i]) Ans++;
}
printf ("%d\n",Ans);
}
return 0;
}