CDQ

P3810 [Template] Three-dimensional partial order (flowers on Moshang) topic background This is a template question

You can use bitset, CDQ divide and conquer, K-DTree, etc. to solve the prob