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

离散化一下就AC了,但是线段树就死活过不了,一直WA,大家帮忙看一下代码,哪里错了啊???

Posted by chenhaifeng at 2008-08-07 20:45:42 on Problem 2528
离散化一下就AC了,但是线段树就死活过不了,一直WA,大家帮忙看一下代码,哪里错了啊???
#include <stdio.h>
#include <stdlib.h>

#define TT tree[treel]
#define TR tree[root]

struct SEG
{
	int x1, x2;
}seg[10005];
struct IN 
{
	int x;
	int i;
	int LorR;
}inx[20005];

int cmp(const void *a, const void *b)
{
	struct IN *c = (struct IN *)a;
	struct IN *d = (struct IN *)b;
	if(c->x < d->x) return -1;
	return 1;
}

struct TreeNode
{
	int tag;
	int mid;
	int l, r;
	int lson, rson;
};
TreeNode tree[5000000];
int treel, flag;

int BuildNode(int l, int r)
{
	TT.l = l;
	TT.r = r;
	TT.mid = (l + r) / 2;
	TT.lson = -1;
	TT.rson = -1;
	TT.tag = 0;
	treel++;
	return treel - 1;
}

void BuildTree(int root)
{
	if(root < 0) return;
	if(TR.l + 1 < TR.r)
	{
		TR.lson = BuildNode(TR.l, TR.mid);
		TR.rson = BuildNode(TR.mid, TR.r);
		BuildTree(TR.lson);
		BuildTree(TR.rson);
	}
}

void insert(int root, int l, int r)
{
	if(root < 0) return;
	if(l <= TR.l && r >= TR.r)
	{
		if(TR.tag == 0) flag = 1;
		TR.tag = 1;
		return ;
	}
	else 
	{
		if(TR.tag == 1) return;
		if(l >= TR.mid) insert(TR.rson, l, r);
		else if(r <= TR.mid) insert(TR.lson, l, r);
		else 
		{
			insert(TR.lson, l, TR.mid);
			insert(TR.rson, TR.mid, r);
		}
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("ou1t.txt", "w", stdout);
	int ca, n, inxl, xlen, ans;
	scanf("%d", &ca);
	while(ca--)
	{
		scanf("%d", &n);
		inxl = 0;
		for(int i = 0;i < n;i++)
		{
			scanf("%d%d", &seg[i].x1, &seg[i].x2);
			seg[i].x1 -= 1;
			inx[inxl].x = seg[i].x1;
			inx[inxl].i = i;
			inx[inxl++].LorR = 0;
			inx[inxl].x = seg[i].x2;
			inx[inxl].i = i;
			inx[inxl++].LorR = 1;
		}
		//sort(inx, inx + inxl, cmp);
		qsort(inx, inxl, sizeof(inx[0]), cmp);
		/*for(int i = 0;i < inxl;i++)
		{
			printf("%d ", inx[i].x);
		}
		printf("\n");*/
		xlen = 0;
		if(inx[0].LorR == 0) seg[inx[0].i].x1 = 0;
		else seg[inx[0].i].x2 = 0;
		for(int i = 1;i < inxl;i++)
		{
			if(inx[i].x > inx[i - 1].x) xlen++;
			if(inx[i].LorR == 0) seg[inx[i].i].x1 = xlen;
			else seg[inx[i].i].x2 = xlen;
		}
		treel = 0;
		BuildNode(0, xlen);
		BuildTree(0);
		ans = 0;
		for(int i = n - 1;i >= 0;i--)
		{
			if(tree[0].tag == 1) break;		
			flag = 0;
			insert(0, seg[i].x1, seg[i].x2);	
			if(flag == 1) ans++;
		}
		printf("%d\n", ans);	
	}
	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