ZOJ 1610 Count the Colors(线段树区间更新)

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

题意: n个操作,,每次把一段区间更新,最后统计每一种颜色的段数

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8//typedef __int64 ll;#define fre(i,a,b) for(i = a; i <b; i++)#define free(i,b,a) for(i = b; i >= a;i–)#define mem(t, v) memset ((t) , v, sizeof(t))#define ssf(n)scanf("%s", n)#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 bugpf("Hi\n")using namespace std;#define INF 0x3f3f3f3f#define N 8005struct stud{int le,ri;int va;}f[N*4];int n,k;int color[N];struct studd{int le,ri;int va;}ans[N];inline void pushdown(int pos){f[L(pos)].va=f[R(pos)].va=f[pos].va;f[pos].va=-1;}void build(int pos,int le,int ri){f[pos].le=le;f[pos].ri=ri;f[pos].va=-1;if(le==ri) return ;int mid=MID(le,ri);build(L(pos),le,mid);build(R(pos),mid+1,ri);}void update(int pos,int le,int ri,int va){if(f[pos].le==le&&f[pos].ri==ri) {f[pos].va=va;return ; }if(f[pos].va!=-1) pushdown(pos);int mid=MID(f[pos].le,f[pos].ri);if(mid>=ri)update(L(pos),le,ri,va); elseif(mid<le) update(R(pos),le,ri,va); else {update(L(pos),le,mid,va);update(R(pos),mid+1,ri,va); }if(f[L(pos)].va==f[R(pos)].va&&f[L(pos)].va!=-1)f[pos].va=f[pos].va;}void query(int pos){if(f[pos].va!=-1){ans[k].le=f[pos].le;ans[k].ri=f[pos].ri;ans[k++].va=f[pos].va;return ;}if(f[pos].le==f[pos].ri) return ;query(L(pos));query(R(pos));}int main(){int i,j;while(~sf(n)){build(1,0,N);int le,ri,va;fre(i,0,n){sfff(le,ri,va);update(1,le,ri-1,va);}k=0;query(1);mem(color,0);color[ans[0].va]++;fre(i,1,k)if(ans[i].va!=ans[i-1].va||ans[i-1].ri+1<ans[i].le)color[ans[i].va]++;fre(i,0,N) if(color[i])pf("%d %d\n",i,color[i]);pf("\n");}return 0;}

即使爬到最高的山上,一次也只能脚踏实地地迈一步。

ZOJ 1610 Count the Colors(线段树区间更新)

相关文章:

你感兴趣的文章:

标签云: