This problem extends distance queries. You need to count how many marked nodes appear on the path from to . The path from to goes through their LCA. Split the problem: count marked nodes from to , plus marked nodes from to .
Subtract the LCA if it was counted twice. Think about how to preprocess the tree to answer "how many marked nodes are there from node up to the root?" in constant time.