C - カードの重ね置き / Stacking Cards Editorial by admin
Qwen3-Coder-480BOverview
This is a problem where you manage XOR values while stacking cards, and for each check operation, find the most recent previous time the same XOR value appeared.
Analysis
In this problem, the “pile” has a stack structure, and cards are added and removed. The check operation computes the XOR of the entire pile at that point, but computing XOR over all cards every time would be inefficient.
The key insight is that “XOR is a reversible operation.” That is, XORing with a value \(x\) and then XORing with \(x\) again returns to the original value: $\( a \oplus x \oplus x = a \)$
Using this property, when adding a new card, we simply XOR the current XOR value with that card’s value, and when removing a card, we similarly XOR with that card’s value again to update.
Furthermore, to quickly determine whether the XOR value obtained during a check operation has appeared before, we can use an associative array (dict in Python) to record “XOR value → the number of the last check operation where it was seen.”
A naive approach (computing XOR over all elements in the stack every time) would result in a time complexity of \(O(N^2)\), which is too slow for the constraint \(N \leq 2 \times 10^5\).
Algorithm
- Manage the pile’s state with a stack
stack. - Maintain the current XOR value of the entire pile with a variable
xor_val. - Record “XOR value → most recent check operation number” in a dictionary
last_index. - Process each operation:
PUT c:- Push \(c\) onto the stack.
- Update the XOR value with
xor_val ^= c.
REMOVE:- Pop the top element from the stack.
- XOR it again to restore
xor_val.
LOOK:- Record the current
xor_val. - If it has appeared in the past, output that operation number; otherwise, output \(-1\).
- Update the dictionary to record the current operation number.
- Record the current
Complexity
- Time complexity: \(O(N)\)
- Each operation is processed in constant time, and lookups during check operations are \(O(1)\) using a hash table.
- Space complexity: \(O(N)\)
- The sizes of the stack and hash table are at most \(N\).
Implementation Notes
Efficiently manage the overall value by leveraging the reversibility of XOR.
Store past check results in a dictionary to speed up searching for matching values.
Use
sys.stdin.read()to read input quickly.Source Code
import sys
input = sys.stdin.read
def main():
data = input().split()
N = int(data[0])
stack = [] # 山の状態(スタック)
xor_val = 0 # 現在のXOR値
look_values = [] # これまでの確認操作でのXOR値を記録
result = [] # 結果を格納
index = 1
look_count = 0
# XOR値から直近のインデックスを取得するための辞書
last_index = dict()
for _ in range(N):
operation = data[index]
if operation == "PUT":
c = int(data[index+1])
stack.append(c)
xor_val ^= c
index += 2
elif operation == "REMOVE":
removed = stack.pop()
xor_val ^= removed
index += 1
else: # LOOK
look_count += 1
look_values.append(xor_val)
if xor_val in last_index:
result.append(last_index[xor_val])
else:
result.append(-1)
last_index[xor_val] = look_count
index += 1
print('\n'.join(map(str, result)))
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
posted:
last update: