Count the Colors (zoj 1610 线段树 区间颜色覆盖)

Count the ColorsTime Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.

InputThe first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:x1 x2 cx1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.

OutputEach line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can’t be seen, you shouldn’t print it.

Print a blank line after every dataset.

Sample Input50 4 40 3 13 4 20 2 20 2 340 1 13 4 11 3 21 3 160 1 01 2 12 3 11 2 02 3 01 2 1

Sample Output1 12 13 1

1 1

0 21 1

Author: StandloveSource: ZOJ Monthly, May 2003Submit Status

题意:在一条线段上画颜色,画n次,,每次使x1到x2区间颜色变为c。求表面上能看到的颜色种类和该颜色的段数。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 8005#define MAXN 40005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FRL(i,a,b) for(i = a; i < b; i++)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n)scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pfprintf#define DBGpf("Hi\n")typedef long long ll;using namespace std;struct tree{int l,r,color;}tree[maxn<<2],TREE[maxn<<2];int n,num;int ans[maxn];void pushdown(int rt){if (tree[rt].color>-1){tree[rt<<1].color=tree[rt].color;tree[rt<<1|1].color=tree[rt].color;tree[rt].color=-1;}}void build(int rt,int l,int r){tree[rt].l=l;tree[rt].r=r;tree[rt].color=-1;if (l==r)return ;int mid=(tree[rt].l+tree[rt].r)>>1;build(lson);build(rson);}void update(int rt,int l,int r,int color){if (l<=tree[rt].l&&tree[rt].r<=r){tree[rt].color=color;return ;}pushdown(rt);int mid=(tree[rt].l+tree[rt].r)>>1;if(mid>=r) update(rt<<1,l,r,color);else if (mid<l) update(rt<<1|1,l,r,color);else{update(lson,color);update(rson,color);}}void Count(int rt,int l,int r){if (l==tree[rt].l&&tree[rt].r==r&&tree[rt].color>-1){TREE[num].l=l;TREE[num].r=r;TREE[num++].color=tree[rt].color;return ;}if (l==r) return ;int mid=(tree[rt].l+tree[rt].r)>>1;if (mid>=r) Count(rt<<1,l,r);else if(mid<l) Count(rt<<1|1,l,r);else{Count(lson);Count(rson);}}int main(){int i,j;int l,r,color;while (~sf(n)){num=0;build(1,1,maxn); //注意是maxn,不是n!!!FRL(i,0,n){sfff(l,r,color);update(1,l+1,r,color);}Count(1,1,maxn); //注意是maxn,不是n!!!mem(ans,0);ans[TREE[0].color]++;FRL(i,1,num){//前后两个线段颜色不一样或者不相邻if (TREE[i].color!=TREE[i-1].color||TREE[i].l-1>TREE[i-1].r)ans[TREE[i].color]++;}FRL(i,0,maxn){if (ans[i])pf("%d %d\n",i,ans[i]);}pf("\n");}return 0;}/*50 4 40 3 13 4 20 2 20 2 340 1 13 4 11 3 21 3 160 1 01 2 12 3 11 2 02 3 01 2 1*/

抱最大的希望,为最大的努力,做最坏的打算

Count the Colors (zoj 1610 线段树 区间颜色覆盖)

相关文章:

你感兴趣的文章:

标签云: