infO(1)Cup 2025 Day 1 - Mirror | Cards

This was the problem page during the contest. Access the current page here.
Time limit: 2s Memory limit: 256MB Input: Output:

Little CS is playing a game which involves a deck of NN cards numbered from 11 to NN and an initially empty board. Each card has a positive integer value written on it, so that no two cards in the deck have the same value. The game is played as follows:

  1. Little CS reshuffles the cards so that they are in an order of his choice. Let p1p_1, p2p_2, \dots, pNp_N be the order of the cards in the deck after Little CS reshuffles them, where pip_i is the value written on the ii-th card;
  2. Little CS repeatedly picks the first card in the deck and has two options:
    • If the card is dominant, he then has to place it on the board. A card is considered dominant if either the board is empty or the value written on it is at least twice as big as the value written on any card on the board.
    • If the card is not dominant, he has to throw it away and cannot use it again.
  3. When the deck becomes empty, let SS be the sum of the values written on the cards on the board. Then, SS is the score of the game.

Task

Little CS is very competitive by nature, and desires that his score is as great as possible. In fact, he wonders what is the greatest score he could achieve. That is, what is the highest value of SS that could be obtained by choosing p1p_1, p2p_2, \dots, pNp_N Optimally. Little CS also wonders what is the number of orders p1p_1, p2p_2, \dots, pNp_N that lead to the highest value of SS.

This is too easy of a task for Little CS, so instead, he decided to make it harder and give it to you. Therefore, he has chosen NN cards with values a1,a2,,aya_1, a_2, \dots, a_y written on them, and QQ pairs of integers (l,r)(l, r) so that 1lrN1 \leq l \leq r \leq N, and for each one of them, he asks you to calculate the two values he wishes to find, if he would only play the game with the cards al,al+1,ara_l, a_{l+1}, \dots a_r.

Input data

The first line of the input contains NN, the number of cards, and QQ, the number of queries you are expected to answer. The second line contains a1a_1, a2a_2, \dots, aNa_N, the values written on Little CS' cards. Each of the following QQ lines contains two integers, the ii-th line containing the ii-th pair (li,ri)(l_i, r_i) that Little CS has chosen for you.

Output data

Each of the QQ lines of output must contain two space separated integers, representing the answer to Little CS' queries. More specifically, the ii-th line must contain the following values in the given order:

  • The maximum value of SS that could be obtained by playing only with the cards numbered from lil_i to rir_i;
  • The number of arrays p1p_1, p2p_2, \dots, prili+1p_{r_i-l_i+1} that lead to the maximum score. Since this number could be quite large, you should output it modulo 109+710^9+7.

Implementation details

You may use the following function while implementing your solution:

int product(int a, int b);

This will return, in constant time, the value a×(a+1)××(b1)×ba \times (a+1) \times \dots \times (b-1) \times b mod 109+710^9+7, i.e. the product of all integers between aa and bb modulo 109+710^9+7. Note that the parameters aa and bb need to satisfy 1ab200 0001 \leq a \leq b \leq 200 \ 000.

Remember to include the header cards.h, using the command #include "cards.h"! Note that you should still implement the main function.

Constraints and clarifications

  • 1N200 0001 \leq N \leq 200 \ 000;
  • 1Q50 0001 \leq Q \leq 50 \ 000;
  • 1ai500 0001 \leq a_i \leq 500 \ 000, for all 1iN1 \leq i \leq N, the elements of aia_i are pairwise distinct;
  • 1liriN1 \leq l_i \leq r_i \leq N, for all 1iQ1 \leq i \leq Q
  • 50%50\% of the points for each subtask are awarded if the maximum value of SS is correct for all queries, and the other 50%50\% are awarded if the number of arrays is correct for all queries. Please note that you still have to output the number of arrays for each query, even if they are not correct, in order to get the points for finding the values of SS.
# Points Restrictions
1 8 Q300Q \leq 300, rili7r_i-l_i \leq 7 for all 1iQ1 \leq i \leq Q
2 15 N,Q300N, Q \leq 300
3 9 N,Q5 000N, Q \leq 5 \ 000
4 10 a1<a2<<aN1<aNa_1 \lt a_2 \lt \dots \lt a_{N-1} \lt a_N
5 19 N5 000N \leq 5 \ 000
6 21 N,Q40 000N, Q \leq 40 \ 000
7 18 No further restrictions.

Example 1

stdin

6 2
4 1 7 12 5 21
1 4
4 6

stdout

17 2
26 1

Explanation

For each query, the numbers written on the cards that end up on the board are boxed.

For the first query, the following arrays lead to the maximal value of SS:

  1. 1,4,7,12\boxed{1, 4}, 7, \boxed{12}
  2. 1,4,12,7\boxed{1, 4, 12}, 7

For the second query, the following arrays lead to the maximal value of SS:

  1. 5,21,12\boxed{5, 21}, 12

Example 2

stdin

5 4
1 3 7 9 15
1 4
2 5
4 5
3 4

stdout

13 1
25 2
15 1
9 1

Example 3

stdin

10 6
115 4 319 19 28 1032 516 140 9 710
1 3
2 7
4 10
5 7
1 9
6 10

stdout

438 1
1580 8
1725 10
1576 1
1729 48
1697 2

Log in or sign up to be able to send submissions!