CF1249B2.Books Exchange (hard version)
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The only difference between easy and hard versions is constraints.
There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the pi-th kid (in case of i=pi the kid will give his book to himself). It is guaranteed that all values of pi are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed.
For example, if n=6 and p=[4,6,1,3,5,2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on.
Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n.
Consider the following example: p=[5,1,2,4,3]. The book of the 1-st kid will be passed to the following kids:
- after the 1-st day it will belong to the 5-th kid,
- after the 2-nd day it will belong to the 3-rd kid,
- after the 3-rd day it will belong to the 2-nd kid,
- after the 4-th day it will belong to the 1-st kid.
So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.
You have to answer q independent queries.
简单版本与困难版本的唯一区别在于约束条件。
有 n 个孩子,每人正在阅读一本独一无二的书。每天结束时,第 i 个孩子会将自己的书交给第 pi 个孩子(若 i=pi,则该孩子将书交还给自己)。保证所有 pi 的值均为 1 到 n 之间的互不相同的整数(即 p 是一个排列)。序列 p 每天保持不变,是固定的。
例如,若 n=6 且 p=[4,6,1,3,5,2],则第一天结束时,第 1 个孩子的书将属于第 4 个孩子,第 2 个孩子的书将属于第 6 个孩子,依此类推。第二天结束时,第 1 个孩子的书将属于第 3 个孩子,第 2 个孩子的书将属于第 2 个孩子,依此类推。
你的任务是:对每个 i(从 1 到 n),求出第 i 个孩子的书首次回到他自己手中的那一天的编号。
考虑如下示例:p=[5,1,2,4,3]。第 1 个孩子的书将按以下顺序传递:
- 第 1 天结束后,它将属于第 5 个孩子,
- 第 2 天结束后,它将属于第 3 个孩子,
- 第 3 天结束后,它将属于第 2 个孩子,
- 第 4 天结束后,它将属于第 1 个孩子。
因此,第 1 个孩子的书将在第 4 天后回到其主人手中。第 4 个孩子的书则恰好在第 1 天后首次回到他自己手中。
你需要回答 q 个相互独立的查询。
输入格式
The first line of the input contains one integer q (1≤q≤1000) — the number of queries. Then q queries follow.
The first line of the query contains one integer n (1≤n≤2⋅105) — the number of kids in the query. The second line of the query contains n integers p1,p2,…,pn (1≤pi≤n, all pi are distinct, i.e. p is a permutation), where pi is the kid which will get the book of the i-th kid.
It is guaranteed that ∑n≤2⋅105 (sum of n over all queries does not exceed 2⋅105).
输入的第一行包含一个整数 q(1≤q≤1000),表示查询次数。随后是 q 次查询。
每次查询的第一行包含一个整数 n(1≤n≤2⋅105),表示该次查询中孩子的数量。查询的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n,且所有 pi 互不相同,即 p 是一个排列),其中 pi 表示第 i 个孩子所拥有的书将被传递给的孩子编号。
保证所有查询的 n 之和满足 ∑n≤2⋅105(即所有查询中 n 的总和不超过 2⋅105)。
输出格式
For each query, print the answer on it: n integers a1,a2,…,an, where ai is the number of the day the book of the i-th child is returned back to him for the first time in this query.
对于每个查询,请输出对应的答案:n 个整数 a1,a2,…,an,其中 ai 表示在该查询中,第 i 个孩子的书首次归还给他的那一天的编号。
输入输出样例
输入#1
6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3
输出#1
1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
输入解题思路,AI测评打分。不知道怎么写?