Approach
Sorting and greedy selection
For ABC346 E — Paint, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 61 lines of Python from the credited upstream file abc346_e.py.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def main():5 import sys6 from collections import defaultdict7 8 input = sys.stdin.readline9 10 h, w, m = map(int, input().split())11 tax = list()12 13 for i in range(m):14 tax.append(list(map(int, input().split())))15 16 colors = defaultdict(int)17 remain_h, remain_w = h, w18 used_h, used_w = [False] * h, [False] * w19 20 21 22 for i in range(m - 1, -1, -1):23 ti, ai, xi = tax[i]24 ai -= 125 26 if ti == 1:27 if used_h[ai]:28 continue29 30 used_h[ai] = True31 32 colors[xi] += remain_w33 remain_h -= 134 else:35 if used_w[ai]:36 continue37 38 used_w[ai] = True39 40 colors[xi] += remain_h41 remain_w -= 142 43 44 colors[0] += remain_h * remain_w45 ans = list()46 47 for key in sorted(colors.keys()):48 count = colors[key]49 50 if count > 0:51 ans.append((key, count))52 53 print(len(ans))54 55 for key, count in ans:56 print(key, count)57 58 59if __name__ == "__main__":60 main()61