This solution reads an initial integer set A, applies N in‑place mutation operations—update, intersection_update, difference_update, symmetric_difference_update—each driven by another set B, and finally prints the sum of the mutated A. The long‑form script uses only ASCII characters, straightforward input parsing, and an explicit for‑loop with if/elif dispatch to each set method. The compact one‑liner leverages getattr and a list comprehension for succinct dynamic dispatch. Both variants produce identical results; use the script for clarity and the one‑liner for brevity.
#Sets #InPlaceMutation #PythonIO #Sum #OneLiner #getattr #map #input
Codes and Explanation:
Long Version (ASCII‑only)
Read and ignore the reported size of A
_ = int(input().strip())
Build the initial set A from the next line
A = set(map(int, input().split()))
Read the number of mutation operations
n = int(input().strip())
Apply each mutation in place
for _ in range(n):
parts = input().split()
op = parts[0] # operation name
parts[1] is the reported size of the next set, ignore it
B = set(map(int, input().split()))
if op == "update":
A.update(B)
elif op == "intersection_update":
A.intersection_update(B)
elif op == "difference_update":
A.difference_update(B)
elif op == "symmetric_difference_update":
A.symmetric_difference_update(B)
Print the sum of all elements in the final set A
print(sum(A))
Step‑by‑Step Explanation:
Parse A: Skip its size, then read its elements into a set for O(1) membership and in‑place methods.
Parse N: The count of subsequent mutations.
Loop Mutations: Each iteration reads an operation name and builds the auxiliary set B. A simple if/elif selects and invokes the correct in‑place method on A, modifying it directly.
Output: Finally, sum(A) aggregates the mutated set in linear time and is printed.
Compact One‑Liner
_ = input().strip()
A = set(map(int, input().split()))
n = int(input().strip())
[ getattr(A, op)(set(map(int, input().split())))
for _ in range(n)
for op, _ in [input().split()] ]
print(sum(A))
One‑Liner Breakdown:
Build A: Read & parse.
Read n: Number of ops.
Mutate: A nested comprehension runs n times, each time reading op and the next line, then calls getattr(A, op) with B.
Sum: Print the final sum in one statement.
Both the long version and the one‑liner adhere strictly to ASCII, require no extra libraries, and complete in O(N · M) time where M is the average size of the auxiliary sets.