hdu 1540 Tunnel Warfare(线段树单点更新+区间合并)

Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.

Output

Output the answer to each of the Army commanders’ request in order on a separate line.

Sample Input

Sample Output


很类似于GSS1,然后需要维护的信息也比较少,所以也按照GSS1的查询写法写了一下。

维护pre代表当前区间左端连续的村庄个数,suf代表当前区间右端连续的村庄个数。更新的时候直接更新到叶子即可。然后查询的时候,我是这么捉的:查询1-x这个区间从右端点开始连续的村庄个数有多少,然后查询x-n这个区间从左端点开始连续的村庄个数有多少。(一开始是查询的x+1-n但这样是不对的,比如当x已经被摧毁x+1没被摧毁的情况就不对)。然后取和。如果和大于0就需要减1.

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注