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


题意:给出牛的可能最高身高。然后输入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(线段树)的更多相关文章

  1. POJ 3263 Tallest Cow 题解

    题目 FJ's \(N (1 ≤ N ≤ 10,000)\) cows conveniently indexed 1..N are standing in a line. Each cow has a ...

  2. 【差分】POJ 3263 Tallest Cow

    题目大意 POJ链接 给出\(n\)头牛的身高,和\(m\)对关系,表示牛\(a[i]\)与\(b[i]\)可以相互看见.已知最高的牛为第\(p\)头,身高为\(h\). 求每头牛的身高最大可能是多少 ...

  3. poj 3263 Tallest Cow

    一个压了很久的题目,确实很难想,看了别人的做法后总算明白了. 首先要明白一点,因为题目说明了不会有矛盾,所以题目给出来的区间是不能相交的,否则是矛盾的.(原因自己想) 然后既然区间只能是包含的,就很明 ...

  4. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  5. poj 3264(RMQ或者线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42929   Accepted: 20184 ...

  6. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  7. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  8. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  9. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

随机推荐

  1. js模拟输入支付密码

    html <div class="content"> <!--<div class="title">支付宝支付密码:</di ...

  2. hibernate5.3版本出现hibernate中The server time zone value“乱码”问题的解决办法。

    <!-- 配置关于数据库连接的四个项 driverClass url username password --> <property name="hibernate.con ...

  3. 对散列进行sort排序,只是会产生一个顺序,但这顺序和初始化时的顺序不一致

    109 my $slice_layouts;    110 my $vertical_assignment;    111 my %map_function_indices = (    112    ...

  4. JavaScript异步编程解决方案探究

    javascript的天生单线程特性,使得异步编程对它异常重要,早期的通常做法是用回调函数来解决.但是随着逻辑的复杂,和javascript在服务端的大显神通,使得我们很容易就陷入“回调陷井”的万丈深 ...

  5. centos7配置静态IP步骤

    centos7按照初始安装时候的developer类型一路装好,在vmware里已经设置为bridge模式,按理说是会自动按照DHCP联网成功的,结果却发现连网卡都没有激活,这里记录下. 1:我要把L ...

  6. 安装php扩展(以swoole)为例

    一.下载swoole到/usr/local/src目录下,操作 git clone https://gitee.com/swoole/swoole.git; 二.cd swoole,phpize(如果 ...

  7. memcached协议解析 及使用

    本文转载自:http://www.ccvita.com/306.html 协议memcached 的客户端使用TCP链接与服务器通讯.(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的 ...

  8. ruby on rails 常见配置错误解决

    error:in `require': cannot load such file -- sqlite3/sqlite3_native (LoadError) 先删除 Ruby下的D:\Ruby22- ...

  9. 选项B中:int b[][3]={0,1,2,3}

    选项B中:int b[][3]={0,1,2,3};等价于 int b[][3]={0,1,2,3,0,0};    int b[][3]={0,1,2,3,4};         cout<& ...

  10. 【Kafka问题解决】Connection to xxx could not be established. Broker may not be available.

    请检查Kafka的config/server.properties 看看是否有填写 listeners=PLAINTEXT://kafka-host:9092 advertised.listeners ...