33OJ
登录 注册

#P14420. [JOISC 2014] 历史的研究 / Historical Research

    ID: 16187 远端评测题 4000ms 512MiB 尝试: 9 已通过: 2 显示难度提高+/省选− 上传者: 标签>2014莫队JOISC/JOIST(日本)

[JOISC 2014] 历史的研究 / Historical Research

Problem Description

As a pioneer in the study of the history of the country of IOI, Professor Joy obtained a diary said to have been written by the inhabitants of the ancient country of IOI. Professor Joy plans to study the life of the ancient country of IOI by analyzing this diary, so he decides to investigate the events recorded in it.

The diary records the events that happened on each of NN consecutive days, and each event is classified into one of several types. The type of the event on day ii (1≤i≤N1 \le i \le N) is represented by the integer XiX_i; a larger value of XiX_i means a larger scale of the event.

Professor Joy decides to analyze the diary in the following way:

  1. Choose several consecutive days among the NN days of the diary as the analyzed interval.
  2. For an event type tt, its "importance" is defined as t×t \times (the number of events of type tt in the interval).
  3. Compute the importance of every event type and take the maximum.

You are asked by Professor Joy to write a program for the analysis. Given an analyzed interval, the program must find the maximum importance.

Problem

Given the event type of each of the NN days in the diary and QQ queries representing intervals of the diary, write a program that finds, for each query, the maximum importance of the events in the corresponding interval.

Input Format

Read the following data from standard input:

  • The first line contains two integers NN and QQ, separated by a space. This means that the diary lasts NN days and that QQ queries are given.
  • The next line contains NN integers X1,…,XNX_1, \dots, X_N, separated by spaces, where XiX_i (1≤i≤N1 \le i \le N) is the type of the event that happened on day ii.
  • In each of the following QQ lines, the jj-th line (1≤j≤Q1 \le j \le Q) contains two integers AjA_j and BjB_j (1≤Aj≤Bj≤N1 \le A_j \le B_j \le N), separated by a space, meaning that the interval of the jj-th query is from day AjA_j to day BjB_j.

Output Format

Output QQ lines to standard output. The jj-th line (1≤j≤Q1 \le j \le Q) contains one integer, the maximum importance of the events in the interval of the jj-th query.

5 5
9 8 7 8 9
1 2
3 4
4 4
1 4
2 4
9
8
8
16
16
8 4
9 9 19 9 9 15 9 19
1 4
4 6
3 5
5 8
27
18
19
19
12 15
15 9 3 15 9 3 3 8 16 9 3 17
2 7
2 5
2 2
1 12
4 12
3 6
11 12
1 7
2 6
3 5
3 10
7 10
1 4
4 8
4 8
18
18
9
30
18
15
17
30
18
15
18
16
30
15
15

Hint

Sample 1 Explanation

  • This diary lasts 55 days, and the event types recorded in the diary are only 77, 88 and 99.
  • From day 11 to day 22, the importance of event type 77 is 7×0=07 \times 0 = 0, the importance of event type 88 is 8×1=88 \times 1 = 8, and the importance of event type 99 is 9×1=99 \times 1 = 9, so the maximum of the three is 99.
  • From day 33 to day 44, the importance of event type 77 is 7×1=77 \times 1 = 7, the importance of event type 88 is 8×1=88 \times 1 = 8, and the importance of event type 99 is 9×0=09 \times 0 = 0, so the maximum of the three is 88.
  • On day 44, the importance of event type 77 is 7×0=07 \times 0 = 0, the importance of event type 88 is 8×1=88 \times 1 = 8, and the importance of event type 99 is 9×0=09 \times 0 = 0, so the maximum of the three is 88.
  • From day 11 to day 44, the importance of event type 77 is 7×1=77 \times 1 = 7, the importance of event type 88 is 8×2=168 \times 2 = 16, and the importance of event type 99 is 9×1=99 \times 1 = 9, so the maximum of the three is 1616.
  • From day 22 to day 44, the importance of event type 77 is 7×1=77 \times 1 = 7, the importance of event type 88 is 8×2=168 \times 2 = 16, and the importance of event type 99 is 9×0=09 \times 0 = 0, so the maximum of the three is 1616.

Constraints

All input data satisfy the following conditions:

  • 1≤N≤1051 \le N \le 10^5.
  • 1≤Q≤1051 \le Q \le 10^5.
  • 1≤Xi≤1091 \le X_i \le 10^9 (1≤i≤N1 \le i \le N).

Subtasks

Subtask 1 [55 points]

The following conditions are satisfied:

  • N≤100N \le 100.
  • Q≤100Q \le 100.

Subtask 2 [1010 points]

The following conditions are satisfied:

  • N≤5000N \le 5000.
  • Q≤5000Q \le 5000.

Subtask 3 [2525 points]

There are no i,ji, j satisfying Ai≤Aj≤Bj≤BiA_i \le A_j \le B_j \le B_i (where 1≤i≤Q1 \le i \le Q, 1≤j≤Q1 \le j \le Q and i≠ji \ne j).

Subtask 4 [6060 points]

No additional constraints.

Translated by QwenWork