#C4721. Erase Inconsistent Instructions

    ID: 48291 Type: Default 1000ms 256MiB

Erase Inconsistent Instructions

Erase Inconsistent Instructions

You are given two integers \(N\) and \(M\), where \(N\) represents the number of nodes and \(M\) the number of instructions. The following \(M\) lines of input each contain one instruction of one of the following two types:

  • Union Instruction: S a b. This means that the nodes in the interval \([a, b]\) should be merged sequentially using union operations (i.e., perform union on \(a\) and \(a+1\), union on \(a+1\) and \(a+2\), ..., union on \(b-1\) and \(b\)).
  • Color Assignment: C a c. This assigns a color c (represented as a string) to node \(a\). The color assignment applies to the entire connected component (established from the union operations), and for the instructions to be consistent, every node in the same component must have the same color.

If at any point a conflict in color assignments is detected for the same connected component, you may resolve the inconsistency by erasing one of the conflicting instructions. In addition, if a union instruction connects nodes that have already been assigned conflicting colors, this also creates an inconsistency, and one of the instructions must be removed.

Your task is to determine the minimum number of instructions that must be erased so that the remaining set of instructions is logically consistent.

inputFormat

The input begins with a single line containing two integers (N) and (M). Each of the following (M) lines contains one of the following instructions:

  • S a b : Merge nodes in the interval ([a, b]) (i.e. perform union on consecutive nodes).
  • C a c : Assign the color c to node (a) (and its connected component).

(a) and (b) are 1-indexed integers, and c is a string representing a color.

outputFormat

Output a single integer indicating the minimum number of instructions that must be erased to make the set of instructions free of logical inconsistencies.## sample

4 5
S 1 2
C 1 red
S 2 3
C 2 blue
S 3 4
1