hdu4747(线段树区间更新)

来源:互联网 发布:led走字屏改字软件 编辑:程序博客网 时间:2024/06/06 02:15

Mex

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1892    Accepted Submission(s): 625


Problem Description
Mex is a function on a set of integers, which is universally used for impartial game theorem. For a non-negative integer set S, mex(S) is defined as the least non-negative integer which is not appeared in S. Now our problem is about mex function on a sequence.

Consider a sequence of non-negative integers {ai}, we define mex(L,R) as the least non-negative integer which is not appeared in the continuous subsequence from aL to aR, inclusive. Now we want to calculate the sum of mex(L,R) for all 1 <= L <= R <= n.
 

Input
The input contains at most 20 test cases.
For each test case, the first line contains one integer n, denoting the length of sequence.
The next line contains n non-integers separated by space, denoting the sequence.
(1 <= n <= 200000, 0 <= ai <= 10^9)
The input ends with n = 0.
 

Output
For each test case, output one line containing a integer denoting the answer.
 

Sample Input
30 1 351 0 2 0 10
 

Sample Output
524

题意:求所有区间mex的总和,定义mex[i,j]为区间[i,j]没有出现过的最小的数

思路:比较好的线段树题

            先预处理出mex[1,i] (1=<i<=n)

            然后预处理出每个位置 i 的下一个位置 next[i] ,假设现在位置的数为x,那么规定它的下一个位置为它后面离它最近的x的位置

            然后 i 从1-n开始搞,每次删除i位置的数,然后在[ i+1,next[i]-1 ]这些位置找到满足位置p,且p位置的mex[i,p]值>value[i]

            那么后面到next[i]-1的所有区间的mex都是大于value[i]的,因为mex满足单调性

            然后就将mex[ p,next[i]-1 ]区间的值修改为value[i]

            最后区间[i+1,n]求和就是以i+1为左端点,i+2~n为右端点的所有区间的mex和

            涉及到的操作,区间更新,区间求和,区间求最大 

0 0