zoj --- Count the Colors
来源:互联网 发布:申万宏源证券软件下载 编辑:程序博客网 时间:2024/06/05 19:20
Count the Colors
给一个n,接下来n组数,a,b,c表示a到b区间涂色为c
求最后每种颜色有多少个区间,如果没有,该颜色不用输出
这题中不像以前的某题,只看该区间有没有被遮挡
这题中如果大区间被小区间遮挡了一部分,那么大区间可能被分为两个区间·····
计算有该颜色有多少个区间才是难点,而且如果输入
1 2 1
2 3 1
那么颜色为1的区间只有一个,因为两个区间重合了,变为一个区间了
解题思路:
用线段树记录每个区间的颜色情况
在建树的时候加入一个col表示颜色,先初始化为-1,表示没有颜色(如果该区域有多个颜色,那么也表示为-1)
在加入新的区间时候,线段树递归中发现区间涂过色,那么把区间col变为-1,而把col的值传给子区间(两个子区间)
这样可以观察是这个颜色是否还留的有
col的储存是关键
然后再开一个函数,暴力一下所有的线段树区间,把颜色都记录到新开的col数组里
然后再进行判断,每种颜色有多少个区间
#include <cmath>#include <cstdio>#include <cstdlib>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <stack>#include <climits>#define L(x) (x << 1)#define R(x) (x << 1 | 1)using namespace std;const int MAX = 8010;struct Tnode{ int col; int l,r;};Tnode node[MAX*3];int col[MAX],len[MAX];;void init()//初始化{ memset(node,0,sizeof(node)); memset(col,-1,sizeof(col));}void Build(int t,int l,int r)//建立线段树{ node[t].l = l; node[t].r = r; node[t].col = -1; if( l == r - 1 ) return ; int mid = ( l + r ) >> 1; Build(L(t), l, mid); Build(R(t), mid, r);}void Updata(int t,int l,int r,int col)//更新区间颜色{ if( node[t].l == l && node[t].r == r )//如果区间重合,记录颜色 { node[t].col = col; return ; } if( node[t].col == col ) //如果该区间已经有该颜色了,那么返回 return ; if( node[t].col >= 0 )//如果区间有不同的颜色了,那么涂成彩色,标记为-1,子区间颜色标记为父区间颜色 { node[R(t)].col = node[t].col; node[L(t)].col = node[t].col; node[t].col = -1; } int mid = (node[t].l + node[t].r) >> 1;//正常递归,线段树正常套路 if( l >= mid ) Updata(R(t),l,r,col); else if( r <= mid ) Updata(L(t),l,r,col); else { Updata(L(t),l,mid,col); Updata(R(t),mid,r,col); }}void Compute(int t,int l,int r){ if( node[t].col >= 0 ) //如果区间有颜色,且不为彩色 { for(int i=l; i<r; i++)//把该区间颜色统计一下 col[i] = node[t].col; return ; } if( node[t].l == node[t].r - 1 ) return ;// 如果父亲节点已经是这种颜色了,就没必要再染色了 int mid = (node[t].l + node[t].r) >> 1; if( l >= mid ) Compute(R(t),l,r); else if( r <= mid ) Compute(L(t),l,r); else { Compute(L(t),l,mid); Compute(R(t),mid,r); }}int main(){ int n,x,y,color; while( ~scanf("%d",&n) ) { init(); Build(1,0,8000); while( n-- ) { scanf("%d%d%d",&x,&y,&color); Updata(1,x,y,color); } Compute(1,0,8000);//将颜色分布情况反映到col里 memset(len,0,sizeof(len)); for(int i=0; i<MAX; i++) if( col[i+1] != col[i] && col[i] != -1 )//数一数区间颜色不为1的区间 len[col[i]]++; for(int i=0; i<MAX; i++) if( len[i] ) printf("%d %d\n",i,len[i]); printf("\n"); } return 0;}
阅读全文
0 0
- Count the Colors ZOJ
- Count the Colors ZOJ
- Count the Colors ZOJ
- Count the Colors ZOJ
- zoj --- 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
- python基础--数学函数
- Android 高级自定义Toast及源码解析
- 连接池&DBUtils
- SQL Server与MySQL的选择
- marathon
- zoj --- Count the Colors
- SEO(搜索引擎优化)企业网站容易犯的十大通病
- hdu6121 Build a tree(好题)
- PHP代码优化-提高项目效率
- 用composer搭建php框架(五)--MVC之模型
- 斜杠与反斜杠总结
- python基础--随机函数
- 学习java的方向是什么?必须了解
- [原创]irrlicht 3d游戏武器粒子特效的实现2