ZOJ 1610 Count the Colors

来源:互联网 发布:license.lic 破解软件 编辑:程序博客网 时间:2024/04/29 07:39
//线段树涂色问题: 对每一段区间进行染色 统计出现的颜色和该颜色的长度//这里建图时 以线段长度1为单位节点#include <iostream>#include <cstdio>using namespace std;const int N = 20001;int temp;struct Node{int L;int R;int kind;};Node segtree[N];int color[8001];void create_tree( int n, int l, int r ){                    segtree[n].L = l;segtree[n].R = r;segtree[n].kind = -1;if( l + 1 == r ) return;int mid = ( l + r ) >> 1;create_tree( n * 2, l, mid );create_tree( n * 2 + 1, mid, r );}void insert_tree( int n, int l, int r, int c) {             if( l == r ) return;if( segtree[n].kind == c ) return;//颜色相同,返回 if( l == segtree[n].L && segtree[n].R == r ) {                segtree[n].kind = c;return;}if( segtree[n].kind >= 0 ) //存在颜色,做更新{                        segtree[ n * 2 ].kind = segtree[n].kind;segtree[ n * 2 + 1 ].kind = segtree[n].kind;segtree[n].kind = -2;       //传值下去,完成该整段区间更新后赋值-2}int mid = ( segtree[n].L + segtree[n].R ) >> 1;if( r <= mid ) {insert_tree( n * 2, l, r, c );}else if( l >= mid ) {insert_tree( n * 2 + 1, l, r, c );}else {insert_tree( n * 2, l, mid, c);insert_tree( n * 2 + 1, mid, r, c );}}void count( int n ) //先序遍历{                                      if( segtree[n].kind != -1 && segtree[n].kind != -2 )  //颜色不为-1和-2 {          if( segtree[n].kind != temp ) {color[ segtree[n].kind ]++;temp = segtree[n].kind;}return;}if( segtree[n].L + 1 != segtree[n].R ) //非子节点{                 count( n * 2 );count( n * 2 + 1 );}else temp = -1;}int main() {int i, t;while( scanf("%d", &t) != EOF ) {create_tree( 1, 0, 8000 );for(i=0;i<t;i++){int a,b,c;scanf("%d%d%d", &a, &b, &c);insert_tree(1, a, b, c);}temp = -1;fill(color,color+8001,0);count(1);for( i = 0; i <= 8000; i++ ) {if( color[i] ) printf("%d %d\n", i, color[i]);}printf("\n");}return 0;}

//线段树指针形式 //注释的代码 WA 未解#include<iostream>#include<cstdio>using namespace std;const int MaxN=8002;struct Cnode{    int L,R;          Cnode *pLeft,*pRight;     int col; };Cnode Tree[MaxN*2];    int Ans[MaxN];int Col[MaxN];int nCount=0; int m;int temp;void BuildTree(Cnode *pRoot,int L,int R) //Notice ---> 每个节点代表一个区间{    pRoot->L=L;    pRoot->R=R;    pRoot->col=-1;    if(L+1==R) {pRoot->pLeft=NULL;pRoot->pRight=NULL;return;}nCount++;pRoot->pLeft = Tree + nCount;nCount++;pRoot->pRight = Tree + nCount;BuildTree( pRoot->pLeft, L, ( L + R )/2);BuildTree( pRoot->pRight, (L + R) / 2, R);}int Mid( Cnode * pRoot){    return (pRoot->L + pRoot->R)/2;}void update(Cnode *pRoot,int a,int b,int c){if(pRoot->col == c) return; //同色不处理    if(pRoot->L==a && pRoot->R==b) //完全覆盖才涂色     {        pRoot->col=c;        return ;    }    if(pRoot->col>=0)    {        pRoot->pLeft->col = pRoot->pRight->col = pRoot->col;        pRoot->col=-2;    }    if(b<=Mid(pRoot)) update(pRoot->pLeft,a,b,c);    else if(a>= (Mid(pRoot)) ) update(pRoot->pRight,a,b,c);    else     {        update(pRoot->pLeft, a, Mid(pRoot), c);        update(pRoot->pRight, Mid(pRoot), b, c);    }}/*void Query(Cnode *pRoot,int a,int b){if(pRoot == NULL) return;    if(pRoot->col!=-1 && pRoot->col!=-2)    {        for(int i=pRoot->L; i<=pRoot->R; i++) {Col[i]=pRoot->col;}return;    }    if(b<= Mid(pRoot) )         Query(pRoot->pLeft,a,b);    else if(a >=(Mid(pRoot)) )         Query(pRoot->pRight,a,b);    else     {Query(pRoot->pLeft, a, Mid(pRoot));Query(pRoot->pRight, Mid(pRoot), b);    }}*/void count( Cnode *pRoot ) //先序遍历{  if( pRoot->col != -1 && pRoot->col != -2 )  //颜色不为-1和-2 {          if( pRoot->col != temp ) {Ans[ pRoot->col ]++;temp = pRoot->col;}return;}if( pRoot->L + 1 != pRoot->R ) //非子节点{                 count( pRoot->pLeft );count( pRoot->pRight );}else temp = -1;}void run(){    int i;    nCount=0;    BuildTree(Tree,0,MaxN);fill(Ans,Ans+MaxN,0);fill(Col,Col+MaxN,-1);    for(i=0;i<m;i++)     {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        update(Tree,a,b,c);    }    /*Query(Tree,0,MaxN);int init=-1;for(i=0;i<MaxN;i++) if(Col[i]!=-1){if(Col[i]!=init){Ans[Col[i]]++;init=Col[i];}}*/temp = -1;count(Tree);for(i=0;i<MaxN;i++) if(Ans[i]){printf("%d %d\n",i,Ans[i]);}printf("\n");}int main(){while(scanf("%d",&m)!=EOF) run();    return 0;}


Example

4

0 4 4
0 3 1
3 4 2
0 2 2
0 2 3

-1---->未赋值

-2---->该节点已经结束lazy操作往下跟新



原创粉丝点击