正题
题意
一条长m线,有n条长度不同的线段,求该线被分割成多少段
解题思路
标记颜色,然后统计是用一个color表示上次搜到的(因为线段树的查找顺序是从左到右的)去重
代码
#include<cstdio>
#include<cstring>
using namespace std;
struct xjq{int l,r,cover;
}tree[400001];
int n,ll,rr,w,s,cl,color,m;
void build(int x,int a,int b)//建树
{tree[x].l=a;tree[x].r=b;if (b-a==1) return;else{int m=(a+b)/2;build(x*2,a,m);build(x*2+1,m,b);}
}
void inster(int x,int a,int b,int c)//插入
{if (tree[x].cover==c) return;if (tree[x].l==a && tree[x].r==b)//标记{tree[x].cover=c;return;}if (tree[x].cover>=0)//下传标记{tree[x*2].cover=tree[x].cover;tree[x*2+1].cover=tree[x].cover;tree[x].cover=-1;}int m=tree[x*2].r;if (b<=m) inster(x*2,a,b,c);else if (a>=m) inster(x*2+1,a,b,c);else{inster(x*2,a,m,c);inster(x*2+1,m,b,c);}return;
}
void find(int x)
{if (tree[x].cover>=0){if (tree[x].cover!=color){s++;color=tree[x].cover;//标记去重}return;}if (tree[x].r-tree[x].l==1) return;else{find(x*2);find(x*2+1);}
}
int main()
{scanf("%d%d",&n,&m);build(1,1,m);for (int i=1;i<=n;i++){scanf("%d%d%d",&ll,&rr,&cl);inster(1,ll,rr,cl);}s=0;color=-2;find(1);printf("%d",s);
}