D2. Toy Train
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of nn stations, enumerated from 11through nn. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station ii is station i+1i+1 if 1≤i<n1≤i<n or station 11 if i=ni=n. It takes the train 11 second to travel to its next station as described.

Bob gave Alice a fun task before he left: to deliver mm candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 11 through mm. Candy ii (1≤i≤m1≤i≤m), now at station aiai, should be delivered to station bibi (ai≠biai≠bi).

The blue numbers on the candies correspond to bibi values. The image corresponds to the 11-st example.

The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.

Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.

Input

The first line contains two space-separated integers nn and mm (2≤n≤50002≤n≤5000; 1≤m≤200001≤m≤20000) — the number of stations and the number of candies, respectively.

The ii-th of the following mm lines contains two space-separated integers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n; ai≠biai≠bi) — the station that initially contains candy ii and the destination station of the candy, respectively.

Output

In the first and only line, print nn space-separated integers, the ii-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station ii.

Examples
input

Copy
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
output

Copy
10 9 10 10 9
input

Copy
2 3
1 2
1 2
1 2
output

Copy
5 6
Note

Consider the second sample.

If the train started at station 11, the optimal strategy is as follows.

  1. Load the first candy onto the train.
  2. Proceed to station 22. This step takes 11 second.
  3. Deliver the first candy.
  4. Proceed to station 11. This step takes 11 second.
  5. Load the second candy onto the train.
  6. Proceed to station 22. This step takes 11 second.
  7. Deliver the second candy.
  8. Proceed to station 11. This step takes 11 second.
  9. Load the third candy onto the train.
  10. Proceed to station 22. This step takes 11 second.
  11. Deliver the third candy.

Hence, the train needs 55 seconds to complete the tasks.

If the train were to start at station 22, however, it would need to move to station 11 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1=65+1=6 seconds.

D2. Toy Train的更多相关文章

  1. CF1130D Toy Train

    D Toy Train 开始时,对于一个点 \(x\) ,若没有糖果需要运走,则不考虑; 否则,若点上有 \(k\) 颗糖果需要运走,火车每次只能搭上 \(1\) 个,显然经过这个点至少 \(k\) ...

  2. D1. Toy Train (Simplified)

    D1. Toy Train (Simplified) time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. Codeforces Round #542(Div. 2) D1.Toy Train

    链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...

  4. Codeforces 1130D1 Toy Train (Simplified) (思维)【贪心】

    <题目链接> 题目大意: 有一个的环形火车站,其中有$[1,n] n$个站台,站台上能够放糖果,火车只能朝一个方向移动,如果火车在站台$i$,那么下一秒就会在$i+1$站(如果$i=n$, ...

  5. Toy Train(贪心)

    题目链接:http://codeforces.com/contest/1130/problem/D1 题目大意:给你n个点,然后m条运输任务,然后问你从每个点作为起点是,完成这些运输任务的最小花费?每 ...

  6. DMOJ IOI '17 P3 - Toy Train【拓扑排序】

    传送:https://dmoj.ca/problem/ioi17p3 参考:https://blog.csdn.net/qq_27327327/article/details/80711824 妙啊- ...

  7. 【Codeforces 1129A】Toy Train

    [链接] 我是链接,点我呀:) [题意] 火车从1,2,3...n->1的方式绕圈走.(即每次从i走到i+1) 有一些点有货物需要装载,但是每个点只能装上去一个货物. 每个货物都有目标点卸货点( ...

  8. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2

    A. Be Positive 链接:http://codeforces.com/contest/1130/problem/A 题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后 ...

  9. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

    A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...

随机推荐

  1. mac系统下安装mysql步骤

    1.下载mysql-5.7.13-osx10.11-x86_64.dmg安装包,并点击dmg安装包进行安装 2.安装完成后弹出如以下提示信息: 2016-06-23T01:14:48.649253Z ...

  2. 函数,#include <>和#include " "区别

    l 4..函数定义到使用分三步:1.声明.2.定义函数.3.调用函数. 2.函数的定义格式:返回值 函数名(形参列表){ 函数体; return } 命名规则:1.只能由字母.数字.下划线或者美元符号 ...

  3. [Usaco2009 Dec] 过路费

    [题目链接] https://www.luogu.org/problemnew/show/P2966 [算法] SPFA最短路 时间复杂度 : O(N ^ 2) [代码] #include<bi ...

  4. 【WebRTC音频预处理单元APM的整体编译及使用 - android】

    前言 在写[单独编译使用WebRTC的音频处理模块 - android]一文之前,就一直想直接把WebRTC的音频处理引擎VoE整个儿编译出来供自己的项目使用,但限于技术拙劣.时间紧迫,所以没能成功. ...

  5. 小程序-demo:小程序示例-page/common

    ylbtech-小程序-demo:小程序示例-page/common 1.返回顶部 0.     1. 2. pages/common返回顶部 1. -lib --weui.wxss /*! * we ...

  6. 《Image-to-Image Translation with Conditional Adversarial Networks》论文笔记

    出处 CVPR2017 Motivation 尝试用条件GAN网络来做image translation,让网络自己学习图片到图片的映射函数,而不需要人工定制特征. Introduction 作者从不 ...

  7. Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import

    出处:http://blog.csdn.net/kernelspirit/article/details/3381666 最近在看<Python源码剖析>,对Python内部运行机制比以前 ...

  8. bzoj 4552: [Tjoi2016&Heoi2016]排序【二分+线段树】

    二分值mid,然后把>=mid的赋值为1,其他赋值为0,每次排序就是算出区间内01的个数,然后分别把0和1放到连续的一段内,这些都可以用线段树来维护 二分的判断条件是操作完之后q位置上是否为1 ...

  9. python之定时器Timer

    timer类 Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法. 构造方法: Timer(interval, function, args=[], kwargs={})  in ...

  10. icons使用

    1.将选中图标加入项目 2.unicode方式查看连接在线连接 3.复制代码到样式表 4.引用样式,并设置I标签,颜色和大小可以通过设置i标签color和font-size进行调整 <i cla ...