码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3263 Tallest Cow(线段树)

时间:2014-10-23 22:44:04      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:线段树

Language:
Tallest Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1964   Accepted: 906

Description

FJ‘s N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of that cow.

FJ has made a list of (0 ≤ R ≤ 10,000) lines of the form "cow 17 sees cow 34". This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.

For each cow from 1..N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

Input

Line 1: Four space-separated integers: NIH and R 
Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ AB ≤ N), indicating that cow A can see cow B.

Output

Lines 1..N: Line i contains the maximum possible height of cow i.

Sample Input

9 3 5 5
1 3
5 3
4 3
3 7
9 8

Sample Output

5
4
5
3
4
4
5
5
5

Source



题意:给出牛的可能最高身高,然后输入m组数据 a b,代表a,b可以相望,最后求所有牛的可能最高身高输出


注意问题:1>可能有重边

                    2>  a,b可以相望就是要降低a+1到b-1之间的牛的身高


详情看代码:


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define INF 0x3f3f3f3f
#define N  1000005

int n,i,h,m;
int lle[N],rri[N],k;

struct stud{
int le,ri;
int va;
}f[N];

void pushdown(int pos)
{

    if(f[pos].va==0) return ;
	  f[L(pos)].va+=f[pos].va;
      f[R(pos)].va+=f[pos].va;
      f[pos].va=0;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].va=0;
	if(le==ri) return;

	int mid=MID(le,ri);
	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);
}

void update(int pos,int le,int ri)
{
	if(f[pos].le==le&&f[pos].ri==ri)
	{
		f[pos].va++;
		return ;
	}
    pushdown(pos);

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
		update(L(pos),le,ri);
	else
		if(mid<le)
		update(R(pos),le,ri);
	else
	{
		update(L(pos),le,mid);
		update(R(pos),mid+1,ri);
	}

}

int query(int pos,int le)
{
	if(f[pos].le==le&&f[pos].ri==le)
	{
		return h-f[pos].va;
	}
    pushdown(pos);

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=le)
		return query(L(pos),le);
	else
		return query(R(pos),le);
}

int main()
{
	int i,j;
	while(~scanf("%d%d%d%d",&n,&i,&h,&m))
	{
		build(1,1,n);
		k=0;
		lle[0]=rri[0]=0;
		int le,ri;
		while(m--)
		{
			scanf("%d%d",&le,&ri);
			if(le>ri) {i=le; le=ri;ri=i;}
			if(le+1==ri) continue;
			for(i=0;i<k;i++)
			if(lle[i]==le&&rri[i]==ri)
			{
				i=-1;
				break;
			}
           if(i==-1) continue;
           lle[k]=le;
           rri[k++]=ri;
           update(1,le+1,ri-1);
		}

	    for(i=1;i<=n;i++)
		{
			printf("%d\n",query(1,i));
		}

	}
   return 0;
}







poj 3263 Tallest Cow(线段树)

标签:线段树

原文地址:http://blog.csdn.net/u014737310/article/details/40404483

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!