题目描述

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 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, BN), 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

分析

一句话题意:给出n头牛的身高,和m对关系(a[i]b[i]可以相互看见。即他们中间的牛都比他们矮)。已知最高的牛为第p头,身高为h,求每头牛的身高最大可能是多少。

因为我们要是每一头牛的身高尽量高,所以我们初始的时候设每一头牛的身高都为\(h\)

因为两头牛\(x,y\)之间可以相互看到,所以我们需要把区间\([x+1,y-1]\)内牛的身高都减去1

直接操作的话显然会T掉,所以我们可以用差分来维护

注意重复的操作要判断一下

代码

#include<cstdio>
#include<map>
#include<iostream>
#include<algorithm>
#include<utility>
using namespace std;
const int maxn=1e6+5;
map<pair<int,int>,bool> ma;
int d[maxn];
int main(){
int n,m,h,r;
scanf("%d%d%d%d",&n,&m,&h,&r);
for(int i=1;i<=r;i++){
int aa,bb;
scanf("%d%d",&aa,&bb);
if(aa>bb) swap(aa,bb);
if(ma[make_pair(aa,bb)]) continue;
d[aa+1]--,d[bb]++;
ma[make_pair(aa,bb)]=1;
}
for(int i=1;i<=n;i++){
d[i]+=d[i-1];
printf("%d\n",h+d[i]);
}
return 0;
}

POJ3263 Tallest Cow 差分的更多相关文章

  1. poj3263 Tallest Cow

    题意略去. 考虑给定的R对pair(A, B). 即A能看见B,这意味着B不比A低,并且区间内部的所有元素的高度严格小于A的高度. 我们规定区间的方向:若A > B,为反方向,反之称为正方向. ...

  2. bzoj1635 / P2879 [USACO07JAN]区间统计Tallest Cow

    P2879 [USACO07JAN]区间统计Tallest Cow 差分 对于每个限制$(l,r)$,我们建立一个差分数组$a[i]$ 使$a[l+1]--,a[r]++$,表示$(l,r)$区间内的 ...

  3. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分

    Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...

  4. 【差分】Tallest Cow

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

  5. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛(差分序列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1635 差分序列是个好东西啊....很多地方都用了啊,,, 线性的进行区间操作orz 有题可知 h[a ...

  6. [Luogu2879][USACO07JAN]区间统计Tallest Cow

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

  7. 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 ...

  8. BZOJ1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 346  Solved: 184 ...

  9. BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Description FJ's N ( ...

随机推荐

  1. 栈 & 队列

    栈 先进者后出,后进者先出,LIFO,典型的"栈"结构 从栈的操作特性上来看,栈是一种"操作受限"的线性表,只允许在一段插入和删除数据. 在功能上来说,数组和链 ...

  2. OAuth + Security - 5 - Token存储升级(数据库、Redis)

    PS:此文章为系列文章,建议从第一篇开始阅读. 在我们之前的文章中,我们当时获取到Token令牌时,此时的令牌时存储在内存中的,这样显然不利于我们程序的扩展,所以为了解决这个问题,官方给我们还提供了其 ...

  3. css布局中的垂直水平居中对齐

    前言 我们都知道,固定高宽的div在网页中垂直居中很简单,相信大家也很容易的写出来,但是不是固定高宽的div如何垂直居中呢?我们在网页布局,特别是手机等web端网页经常是不固定高宽的div,那么这些d ...

  4. 自动完成 APP【字典树(Trie树)+dfs】

    自动完成 APP 传送门  来源:upc12786 题目描述 奶牛 Bessie 很喜欢用手机上网聊天,但她的蹄子太大,经常会按到好几个键造成不必要的麻烦(丢死人了,你下辈子还是不要当奶牛了).于是 ...

  5. JSONobject按照put顺序存储和读取

    new的时候加true即可: JSONObject jsonObject = new JSONObject(true);

  6. SpringBoot从入门到放弃之配置Spring-Data-JPA自动建表

    pom文件配置引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  7. swiper 实现滑动解锁

    最近项目中有这样一个需求,研究了两种写法一个原生,一个使用框架 原生写法: <!DOCTYPE html> <html> <head> <meta chars ...

  8. 警告Establishing SSL connection without server's identity verification is not recommended

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] SpringBo ...

  9. 从Spring Initializr开始

    出识springcloud我们这里需要建立两个项目 来感受下微服务 一.配置服务 1. Spring Initializr. 用idea自带的 Spring Initializr. 建立第一个项目 2 ...

  10. Day10-微信小程序实战-交友小程序-添加好友功能之创建并更新message信息

    1.首先要在 添加好友 这个按钮上添加一个事件,也就是在detail.wxml的添加好友这个按钮的哪里,添加一个点击事件 handleAddFriend 并且添加好友还要考虑,现在是已登陆状态还是未登 ...