Optional: combine and compare keys
Use composite keys, set intersections, and explicit ordering rules.
Two fields can identify one object
A grid point is identified by both coordinates. std::set<std::pair<int,int>> stores unique full pairs in lexicographic order: first coordinate first, then second as a tie-break. (2, 0) and (2, 1) remain distinct even though their first fields match.
Stored keys cannot be changed in place. Erase an old pair and insert a new one when its coordinates change. Optional std::greater<int> gives descending set order; then begin() is the greatest key and bound meanings follow that comparator, so the usual ascending “at least” wording cannot be copied unchanged.
Intersection, union, and difference
For distinct intersection, record the first list in a set. While reading the second, insert x into a result set only if the first set contains x. The result collapses duplicates and prints common keys in ascending order.
A union inserts both lists into one set. A difference keeps first-list keys absent from the second. These represent distinct values; a task about repeated occurrences needs a multiplicity-aware rule and representation.
// Complete C++23 program. Input: n m, n first-list keys, m second-list keys.
#include <iostream>
#include <set>
int main() {
int n, m;
std::cin >> n >> m;
std::set<int> first, common;
for (int i = 0; i < n; ++i) {
int x;
std::cin >> x;
first.insert(x);
}
for (int i = 0; i < m; ++i) {
int x;
std::cin >> x;
if (first.contains(x)) common.insert(x);
}
bool initial = true;
for (int x : common) {
if (!initial) std::cout << ' ';
std::cout << x;
initial = false;
}
std::cout << '\n';
}Account for both construction and output
The intersection program performs n insertions and m membership checks, plus insertions for matches. A simple bound is O((n + m)(1 + log(n + m + 1))). Storage includes the distinct first-list keys and the distinct common keys.
Empty input or no shared values naturally produces an empty line. Test duplicate-only inputs to verify that the problem truly asks for distinct intersection.
How many distinct points are in (2,1), (1,9), (2,1), (2,0)?
Turn the idea into code
For A = [2, 8, 2, 5] and B = [8, 8, 1, 2, 9], give the distinct intersection, union, and A minus B.
Give me a hint
Write each distinct collection before performing the requested membership rule.
Show the explained solution
Intersection: {2, 8}. Union: {1, 2, 5, 8, 9}. A minus B: {5}. Each appears once because the requested results are sets of distinct values, not lists of physical occurrences.
Your notes
Mark complete when you can explain it yourself.