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

思路:用差分数组维护区间的增减,坑点是有可能出现相同区间,如果出现相同的区间就不用处理了,真坑,wa了,用map标记之前是否出现过

其余应该比较简单

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<stack>
#include<map>
#define MAX 10005typedef long long ll;using namespace std;map<int,int>mp;
int a[MAX];
int d[MAX];
int vis[1000005];
int main()
{int N,I,H,R;cin>>N>>I>>H>>R;for(int t=1;t<=N;t++){a[t]=H;}for(int t=1;t<=N;t++){d[t]=a[t]-a[t-1];}int l,r;for(int t=0;t<R;t++){scanf("%d%d",&l,&r);if(max(l,r)-min(l,r)>=2&&mp[l]!=r){d[min(l,r)+1]--;d[max(l,r)]++;mp[l]=r;}}for(int t=1;t<=N;t++){a[t]=a[t-1]+d[t];}for(int t=1;t<=N;t++){cout<<a[t]<<endl;}return 0;
}

转载于:https://www.cnblogs.com/Staceyacm/p/10781798.html

C - 思考使用差分简化区间操作相关推荐

  1. 封装CoreGraphics的API简化绘图操作

    封装CoreGraphics的API简化绘图操作 效果 说明 1. 将CoreGraphics的API接口抽象为对象,让绘图变得简单易懂 2. 简化常用的绘制操作 3. 源码长期更新 源码 https ...

  2. C# 使用 Index 和 Range 简化集合操作

    C# 使用 Index 和 Range 简化集合操作 Intro 有的语言数组的索引值是支持负数的,表示从后向前索引,比如:arr[-1] 从 C# 8 开始,C# 支持了数组的反向 Index,和 ...

  3. 在ASP.NET Core中使用AOP来简化缓存操作

    前言 关于缓存的使用,相信大家都是熟悉的不能再熟悉了,简单来说就是下面一句话. 优先从缓存中取数据,缓存中取不到再去数据库中取,取到了在扔进缓存中去. 然后我们就会看到项目中有类似这样的代码了. pu ...

  4. 利用伸展树提高区间操作的性能

    一.首先,什么是区间操作?以及各种数据结构性能对比 区间操作就是对一个序列的某个区间的所有元素进行的操作.比如,对区间所有元素增加一个值,翻转区间元素等. 对区间操作,最普通的方法就是数组.比如:对一 ...

  5. 经纬财富:铜陵支撑和阻力位,可小区间操作

    [消息面] 美国公布的经济数据好坏不一,初请失业金人数好于预期,但房市数据差于预期,现货白银略微冲高至20.84美元/盎司,整体位于0.10美元的区间操作. [技术面] 现货白银日线昨日收取一根小阳, ...

  6. 简化电脑操作:比快捷键还好用、方便、试试

    在日常的电脑操作中,其实也有很多环节可以进行简化. 什么叫简化操作流程呢? 搜索网页上的某个关键词:通常是"选中文字.复制.新建标签页.地址栏粘贴文字.回车": 但随着浏览器的发展 ...

  7. 利用Java存储过程简化数据库操作

       利用Java存储过程沟通SQL.XML.Java.J2EE和Web服务. 存储过程(stored procedure)允许将运行于数据库层中的持久性逻辑与运行于中间层中的商务逻辑有效地分离开来. ...

  8. 【Spring学习】使用Spring的jdbcTemplate简化JDBC操作

    applicationContext.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  9. 【CF566#D】 Restructuring Company (并查集---合并区间操作)

    题干: Even the most successful company can go through a crisis period when you have to make a hard dec ...

最新文章

  1. 论场景在研发中的重要性
  2. des解密 given final_真相解密创新Aurvana Live SE评测怎么样?【使用一个月后感受实情爆料!!!...
  3. euv光刻机有什么用_台积电又买了13台EUV光刻机?
  4. oracle 加全文索引,oracle全文索引的创建和使用
  5. selenium架构_Selenium测试的干净架构
  6. unity3d 动态合批设置_Unity3D SkinnedMeshRenderer合批优化
  7. 4-1 简单输出整数 (10分)
  8. [转]itertools --- 为高效循环而创建迭代器的函数
  9. g2o图优化简介与基本使用方法
  10. Makefile.am、Makefile.in、Makefile、configure.ac关系(十二)
  11. linux popen阻塞_linux popen()与system()的区别
  12. 用户、角色、权限管理-设计方案之权限检测
  13. oracle if 语句的使用
  14. LINUX打包并下载到本地
  15. 微信H5开发问题集锦
  16. 【熊出没注意!】大家都是怎么治那些来家里玩的熊孩子的?
  17. 漫步数学分析二十二——魏尔斯特拉斯测试
  18. ffmpeg音频合并生成新的音频文件
  19. 欧文分校的计算机科学博士,2020年加州大学欧文分校博士含金量
  20. Spring Cloud--Sleuth+Zipkin 链路跟踪/订单的流量削峰

热门文章

  1. 设计模式_3_建造者模式
  2. LeetCode 1974. 使用特殊打字机键入单词的最少时间
  3. LeetCode 1551. 使数组中所有元素相等的最小操作数(等差数列)
  4. LeetCode 369. 给单链表加一(递归)
  5. LeetCode 170. 两数之和 III - 数据结构设计(哈希map)
  6. LeetCode 1318. 或运算的最小翻转次数(位运算)
  7. 程序员面试金典 - 面试题 17.15. 最长单词(排序+递归)
  8. 程序员面试金典 - 面试题 16.19. 水域大小(BFS/DFS)
  9. LintCode 802. 数独(回溯)/ LeetCode 37. 解数独
  10. nineoldandroid使用_nineoldandroid 详细使用并且实现drawerlayout侧滑动画