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

#FT0000213. Xóa lần lượt các trung vị (Remove Median Operations)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xóa lần lượt các trung vị (Remove Median Operations)

Remove Median Operations

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two sequences of positive integers, A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N) and B=(B1,B2,…,BM)B=(B_1,B_2,\ldots,B_M). The length NN of AA is even. Both sequences are indexed from 11.

Process QQ assignment queries in order:

  • 1 i x: Replace AiA_i by xx.
  • 2 i x: Replace BiB_i by xx.

Immediately after each assignment, perform the following experiment on the current sequences, starting from scratch for that query:

  1. Initialize a multiset XX with the NN elements of AA, retaining duplicate values.
  2. For j=1,2,…,Mj=1,2,\ldots,M, in this order, insert BjB_j into XX, then remove exactly one median element of XX.
  3. After processing all MM elements of BB, output the sum of the NN elements remaining in XX.

Before each removal in step 2, XX has N+1N+1 elements. As NN is even, N+1N+1 is odd; the median is the element in position N/2+1N/2+1 in nondecreasing order. If multiple elements equal this median, remove only one occurrence.

Print the required sum after each assignment query in the original order.

Input

  • The first line contains N,M,QN,M,Q: the length of AA, the length of BB, and the number of assignment queries.
  • The second line contains exactly NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N.
  • The third line contains exactly MM integers B1,B2,…,BMB_1,B_2,\ldots,B_M.
  • Each of the next QQ lines contains t i x. If t=1t=1, set Ai=xA_i=x; if t=2t=2, set Bi=xB_i=x. Index ii is 11-based within the specified sequence.

Output

Print exactly QQ lines. Line jj contains the sum of the multiset XX after processing assignment jj and then performing all MM insertion-and-median-removal steps.

Subtasks

  • Subtask 1 (20 points): 2≤N≤402\le N\le 40 and NN is even; 1≤M≤301\le M\le 30; 1≤Q≤401\le Q\le 40; 1≤Ai,Bj≤1091\le A_i,B_j\le 10^9 for every valid position; every operation has t∈{1,2}t\in\{1,2\} and 1≤x≤1091\le x\le 10^9; for t=1t=1, 1≤i≤N1\le i\le N; for t=2t=2, 1≤i≤M1\le i\le M.
  • Subtask 2 (30 points): 2≤N≤20002\le N\le 2000 and NN is even; 1≤M≤20001\le M\le 2000; 1≤Q≤20001\le Q\le 2000; 1≤Ai,Bj≤1091\le A_i,B_j\le 10^9 for every valid position; every operation has t∈{1,2}t\in\{1,2\} and 1≤x≤1091\le x\le 10^9; for t=1t=1, 1≤i≤N1\le i\le N; for t=2t=2, 1≤i≤M1\le i\le M.
  • Subtask 3 (50 points): 2≤N≤2000002\le N\le 200000 and NN is even; 1≤M≤2000001\le M\le 200000; 1≤Q≤2000001\le Q\le 200000; 1≤Ai,Bj≤1091\le A_i,B_j\le 10^9 for every valid position; every operation has t∈{1,2}t\in\{1,2\} and 1≤x≤1091\le x\le 10^9; for t=1t=1, 1≤i≤N1\le i\le N; for t=2t=2, 1≤i≤M1\le i\le M.

Examples

Input

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

Output

10
13

Explanation

After the first assignment 1 3 3, we have A=(5,1,3,3)A=(5,1,3,3) and B=(1,2)B=(1,2). Initially X={1,3,3,5}X=\{1,3,3,5\}. Inserting B1=1B_1=1 produces {1,1,3,3,5}\{1,1,3,3,5\}. Its third element is the median 33; removing one copy gives {1,1,3,5}\{1,1,3,5\}. Inserting B2=2B_2=2 produces {1,1,2,3,5}\{1,1,2,3,5\}; remove the median 22 to obtain {1,1,3,5}\{1,1,3,5\}. The sum is 1+1+3+5=101+1+3+5=10.

The second assignment 2 1 5 changes BB to (5,2)(5,2), leaving AA unchanged. Starting again at X={1,3,3,5}X=\{1,3,3,5\}, insert 55 and remove median 33 to get {1,3,5,5}\{1,3,5,5\}. Insert 22 to get {1,2,3,5,5}\{1,2,3,5,5\}; remove median 33, leaving {1,2,5,5}\{1,2,5,5\}. The final sum is 1+2+5+5=131+2+5+5=13. These two results are printed on separate lines.