poj 3263 Tallest Cow(线段树)
Language:
Default
Tallest Cow
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 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, B ≤ 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 Sample Output 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(线段树)的更多相关文章
- 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 ...
- 【差分】POJ 3263 Tallest Cow
题目大意 POJ链接 给出\(n\)头牛的身高,和\(m\)对关系,表示牛\(a[i]\)与\(b[i]\)可以相互看见.已知最高的牛为第\(p\)头,身高为\(h\). 求每头牛的身高最大可能是多少 ...
- poj 3263 Tallest Cow
一个压了很久的题目,确实很难想,看了别人的做法后总算明白了. 首先要明白一点,因为题目说明了不会有矛盾,所以题目给出来的区间是不能相交的,否则是矛盾的.(原因自己想) 然后既然区间只能是包含的,就很明 ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- poj 3264(RMQ或者线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42929 Accepted: 20184 ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
随机推荐
- PowerDesigner连接MySQL数据库
详细步骤请点击下面的链接查看! 我在网上找了很多篇教程, 其中这一篇是最好的. 可以成功的帮助我们把PowerDesigner和MySQL数据库相连. PowerDesigner真的非常强大! 设计数 ...
- 03XML Schema Definition
1. XML Schema Definition 1. XML Schema Definition XML Schema(XML Schema Definition,XSD)用于描述 XML 文档的结 ...
- wampserver更改语言步骤
wampserver更改语言步骤的具体步骤: 右击屏幕右下角图标>选择language>选择更改的语言
- [Luogu] P4838 P哥破解密码
题目背景 P哥是一个经常丢密码条的男孩子. 在ION 8102赛场上,P哥又弄丢了密码条,笔试满分的他当然知道这可是要扣5分作为惩罚的,于是他开始破解ION Xunil系统的密码. 题目描述 定义一个 ...
- Mysql:零散记录
limit用法 查询第4行记录 select * from tablename limit 3,1; limit 3,1:截取第3行加1行的数据 查询第6-15行 select * from tabl ...
- win7右键菜单不见解决办法
直接 开始 运行: cmd /k reg add "HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandlers\Ne ...
- CEO的作用
看到有人讨论CEO的作用. 一个观点认为CEO有三大任务: 1)为公司确定战略,并与股东沟通 2)为公司其他职位找来合适的人员 3)保证公司随时有足够的钱 他认为,可能CEO会有其他的作用,但是这三点 ...
- Swift - 修改导航栏“返回”按钮文字,图标
Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037 项目中常常会使用 UINavigationController 对各个页面进行导 ...
- nativeLibraryDirectories=[/data/app/com.lukouapp-1/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libxxxx.so
一开始我遇到的问题是,如果手机有SD卡槽,则不管有没有插卡,都会闪退,打日记后发现是找不到so文件.报错日记如下: nativeLibraryDirectories=[/data/app/com.lu ...
- Webdriver元素定位2(XPath)
XPath即为XML路径语言,它是一种用来确定XML文档中某部分位置的语言.XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力. 绝对路径定位 案例:在百度首页搜索框输入selenium ...