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. layui timeline使用

    使用layui timeline过程记录: layui官网时间线介绍比较少,可能是太简单了,这里把时间线通过请求后台数据.再自动填写到对应区块,进行了封装: 代码如下: function timeli ...

  2. 笔试算法题(04):实现 string & memcpy & strcpy & strlen

    出题:请实现给定String的类定义: 分析:注意检查标准类构造注意事项: 解题: #include <stdio.h> #include <string.h> /** * 检 ...

  3. mybatis传多个参数(不使用@param注解情况下),3.4.2版本之后出现#{0}-#{n}参数绑定异常

    解决方案: 在mybatis配置文件中声明setting属性的useActualParamName 参数值为false ** 这种方法解决mybatis3.4.2之后的版本产生该问题的解决方法**

  4. 用PHP的GD库画五星红旗来玩玩

    1 header("Content-Type:image/jpeg"); $img=imagecreatetruecolor(999,667); $color=imagecolor ...

  5. PHP读取超大的excel文件数据的方案

    场景和痛点 说明 今天因为一个老同学找我,说自己公司的物流业务都是现在用excel处理,按月因为数据量大,一个excel差不多有百万数据,文件有接近100M,打开和搜索就相当的慢 联想到场景:要导入数 ...

  6. CSS3 @font-face的url要添加?#iefix的原因

    转至:https://github.com/CSSLint/csslint/wiki/Bulletproof-font-face When using @font-face to declare mu ...

  7. MySQL-----改

    改 **修改用户名** rename user 'username'@'IP address' to 'new username'@'IP address'; **修改密码** set passwor ...

  8. UI & 原型

    UI & 原型 产品原型设计与团队实时协作平台 https://www.xiaopiu.com/download https://www.xiaopiu.com/forum/posts/5b9 ...

  9. DRF JWT的用法 & Django的自定义认证类 & DRF 缓存

    JWT 相关信息可参考: https://www.jianshu.com/p/576dbf44b2ae DRF JWT 的使用方法: 1. 安装 DRF JWT # pip install djang ...

  10. Spring Boot Jpa 表名小写转大写

    今天在使用SpringBoot整合Hibernate后创建表,表名为小写,而在linux下,mysql的表名是区分大小写的,因此在我的数据表中,就出现了两个一样的表 act_id_user 和  AC ...