← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

const MAXV = 1000005

type Fenwick struct {
	tree []int32
}

func NewFenwick() *Fenwick {
	return &Fenwick{tree: make([]int32, MAXV+1)}
}

func (f *Fenwick) Add(i int, delta int32) {
	i++
	for ; i <= MAXV; i += i & -i {
		f.tree[i] += delta
	}
}

func (f *Fenwick) Query(i int) int32 {
	if i < 0 {
		return 0
	}
	if i >= MAXV {
		i = MAXV - 1
	}
	i++
	sum := int32(0)
	for ; i > 0; i -= i & -i {
		sum += f.tree[i]
	}
	return sum
}

func (f *Fenwick) Kth(k int32) int {
	if k <= 0 || k > f.Query(MAXV) {
		return -1
	}
	sum := int32(0)
	pos := 0
	for i := 19; i >= 0; i-- {
		next := pos + (1 << i)
		if next <= MAXV && sum+f.tree[next] < k {
			sum += f.tree[next]
			pos = next
		}
	}
	return pos
}

type FenwickInt64 struct {
	tree []int64
}

func NewFenwickInt64() *FenwickInt64 {
	return &FenwickInt64{tree: make([]int64, MAXV+1)}
}

func (f *FenwickInt64) Add(i int, delta int64) {
	i++
	for ; i <= MAXV; i += i & -i {
		f.tree[i] += delta
	}
}

func (f *FenwickInt64) Query(i int) int64 {
	if i < 0 {
		return 0
	}
	if i >= MAXV {
		i = MAXV - 1
	}
	i++
	sum := int64(0)
	for ; i > 0; i -= i & -i {
		sum += f.tree[i]
	}
	return sum
}

func evalT2(x int, k int, n int, m int, M int, f2Count *Fenwick, f2Sum *FenwickInt64) int64 {
	P := int64(x - k)
	var CP, SP int64
	if P <= 0 {
		CP = int64(m)
		SP = 0
	} else if P > MAXV {
		CP = 0
		SP = f2Sum.Query(MAXV)
	} else {
		lessCount := int64(f2Count.Query(int(P - 1)))
		CP = int64(m) - lessCount
		SP = f2Sum.Query(int(P - 1))
	}

	minMP := P
	if int64(M) < minMP {
		minMP = int64(M)
	}

	fP := int64(n)*minMP - SP - P*CP
	return fP
}

func evalT1(x int, k int, n int, m int, M int, f2Count *Fenwick, f2Sum *FenwickInt64) int64 {
	P := int64(x - k)
	fP := evalT2(x, k, n, m, M, f2Count, f2Sum)

	extra := P - int64(M)
	if extra < 0 {
		extra = 0
	}

	return extra + fP
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buf := make([]byte, 10*1024*1024)
	scanner.Buffer(buf, 10*1024*1024)

	nextInt := func() int {
		scanner.Scan()
		res := 0
		for _, b := range scanner.Bytes() {
			res = res*10 + int(b-'0')
		}
		return res
	}

	n := nextInt()
	m := nextInt()
	q := nextInt()

	a := make([]int, n+1)
	b := make([]int, m+1)

	f1Count := NewFenwick()
	f2Count := NewFenwick()
	f2Sum := NewFenwickInt64()

	var sumT1, sumT2 int64

	for i := 1; i <= n; i++ {
		a[i] = nextInt()
		f1Count.Add(a[i], 1)
		sumT1 += int64(a[i])
	}

	for i := 1; i <= m; i++ {
		b[i] = nextInt()
		f2Count.Add(b[i], 1)
		f2Sum.Add(b[i], int64(b[i]))
		sumT2 += int64(b[i])
	}

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	for i := 0; i < q; i++ {
		typ := nextInt()
		if typ == 1 {
			pos := nextInt()
			x := nextInt()
			oldVal := a[pos]
			f1Count.Add(oldVal, -1)
			sumT1 -= int64(oldVal)
			a[pos] = x
			f1Count.Add(x, 1)
			sumT1 += int64(x)
		} else if typ == 2 {
			pos := nextInt()
			x := nextInt()
			oldVal := b[pos]
			f2Count.Add(oldVal, -1)
			f2Sum.Add(oldVal, -int64(oldVal))
			sumT2 -= int64(oldVal)
			b[pos] = x
			f2Count.Add(x, 1)
			f2Sum.Add(x, int64(x))
			sumT2 += int64(x)
		} else if typ == 3 {
			k := nextInt()

			m1 := f1Count.Kth(1)
			m2 := f2Count.Kth(1)
			M := m1
			if m2 < M {
				M = m2
			}

			max1 := f1Count.Kth(f1Count.Query(MAXV))
			max2 := f2Count.Kth(f2Count.Query(MAXV))

			c3 := -1
			val3 := max2 + k
			if val3 > MAXV {
				val3 = MAXV
			}
			c3Count := f1Count.Query(val3)
			if c3Count > 0 {
				c3 = f1Count.Kth(c3Count)
			}

			c4 := -1
			val4 := max2 + k
			if val4 < 0 {
				val4 = 0
			}
			if val4 <= MAXV {
				c4Count := f1Count.Query(val4 - 1)
				totalF1 := f1Count.Query(MAXV)
				if c4Count < totalF1 {
					c4 = f1Count.Kth(c4Count + 1)
				}
			}

			candidates1 := []int{m1, max1, c3, c4}
			candidates2 := []int{m2, max2}

			var minG int64
			first := true

			for _, cand := range candidates1 {
				if cand != -1 {
					g := evalT1(cand, k, n, m, M, f2Count, f2Sum)
					if first || g < minG {
						minG = g
						first = false
					}
				}
			}

			for _, cand := range candidates2 {
				if cand != -1 {
					g := evalT2(cand, k, n, m, M, f2Count, f2Sum)
					if first || g < minG {
						minG = g
						first = false
					}
				}
			}

			ans := sumT1 - sumT2 - minG
			fmt.Fprintf(writer, "%d\n", ans)
		}
	}
}