14641 - Career & Recruitment Fair II   

Description

Imagine you've arrived at the annual NTHU Career & Recruitment Fair, where along a long corridor there are N drink booths arranged in a line from the entrance to the exit. Due to foot-traffic and location, prices vary significantly.

Booths near the two ends (entrance and exit) typically have higher visibility and can charge higher prices.

Mid-corridor booths generally have less concentrated traffic, leading to potentially lower prices.

Each type of drink appears at least three times, and booths selling the same drink may charge identical prices.

You wish to evaluate each drink’s popularity score and determine the number of distinct booth combinations that achieve this score, based on their price distribution as you walk by.

Suppose a drink appears at booths 1 ≤ i1 < i2 < ⋯ < im ≤ N with corresponding prices p1 ​, p2 ​, … , pm.

Define:

  • The popularity score Smax(pu ​- pv ​+ pw)​ , for all u < v < w 

  • The number of distinct triples reaching this maximum score C = (the number of (u,v,w) such that pu ​- pv ​+ p= S), counted modulo 109+7.

Intuitively, you select an early booth at price pu, a middle booth at price pv, and a later booth at price pw; higher outer prices combined with a lower middle price yield a higher popularity score.

Note: This formula does not necessarily imply mid-corridor booths are always cheaper, but it helps capture a general trend in drink popularity.

Input

The first line contains a single integer N, the total number of drink booths arranged in a line at the NTHU Career & Recruitment Fair.

The second line contains N space-separated strings s1,s2,…,sN , where si​ is the name of the drink sold at booth i.

The third line contains N space-separated integers a1,a2,…,aN , where ai​ is the price charged by booth i.

The fourth line contains an integer Q, the number of queries you will make, and then Q more lines each containing a single string Tj​ ; each Tj​​ is guaranteed to match at least one of the si.

Constraints

  • 1 ≤ N,Q ≤ 2×105

  • 1 ≤ | s|,| T| ≤ 10

  • 1 ≤ ai ≤ 108

  • Every drink appears at least three times.

  • Every queried drink Tj matches at least one of the drinks si.

Subtasks

  • Testcases 1 ~ 2: si ,Tj   = "a", no duplicate prices for the same drink.(The number of triples C is always equal to 1)

  • Testcases 3 ~ 4: N,Q ≤ 1000, no duplicate prices for the same drink.(The number of triples C is always equal to 1)

  • Testcases 5 ~ 6: No duplicate prices for the same drink.(The number of triples C is always equal to 1)

  • Testcases 7 ~ 8: The number of triples C ≤ 10.

  • Testcases 9 ~ 10: No additional restrictions.

Hints

  1. Use a map<string, vector<int>> to collect, for each drink s, the list of its prices in the exact order they appear. That way, each query can immediately retrieve the price-sequence for its drink.
  2. Basic multiset usage
    • ​​multiset allows multiple elements with equivalent values, whereas a set enforces uniqueness.
    • Internally, both are typically implemented as balanced binary trees, so most operations have O(log n) complexity.
    • find(val) returns an iterator to one matching element (not all of them).
    • ms.erase(val) removes all elements equal to val.

    • To remove only a single occurrence, use auto it = ms.find(val); if (it != ms.end()) ms.erase(it)

  3. You want the largest pu before v and the largest pw after v.
  4. Initialize a multiset suffix containing all prices for this drink and a parallel map cntSuffix that records, for every price x, how many times x currently appears in suffix.
  5. Keep an empty multiset prefix and an empty map cntPrefix; they will hold prices (and their multiplicities) that lie to the left of the current position.
  6. As you scan each price pv in order:
    • Remove pv from suffix, decrement cntSuffix[p[v]].
    • If both prefix and suffix are nonempty, update the best value best = max(best,prefix.rbegin()+suffix.rbegin() − pv).
    • Accumulate the number of ways by multiplying the occurrences of L and R: ways += cntPrefix[L] × cntSuffix[R] (mod 109+7).
    • Insert pv into prefix and increment cntPrefix[p[v]].
    • If the same drink is queried more than once, store its computed score in a map<string, vector<int>> ans to avoid repeated computation.
  7. Cache the pair (best, ways) in map<string, pair<ll,ll>> ans so that a repeated query for the same drink returns in O(log n) time without recomputation.
  8. Each drink’s price-vector is scanned once, and each insertion/erase into a balanced set costs O(log⁡ m). Overall you get O(nlog⁡n + qlog⁡n), which is fast enough up to 2×105.

Output

For each query Tj, output a single line containing two integers:

  • S(Tj): the maximum popularity score.

  • C(Tj): the number of triples that achieve this score, modulo 109+7.

Sample Input  Download

Sample Output  Download

Tags




Discuss