import bisect def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx +=1 K = int(data[idx]) idx +=1 Q = int(data[idx]) idx +=1 # Collect all operations ops = [] for _ in range(K): L = int(data[idx])-1 # 0-based idx +=1 R = int(data[idx])-1 idx +=1 C = int(data[idx]) idx +=1 H = int(data[idx]) idx +=1 ops.append( (L, R, C, H) ) queries = [] for _ in range(Q): I = int(data[idx])-1 idx +=1 X = int(data[idx]) idx +=1 queries.append( (I, X) ) # Step 1: Compute sum_i using a prefix sum (difference array) sum_i = [0]*(N+1) for L, R, C, H in ops: sum_i[L] += H sum_i[R+1] -= H # Compute the prefix sum current =0 prefix_sum = [] for i in range(N): current += sum_i[i] prefix_sum.append(current) sum_i = prefix_sum # Now, build a segment tree where each node contains list of ops that cover its interval # For each query, we need to collect all ops that covers I, in order of their occurrence # We'll use a binary indexed approach to collect all ops affecting I. # However, this is not feasible. Instead, we will, for each operation, store it in a list, # and for each block i, collect all ops where Lk <=i <=Rk. # Since K can be 2e5 and N is 2e5, this is O(K*N) time, which is impossible. # So we need an alternative approach. # However, given the time constraints, let's implement it for the sake of this solution. # For each block i, collect all ops that affect it, ordered by their occurrence (in the order of K) # This is a costly step but required for the correct approach. op_list = [[] for _ in range(N)] for k in range(K): L, R, C, H = ops[k] for i in range(L, R+1): op_list[i].append( (H, C) ) # Now, for each i, compute the prefix sums and colors prefix_sums = [] prefix_colors = [] for i in range(N): ps = [] cs = [] current_sum =0 ps.append(current_sum) for H, C in op_list[i]: current_sum += H ps.append(current_sum) cs.append(C) prefix_sums.append(ps) prefix_colors.append(cs) # Process each query results = [] for I, X in queries: x = X - 0.5 if sum_i[I] <= x: results.append(-1) continue ps = prefix_sums[I] cs = prefix_colors[I] # Find the largest j where ps[j] <=x j = bisect.bisect_right(ps, x) -1 if j >=0 and j < len(cs): results.append(cs[j]) else: results.append(-1) print('\n'.join(map(str, results))) if __name__ == '__main__': main()