Copying Data (Codeforces 292E)
Tags: Others, data structures

We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:  Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 ≤ q < k). The given operation is correct — both subsegments do not touch unexistent elements.  Determine the value in position x of array b, that is, find value bx. For each query of the second type print the result — the value of the corresponding element of array b.

Input: The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| ≤ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| ≤ 109).Next m lines contain the descriptions of the queries. The i-th line first contains integer ti — the type of the i-th query (1 ≤ ti ≤ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 ≤ xi, yi, ki ≤ n) — the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 ≤ xi ≤ n) — the position in array b.All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.

Output: For each second type query print the result on a single line.


Sample Inputs:
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2

Sample Outputs:
0
3
-1
3
2
3
-1

Solution (Python):
import sys
input = sys.stdin.readline
import bisect
if __name__ == '__main__':
    (n, m) = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    query = [[0, 1, 0]]
    for _ in range(m):
        f = list(map(int, input().split()))
        if f[0] == 2:
            i = f[1] - 1
            idx = bisect.bisect(query, [i, n + 1])
            (start, arr, shift) = query[idx - 1]
            print(b[i]) if arr == 1 else print(a[i + shift])
        elif f[0] == 1:
            i = f[2] - 1
            left = bisect.bisect(query, [i, 0])
            right = bisect.bisect(query, [i + f[3], n + 1])
            query[left:right] = [[i, 0, f[1] - f[2]], [i + f[3], query[right - 1][1], query[right - 1][2]]]

Solution (Python):
import sys
''

class SegmTree:
    """"""

    def __init__(self, size):
        N = 1
        while N < size:
            N <<= 1
        self.N = N
        self.tree = [0] * (2 * N)

    def modify_range(self, l, r, value):
        l += self.N
        r += self.N
        while l < r:
            if l & 1:
                self.tree[l] = value
                l += 1
            if r & 1:
                r -= 1
                self.tree[r] = value
            l >>= 1
            r >>= 1

    def query(self, i):
        i += self.N
        latest_change = self.tree[i]
        p = i
        while p > 1:
            p >>= 1
            latest_change = max(latest_change, self.tree[p])
        return latest_change
reader = (map(int, line.split()) for line in sys.stdin)
input = reader.__next__
(n, m) = input()
a = list(input())
b = list(input())
st = SegmTree(n)
request = [None] * (m + 1)
for i in range(1, m + 1):
    (t, *arg) = input()
    if t == 1:
        (x, y, k) = request[i] = arg
        st.modify_range(y - 1, y - 1 + k, i)
    else:
        pos = arg[0] - 1
        req_id = st.query(pos)
        if req_id > 0:
            (x, y, k) = request[req_id]
            ans = a[x + (pos - y)]
        else:
            ans = b[pos]
        sys.stdout.write(f'{ans}\n')

Solution (Python):
import sys
readline = sys.stdin.readline

class Dual_Segment_Tree:

    def __init__(self, N, f_act, e_act, operate, lst):
        self.N = N
        self.f_act = f_act
        self.e_act = e_act
        self.operate = operate
        self.lst = [None] * self.N
        for (i, x) in enumerate(lst):
            self.lst[i] = x
        self.segment_tree_act = [self.e_act] * (self.N + self.N)

    def __getitem__(self, i):
        if type(i) is int:
            if -self.N <= i < 0:
                i += self.N * 2
            elif 0 <= i < self.N:
                i += self.N
            else:
                raise IndexError('list index out of range')
            self.Propagate_Above(i)
            return self.Operate_At(i)
        else:
            (a, b, c) = (i.start, i.stop, i.step)
            if a == None or a < -self.N:
                a = 0
            elif self.N <= a:
                a = self.N
            elif a < 0:
                a += self.N
            if b == None or self.N <= b:
                b = self.N
            elif b < -self.N:
                b = 0
            elif b < 0:
                b += self.N
            return self.lst[slice(a, b, c)]

    def Operate_At(self, i):
        return self.operate(self.lst[i - self.N], self.segment_tree_act[i])

    def Propagate_At(self, i):
        self.segment_tree_act[i << 1] = self.f_act(self.segment_tree_act[i << 1], self.segment_tree_act[i])
        self.segment_tree_act[i << 1 | 1] = self.f_act(self.segment_tree_act[i << 1 | 1], self.segment_tree_act[i])
        self.segment_tree_act[i] = self.e_act

    def Propagate_Above(self, i):
        H = i.bit_length() - 1
        for h in range(H, 0, -1):
            self.Propagate_At(i >> h)

    def Operate_Range(self, a, L=None, R=None):
        if L == None or L < -self.N:
            L = self.N
        elif self.N <= L:
            L = self.N * 2
        elif L < 0:
            L += self.N * 2
        else:
            L += self.N
        if R == None or self.N <= R:
            R = self.N * 2
        elif R < -self.N:
            R = self.N
        elif R < 0:
            R += self.N * 2
        else:
            R += self.N
        L0 = L // (L & -L)
        R0 = R // (R & -R) - 1
        self.Propagate_Above(L0)
        self.Propagate_Above(R0)
        while L < R:
            if L & 1:
                self.segment_tree_act[L] = self.f_act(self.segment_tree_act[L], a)
                L += 1
            if R & 1:
                R -= 1
                self.segment_tree_act[R] = self.f_act(self.segment_tree_act[R], a)
            L >>= 1
            R >>= 1

    def Update(self):
        for i in range(1, self.N):
            self.Propagate_At(i)
            self.segment_tree_act[i] = self.e_act

    def __str__(self):
        self.Update()
        return '[' + ', '.join(map(str, [self.operate(x, a) for (x, a) in zip(self.lst, self.segment_tree_act[self.N:])])) + ']'
(N, M) = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

def f_act(a, b):
    if b == None:
        return a
    return b
e_act = None

def operate(x, a):
    if a == None:
        return x
    return a
DST = Dual_Segment_Tree(N, f_act, e_act, operate, [None] * N)
for _ in range(M):
    data = map(int, readline().split())
    if next(data) == 1:
        (x, y, k) = data
        x -= 1
        y -= 1
        DST.Operate_Range(x - y, y, y + k)
    else:
        x = next(data)
        x -= 1
        a = DST[x]
        if a == None:
            ans = B[x]
        else:
            ans = A[x + a]
        print(ans)