poj 3263 Tallest Cow(线段树)


Language:
Default

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

USACO 2007 January Silver

题意:给出牛的可能最高身高,然后输入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;
}
时间: 2024-10-11 17:22:34

poj 3263 Tallest Cow(线段树)的相关文章

Tallest Cow(线段树较易)

题目描述 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 tha

POJ 3264-Balanced Lineup(线段树:单点更新+区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

poj 2182 Lost Cows(线段树经典题)

题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9152   Accepted: 5879 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, th

POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

解题报告 题意: 对线段染色,询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话访问线段树,求出颜色种数.结果超时了,最坏的情况下,染色可以染到叶子节点. 换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了,用按位或来处理颜色种数. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

POJ训练计划1177_Picture(扫描线/线段树+离散)

解题报告 题意: 求矩形周长和. 思路: 左扫上扫,扫过了. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct Seg { int lx,rx,ly,ry,h,v; friend bool operator < (Seg a,Seg b) { re

POJ训练计划2828_Buy Tickets(线段树/单点更新)

解题报告 题意: 插队完的顺序. 思路: 倒着处理数据,第i个人占据第j=pos[i]+1个的空位. 线段树维护区间空位信息. #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int x,v; } num[201000]; int sum[1000000],ans[201000]; void cbtree(int rt,int l,in

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

POJ 2155 二维线段树

POJ 2155  二维线段树 思路:二维线段树就是每个节点套一棵线段树的树. 刚开始因为题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂,后面自己把图画出来了才理解. 因为只有0和1,所以可以用异或来搞,而不需要每次都需要修改. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #incl