PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000208. Truy vấn cộng và nhân (Add and Multiply Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn cộng và nhân (Add and Multiply Queries)

Add and Multiply Queries

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given two arrays of positive integers A=(a1,…,an)A=(a_1,\ldots,a_n) and B=(b1,…,bn)B=(b_1,\ldots,b_n), both indexed from 11, process qq operations in order:

  • 1 i x: replace aia_i with xx.
  • 2 i x: replace bib_i with xx.
  • 3 l r: start with v=0v=0. Process the indices i=l,l+1,…,ri=l,l+1,\ldots,r in order. At each index choose exactly one update: v←v+aiv\leftarrow v+a_i or v←v⋅biv\leftarrow v\cdot b_i. Print the largest possible final value after processing index rr.

Type 3 queries do not modify either array, and the choices made within one query do not persist into other queries.

Input

  • The first line contains nn.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n.
  • The third line contains nn integers b1,…,bnb_1,\ldots,b_n.
  • The fourth line contains qq.
  • Each of the next qq lines is one of 1 i x, 2 i x, or 3 l r, as defined in the statement.

Output

For each type 3 operation, print one line containing its maximum attainable value, in query order.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤121\le n,q\le12; 1≤ai,bi≤1091\le a_i,b_i\le10^9; types 1,2: 1≤i≤n1\le i\le n, 1≤x≤1091\le x\le10^9; type 3: 1≤l≤r≤n1\le l\le r\le n; every type 3 answer is at most 101810^{18}.

  • Subtask 2 (30 points): 1≤n,q≤20001\le n,q\le2000; 1≤ai,bi≤1091\le a_i,b_i\le10^9; types 1,2: 1≤i≤n1\le i\le n, 1≤x≤1091\le x\le10^9; type 3: 1≤l≤r≤n1\le l\le r\le n; every type 3 answer is at most 101810^{18}.

  • Subtask 3 (50 points): 1≤n,q≤1051\le n,q\le10^5; 1≤ai,bi≤1091\le a_i,b_i\le10^9; types 1,2: 1≤i≤n1\le i\le n, 1≤x≤1091\le x\le10^9; type 3: 1≤l≤r≤n1\le l\le r\le n; every type 3 answer is at most 101810^{18}.

Examples

Input

3
3 2 4
1 2 2
3
3 1 3
1 1 1
3 1 3

Output

12
7

Explanation

Initially A=[3,2,4]A=[3,2,4] and B=[1,2,2]B=[1,2,2]. For the first query 3 1 3, choose addition at index 11 to obtain v=3v=3, then multiplication by 22 at index 22 to obtain 66, and multiplication by 22 at index 33 to obtain 1212. These choices maximize the result. The update 1 1 1 changes AA to [1,2,4][1,2,4]. In the last query, additions at all three positions yield 11, then 33, then 77; multiplication at position 22 would yield 22 and at position 33 would yield 66, both smaller. Thus the output lines are 12 and 7.