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操作往下跟新
- 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 1610Count the Colors
- Count the Colors(zoj 1610)
- Spring Core---------IoC控制反转
- Collection和Collections的区别
- 图论-搜索,吝啬的国度
- 团队管理和团队文化
- 黑马程序员学习日记----多线程(一)
- ZOJ 1610 Count the Colors
- [团队管理]从《亮剑》看团队建设
- 8088 汇编速查手册
- Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)
- Contacts拨号盘源码学习
- SPOJ Problem Set (classical) 11582. A Famous Grid
- win7 打开80端口
- 为XCode 4加速
- 《Windows核心编程》学习——内核对象