Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 1059/D. Beautiful Permutation
Prev
CodeforcesCodeforces
Expert
Codeforces Round 1059

D. Beautiful Permutation

ArrayMathInteractive
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

This is an interactive problem.

There is a permutation

βˆ—

 π‘

 of length π‘›

.

Someone secretly chose two integers π‘™,π‘Ÿ

 (1β‰€π‘™β‰€π‘Ÿβ‰€π‘›

) and modified the permutation in the following way:

  • For every index π‘–
  •  such that π‘™β‰€π‘–β‰€π‘Ÿ
  • , set π‘
  • 𝑖
  • :=𝑝
  • 𝑖
  • +1
  • .

Let π‘Ž

 denote the resulting array obtained by modifying the permutation.

You are given an integer π‘›

 denoting the length of the permutation π‘

.

In one query, you are allowed to choose two integers π‘™,π‘Ÿ

 (1β‰€π‘™β‰€π‘Ÿβ‰€π‘›

) and ask for the sum of the subarray either of the original permutation π‘[π‘™β€¦π‘Ÿ]

 or of the modified array π‘Ž[π‘™β€¦π‘Ÿ]

. The answer to such a query will be the corresponding integer sum.

Your task is to find the pair (𝑙,π‘Ÿ)

 that was chosen to obtain π‘Ž

 in no more than 40

 queries.


βˆ—

A permutation of length π‘›

 is an array consisting of π‘›

 distinct integers from 1

 to π‘›

 in any order. For example, [2,3,1,5,4]

 is a permutation, but [1,2,2]

 is not a permutation (the number 2

 appears twice in the array), and [1,3,4]

 is also not a permutation (𝑛=3

, but the array contains 4

).

Input


The first line of input contains a single integer π‘‘

 (1≀𝑑≀10

4

) β€” the number of test cases.

Each test case contains a single integer π‘›

 (1≀𝑛≀2β‹…10

4

) β€” the length of the permutation.

It is guaranteed that the sum of π‘›

 over all the test cases does not exceed 2β‹…10

4

.

Interaction


The interaction for each test case begins by reading the integer π‘›

.

You can ask two types of queries.

  • Print "𝟷 πš• πš›
  • " (1β‰€π‘™β‰€π‘Ÿβ‰€π‘›
  • ).
  • In response, you should read a line containing a single integer π‘₯
  •  β€” the sum of the subarray of the original permutation. (Formally, π‘₯=𝑝
  • 𝑙
  • +𝑝
  • 𝑙+1
  • +β‹―+𝑝
  • π‘Ÿ
  • ).
  • Print "𝟸 πš• πš›
  • " (1β‰€π‘™β‰€π‘Ÿβ‰€π‘›
  • ).
  • In response, you should read a line containing a single integer π‘¦
  •  β€” the sum of the subarray of the modified array. (Formally, π‘¦=π‘Ž
  • 𝑙
  • +π‘Ž
  • 𝑙+1
  • +β‹―+π‘Ž
  • π‘Ÿ
  • ).

The permutation π‘

 and the chosen integers π‘™

, π‘Ÿ

 are fixed beforehand and can't be changed during the time of interaction.

You can output the final answer by printing "! πš• πš›

", where π‘™

, π‘Ÿ

 denote the integers that were chosen to obtain π‘Ž

. After printing the answer, your program should proceed to the next test case or terminate if there are no more.

You can ask no more than 40

 queries per testcase. Printing the answer doesn't count as a query. If your program performs more than 40

 queries for one test case or makes an invalid query, you may receive a Wrong Answer verdict.

After printing a query, do not forget to output the end of line and flush

βˆ—

 the output. Otherwise, you will get Idleness limit exceeded.

Hacks

To make a hack, use the following test format.

The first line should contain a single integer π‘‘

 (1≀𝑑≀10

4

)

 β€” the number of testcases.

The first line of each test case should contain a single integer π‘›

 (1≀𝑛≀2β‹…10

4

) β€” the length of the permutation π‘

.

The second line of each test case should contain π‘›

 integers π‘

𝑖

 (1≀𝑝

𝑖

≀𝑛

) β€” denoting the permutation π‘

.

The third line of each test case should contain two space-separated integers π‘™

, π‘Ÿ

 (1β‰€π‘™β‰€π‘Ÿβ‰€π‘›

) β€” the chosen integers.

For example, the following is the hack format of the example test:

2
3
3 1 2
2 2
4
2 1 3 4
2 4


βˆ—

To flush, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages

Example

Input

Copy

2


3

4

5


4

8

8

9

Output

Copy

1 1 2

2 1 2

! 2 2

1 2 4

2 1 3

2 3 4

! 2 4

Note


For the first testcase, π‘=[3,1,2]

 and π‘™=2

, π‘Ÿ=2

. Hence, the modified array π‘Ž

 will be equal to [3,2,2]

.

So, querying "𝟷 πŸ· πŸΈ

" gives π‘

1

+𝑝

2

=3+1=4

. And querying "𝟸 πŸ· πŸΈ

" gives π‘Ž

1

+π‘Ž

2

=3+2=5

.

For the second testcase, π‘=[2,1,3,4]

 and π‘™=2

, π‘Ÿ=4

.

Note that the queries shown in the sample test are only for demonstration purposes, and they may not correspond to any optimal solution.

Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 1059
Previous Problem