Under construction

Still being built. Things may change

Team Formation

Time limit6sMemory limit1024 MB

Summary
For each query, find the largest (A1[i]+A2[j]) times (B1[i]+B2[j]) over a range of boys i and a range of girls j.
Level

Hard8 of 10

Topics
Geometry, Segment tree, Divide and conquer, Sorting
Solved
No attempts yet

Problem

In the 20XX International Informatics Olympiad, a mixed category was added. In this category, one boy and one girl form a team to solve problems. The olympiad committee selected NN boy candidates and MM girl candidates to form the national teams for the mixed category. The boy candidates are numbered from 00 to N−1N - 1, and the girl candidates are numbered from 00 to M−1M - 1.

A team's strength depends on both the ideation ability and the implementation ability of the two students on the team. Each ideation ability and implementation ability is an integer from 11 to 10910^9. The ideation ability of boy ii is A1[i]A_1[i], and his implementation ability is B1[i]B_1[i]. The ideation ability of girl jj is A2[j]A_2[j], and her implementation ability is B2[j]B_2[j]. If boy ii and girl jj form a team, the strength of that team is (A1[i]+A2[j])×(B1[i]+B2[j])(A_1[i] + A_2[j]) \times (B_1[i] + B_2[j]).

Every candidate was selected after fierce competition, so no boy is better than another boy in both ideation and implementation. More precisely, the ideation ability of the boys increases as the number grows, and the implementation ability decreases as the number grows. That is, A1[i]<A1[i+1]A_1[i] < A_1[i + 1] and B1[i]>B1[i+1]B_1[i] > B_1[i + 1] hold for every 0≤i≤N−20 \le i \le N - 2. The girls follow the same rule: A2[j]<A2[j+1]A_2[j] < A_2[j + 1] and B2[j]>B2[j+1]B_2[j] > B_2[j + 1] hold for every 0≤j≤M−20 \le j \le M - 2.

The committee wants to form a team with the maximum strength under several scenarios. There are QQ scenarios, numbered from 00 to Q−1Q - 1. In scenario kk, the boy's number must be at least L1[k]L_1[k] and at most R1[k]R_1[k], and the girl's number must be at least L2[k]L_2[k] and at most R2[k]R_2[k]. Write a program that finds, for each scenario, the maximum strength of a team that can be formed under these conditions.

Constraints

  • 1≤N≤100 0001 \le N \le 100\,000
  • 1≤M≤100 0001 \le M \le 100\,000
  • For all 0≤i≤N−10 \le i \le N - 1, 1≤A1[i],B1[i]≤1091 \le A_1[i], B_1[i] \le 10^9
  • For all 0≤j≤M−10 \le j \le M - 1, 1≤A2[j],B2[j]≤1091 \le A_2[j], B_2[j] \le 10^9
  • For all 0≤i≤N−20 \le i \le N - 2, A1[i]<A1[i+1]A_1[i] < A_1[i + 1] and B1[i]>B1[i+1]B_1[i] > B_1[i + 1]
  • For all 0≤j≤M−20 \le j \le M - 2, A2[j]<A2[j+1]A_2[j] < A_2[j + 1] and B2[j]>B2[j+1]B_2[j] > B_2[j + 1]
  • 1≤Q≤100 0001 \le Q \le 100\,000
  • For all 0≤k≤Q−10 \le k \le Q - 1, 0≤L1[k]≤R1[k]≤N−10 \le L_1[k] \le R_1[k] \le N - 1
  • For all 0≤k≤Q−10 \le k \le Q - 1, 0≤L2[k]≤R2[k]≤M−10 \le L_2[k] \le R_2[k] \le M - 1

Examples1

  1. Example 1

    Input
    1 1
    1 1
    1 1
    1
    0 0 0 0
    
    Expected output
    4