Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

那个大牛可以讲下关于线段树的上传和下传标记啊,我可能就是WA在这个地方。

Posted by yogafrank at 2008-09-06 19:39:23 on Problem 2777
代码的可读性应该还是不错的,另外问下这题有必要离散化数据么,没有必要吧。

#include <iostream>
using namespace std;

const int L = 100001;
const int T = 31;

bool flag[T];

typedef struct node
{
	int start, end;
	int color;
}TreeNode;

class SegmentTree
{
private:
	TreeNode nodes[L * 3];
public:
	void buildtree ( int, int, int );
	void update ( int, int, int, int );
	void query ( int, int, int );
};

void SegmentTree::buildtree ( int start, int end, int p )
{
	nodes[p].start = start;
	nodes[p].end = end;
	nodes[p].color = 0;

	if ( start == end )
		return;

	int mid = ( start + end ) >> 1;
	buildtree ( start, mid, p * 2 );
	buildtree ( mid + 1, end, p * 2 + 1 );
}

void SegmentTree::update ( int start, int end, int p, int color )
{
	if ( nodes[p].start >= start && nodes[p].end <= end )
	{
		nodes[p].color = color;
		return;
	}

	if ( nodes[p].color > 0 && nodes[p].color != color  )
	{
		nodes[p * 2].color = nodes[p * 2 + 1].color = nodes[p].color;
		nodes[p].color = -1;
	}

	int mid = ( nodes[p].start + nodes[p].end ) >> 1;

	if ( start <= mid )
		update ( start, end, p * 2, color );
	if ( mid < end )
		update ( start, end, p * 2 + 1, color );

	if ( nodes[p * 2].color == nodes[p * 2 + 1].color )
		nodes[p].color = nodes[p * 2].color;
}

void SegmentTree::query ( int start, int end, int p )
{
	if ( start <= nodes[p].start && nodes[p].end <= end && nodes[p].color != -1 )
	{
		flag[nodes[p].color] = true;
		return;
	}

	int mid = ( nodes[p].start + nodes[p].end ) >> 1;

	if ( start <= mid )
		query ( start, mid, p * 2 );
	if ( mid < end )
		query ( start, end, p * 2 + 1 );
}

void swap ( int &a, int &b )
{
	if ( a > b )
	{
		int t = a;
		a = b;
		b = t;
	}
}

SegmentTree tree;

int main()
{
	int l, t, o, i, s, e, color;
	char c;
    freopen ("2777.txt", "r", stdin );
	scanf ( "%d%d%d", &l, &t, &o );
	tree.buildtree ( 1, l, 1 );
	tree.update ( 1, l, 1, 1 );
	getchar ();
	for ( i = 0; i < o; i++ )
	{
		scanf ( "%c", &c );
		if ( c == 'C' )
		{
			scanf ( "%d%d%d", &s, &e, &color );
			swap ( s, e );
			tree.update ( s, e, 1, color );
		}
		else
		{
			scanf ( "%d%d", &s, &e );
			memset ( flag, false, sizeof ( flag ) );
			swap ( s, e );
			tree.query ( s, e, 1 );

			int sum = 0;
			for ( int i = 1; i < T; i++ )
				if ( flag[i] )
					sum++;
			printf ( "%d\n", sum );
		}
		getchar ();
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator