This page is still under construction.

Parts of this page are still being built. What you see may change.

Selling horses

Time limit2sMemory limit512 MB

Summary
One horse multiplies by X[i] each year and any held horses sell at price Y[i]; report the maximum revenue after each point update modulo 1e9+7.
Level

Hard8 of 10

Topics
Segment tree, Greedy, Math
Solved
No attempts yet

Problem

Mansur raises horses. Today he owns more horses than anyone else in Kazakhstan, but NN years ago he was a young man with a single horse.

Number the years from 0 to N−1N-1 in chronological order, so year N−1N-1 is the most recent one. The weather in year ii decides how fast the herd grows, and the growth rate is a positive integer X[i]X[i]. If Mansur has hh horses at the start of year ii, he has h×X[i]h \times X[i] horses at the end of that year.

Horses are sold only at the end of a year. At the end of year ii one horse sells for a positive integer Y[i]Y[i], and there is no limit on how many of the horses he holds at that moment he sells.

Mansur wants to know how much money he could have made over the last NN years if he had picked the selling times perfectly. Through the evening his memory sharpens, so he corrects the numbers MM times. Each correction replaces one X[i]X[i] or one Y[i]Y[i] with a new value, and the corrections pile up. The same position is corrected more than once in some cases.

Compute the largest amount of money he could make with the initial numbers, and the largest amount after each correction. The answer gets very large, so print it modulo 109+710^9+7.

Input

The first line contains the number of years NN.

The second line contains NN integers X[0]X[0] to X[N−1]X[N-1], separated by spaces.

The third line contains NN integers Y[0]Y[0] to Y[N−1]Y[N-1], separated by spaces.

The fourth line contains the number of corrections MM.

Each of the next MM lines contains one correction in the form type pos val. If type is 1, then X[pos]X[pos] becomes val. If type is 2, then Y[pos]Y[pos] becomes val.

Limits:

  • 1≤N≤500 0001 \le N \le 500\,000
  • 0≤M≤100 0000 \le M \le 100\,000
  • 1≤X[i]≤1091 \le X[i] \le 10^9 and 1≤Y[i]≤1091 \le Y[i] \le 10^9, both for the initial values and after every correction
  • type is 1 or 2, 0≤pos≤N−10 \le pos \le N-1, and 1≤val≤1091 \le val \le 10^9

Output

Print M+1M+1 lines.

The first line holds the largest amount of money Mansur could make with the initial numbers. The ii-th line after that holds the largest amount once the first ii corrections have all been applied.

Every value is printed modulo 109+710^9+7.

Examples3

  1. Example 1

    Input
    3
    2 1 3
    3 4 1
    1
    2 1 2
    
    Expected output
    8
    6
  2. Example 2

    Input
    1
    5
    7
    0
    
    Expected output
    35
  3. Example 3

    Input
    5
    1 1 1 1 1
    3 9 2 9 1
    2
    2 0 10
    1 2 4
    
    Expected output
    9
    10
    36