acm数据结构题解

2019-03-22 13:18

ZOJ1610

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.

Input

The 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 c

x1 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.

Output

Each 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 Input 5 0 4 4 0 3 1 3 4 2 0 2 2 0 2 3 4

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

Sample Output 1 1 2 1 3 1 1 1 0 2 1 1

#include #include #include #include #include #include #include #include #include #include #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 ) {

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

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); } }


acm数据结构题解.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:评标办法采用综合评分法,满分100分。

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: