程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 3263 Tallest Cow(線段樹)

poj 3263 Tallest Cow(線段樹)

編輯:C++入門知識

poj 3263 Tallest Cow(線段樹)


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 R (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: N, I, H and R
Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ A, BN), 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

USACO 2007 January Silver

題意:給出牛的可能最高身高,然後輸入m組數據 a b,代表a,b可以相望,最後求所有牛的可能最高身高輸出


注意問題:1>可能有重邊

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


詳情看代碼:


#include
#include
#include
#include

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





  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved