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个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
随机推荐
- CAD插入背景图片(网页版)
把图片作为背景图片可见但是不能编辑操作. 主要用到函数说明: _DMxDrawX::DrawImageToBackground 绘光栅图到背景.详细说明如下: 参数 说明 BSTR sFileName ...
- 多线程下单例模式的实现_ThreadLocal_ReentrantLock
package threadStudy; public class MultiThreadSingleInstance { // volatile 防止指令重排 private static vola ...
- 散列的键值对没初始化时不要用print打印此值,不要用 . 操作符去连接打印 这个值。
31 delete $vertical_alignment{$anonymous}; 32 print $vertical_alignment{$anonymous}."\n&quo ...
- 第2节 mapreduce深入学习:8、手机流量汇总求和
第2节 mapreduce深入学习:8.手机流量汇总求和 例子:MapReduce综合练习之上网流量统计. 数据格式参见资料夹 需求一:统计求和 统计每个手机号的上行流量总和,下行流量总和,上行总流量 ...
- vs2008如何新建自己工程的环境变量(局部)和 Windows系统(全局). .
在vs2008的Project->Property设置里经常会看到类似$(IntDir).$(OutDir).$(ProjectName) 的预定义宏.以vc2008为例,有时候我们在引用别的库 ...
- python 列表生成式、lower()和upper()的使用
参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868196389 ...
- iOS视频通话方案
现在iPhone4平台上实时音视频对话已取得初步成果.其间查阅了很多资料,感谢这些信息的提供者.继往开来,我写下此文.我只列出要点,具体编码以及平台移植各位自己去努力吧.照着下面的步骤,您一定能做出来 ...
- getQueryString(option)的用法
//页面参数接收1.function getQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...
- poj3134 Power Calculus
题目描述: 你现在有x^1,每动一步可以用当前存在的x^a和x^b获得x^(a+b)或x^(abs(a-b)).给出n(n<=1000),求最少多少步能得到x^n. 题解: IDDFS.枚举步数 ...
- Dash Speed
题目大意: 比特山是比特镇的飙车圣地.在比特山上一共有n 个广场,编号依次为1 到n,这些广场之间通过n - 1 条双向车道直接或间接地连接在一起,形成了一棵树的结构.因为每条车道的修建时间以及建筑材 ...