ZOJ 1610 Count the Colors (线段树:区间染色set,整体查询)
2018-03-09 09:58
609 查看
题意: 在一条长度为8000的线段上染色,每次把区间[a,b]染成c颜色。后面染的会覆盖前面染的。求染完之后,每个颜色在线段上有多少个间断的区间。 题解: 线段树:区间set,整体查询。区间染色问题,这里所求的是每种颜色在线段上出现的次数(有多少个间断的区间)。 注意: 一般染色问题直接对应区间set都需要进行处理,因为染色是连续的,而线段树的操作是离散的,所以有时候两
4000
个区间的边界相邻就会出现一些问题。 本题是让求最后每种颜色出现的次数。[1,2],[3,4]分别涂上颜色1,这个时候我们在query的时候,无法判断到[2,3]这块区域是没有涂颜色的,我们只知道1,2,3,4,这四个点都是涂上了颜色1,所以会判断成[1,4]涂上了颜色1,而实际上是两个间断的区间。所以我们需要对区间进行一些处理。 简单的直接把区间左开右闭就可以了。每个区间左边都减少了1个长度,相对关系不发生改变(仔细想想~)。
本题并没有给说n,所以说建树的时候按maxn建立即可。
4000
个区间的边界相邻就会出现一些问题。 本题是让求最后每种颜色出现的次数。[1,2],[3,4]分别涂上颜色1,这个时候我们在query的时候,无法判断到[2,3]这块区域是没有涂颜色的,我们只知道1,2,3,4,这四个点都是涂上了颜色1,所以会判断成[1,4]涂上了颜色1,而实际上是两个间断的区间。所以我们需要对区间进行一些处理。 简单的直接把区间左开右闭就可以了。每个区间左边都减少了1个长度,相对关系不发生改变(仔细想想~)。
本题并没有给说n,所以说建树的时候按maxn建立即可。
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> #include<string> #include<cstring> #include<vector> #include<functional> #include<utility> #include<set> #include<map> #include<cmath> #include<cctype> #define INF 0x3f3f3f3f using namespace std; const int maxn=8010; #define lson 2*i,l,m #define rson 2*i+1,m+1,r int n,col[maxn<<2],vis[maxn],ans[maxn]; void pushdown(int i) { if(col[i]!=-1) { col[2*i]=col[2*i+1]=col[i]; col[i]=-1; } } void update(int ql,int qr,int v,int i,int l,int r) { if(ql<=l&&qr>=r) { col[i]=v; return; } pushdown(i); int m=(l+r)/2; if(ql<=m)update(ql,qr,v,lson); if(qr>m)update(ql,qr,v,rson); } void query(int i,int l,int r) { if(col[i]!=-1) { for(int k=l;k<=r;k++) { vis[k]=col[i]; } return; } if(l==r)return; int m=(l+r)/2; query(lson); query(rson); } int main() { while(cin>>n) { memset(col,-1,sizeof(col)); int a,b,c; for(int i=0;i<n;i++) { scanf("%d%d%d",&a,&b,&c); update(a+1,b,c,1,1,8000); } memset(vis,-1,sizeof(vis)); query(1,1,8000); memset(ans,0,sizeof(ans)); int i=1; while(i<maxn) { int color=vis[i]; if(color==-1){i++;continue;} int j=i+1; while(vis[j]!=-1&&vis[j]==color&&j<maxn)j++; ans[color]++; i=j; } for(int i=0;i<maxn;i++) { if(ans[i]) printf("%d %d\n",i,ans[i]); } puts(""); } }
相关文章推荐
- ZOJ - 1610 Count the Colors (线段树区间染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors 线段树区间染色问题
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- 【线段树区间更新 && 染色】ZOJ - 1610 Count the Colors
- ZOJ 1610 Count the Colors(线段树区间染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors 线段树区间染色
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- ZOJ 1610 Count the Colors (线段树区间染色)