Mex

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

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
3
0 1 3
5
1 0 2 0 1
0
 
Sample Output
5
24

Hint

For the first test case:
mex(1,1)=1, mex(1,2)=2, mex(1,3)=2, mex(2,2)=0, mex(2,3)=0,mex(3,3)=0.
1 + 2 + 2 + 0 +0 +0 = 5.

 
Source
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 

 
 
 
题意:Mex(l,r) 求的是[l,r]区间内所有数的集合里没有出现过的最小的数字,即博弈里的mex。现在给出一个序列[1,n],求解所有$1 \le l \le r\le n$ 的[l,r]中的mex[l,r]的和。
 
 
 
  首先我们将区间右端点统一为k,用num[k]存k这个位置对应的数字。即我们处理到k这个端点时,我们处理的是所有$[l,k] (1 \le l \le k)$的mex。此时我们只需要for一遍然后对应处理k就行。
  那怎么加速k这个端点的处理呢? 我们用t[l] 表示[l,k] 中所有数的集合set从零开始连续的最大的数,即mex-1,那么假如我们的序列为6 0 3 2 0 1 0 ,处理到最后一个数k=7,对应的t为 3 3 3 2 1 1 0,可以看出t为一个非递减序列,因此我们用aft[o]存t中数字o连续序列的最后一个位置,例如aft[1]=6。
  我们可以将这个问题化为区间右端点扩张的问题,每次更新对应一些区间的右端点扩张。我们还用last[o]存数字o最后出现的位置。可以看出我们每到一个新端点k,它对应的数字为knum,影响的t区间为大于等于knum的数字所在的区间,准确的说是s大于等于knum并且aft[s]==aft[knum]的数字所在的区间,因为一旦aft[s]<aft[knum]那么限制他的右端点扩张的数字就不是knum了,而是大于knum的数字。所以我们需要更新这些数字所在区间右端点,并且从小到大更新,设更新的区间右端点最大值为maxn。由于这些数字区间右端点还受aft[knum-1]限制,初始maxn=aft[knum-1]。然后每个数字s的右端点最大值为min(aft[knum-1],last[knum],last[knum+1]......last[s])(最左端的端点限制扩张)。至此我们快速的更新t区间。另外我们用all存现在l∈[1,k] 所有[l,k]mex的和,那么每次更新右端点就需要给all加上更新的区间的长度作为扩张对mex的贡献。最后我们把所有的all加起来就是答案。
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define clr(x) memset(x,0,sizeof(x))
#define LL long long
using namespace std;
int a[],aft[],last[];
LL ans,all;
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int n,m,k,dk,maxn;
while(scanf("%d",&n)== && n!=)
{
ans=;
all=;
clr(last);
clr(aft);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]>=n)
{
ans+=all;
continue;
}
last[a[i]]=i;
if(a[i]>)
{
maxn=aft[a[i]-];
}
else
{
maxn=i;
}
k=a[i];
dk=aft[k];
while(aft[k]==aft[k+])
{
maxn=min(maxn,last[k]);
if(maxn==dk)
break;
all+=(LL)(maxn-aft[k]);
aft[k]=maxn;
k++;
}
maxn=min(maxn,last[k]);
all+=(LL)(maxn-aft[k]);
aft[k]=maxn;
ans+=all;
}
printf("%lld\n",ans);
}
return ;
}
 

hdu 4747 Mex( 线段树? 不,区间处理就行(dp?))的更多相关文章

  1. hdu 4747 mex 线段树+思维

    http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...

  2. HDU 4747 Mex ( 线段树好题 + 思路 )

    参考:http://www.cnblogs.com/oyking/p/3323306.html 相当不错的思路,膜拜之~ 个人理解改日补充. #include <cstdio> #incl ...

  3. Can you answer these queries? HDU - 4027 (线段树,区间开平方,区间求和)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  4. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  5. HDU 1698 【线段树,区间修改 + 维护区间和】

    题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...

  6. HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  1e5个点,问 ...

  8. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  9. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. PHP 练习3:租房子

    一.题目要求 二.题目做法 1.建立数据库 2.封装类文件 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 pu ...

  2. UIImagePickerController---iOS-Apple苹果官方文档翻译

    //本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址  //转载请注明出处--本文永久链接:http://www.cnblogs.com ...

  3. 超详细的Java面试题总结(二)之Java基础知识篇

    多线程和Java虚拟机 创建线程有几种不同的方式?你喜欢哪一种?为什么? 继承Thread类 实现Runnable接口 应用程序可以使用Executor框架来创建线程池 实现Callable接口. 我 ...

  4. Piggy-Bank(多重背包+一维和二维通过方式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题面: Problem Description Before ACM can do anythi ...

  5. flask函数已定义参数却出现takes 0 positional arguments but 1 was given的问题

    在flask中定义了一个简单的删除数据库内容的路由 测试却发现一直报错 说delete_history函数定义时没有接受参数,但是检查delete_history函数却发现没有问题 后来想了半天才发现 ...

  6. CSS(Cascading Style Shee)

    1.CSS是Cascading Style Sheet这个几个英文单词的缩写,翻译成中文是“层叠样式表”的意思 CSS能让网页制作者有效的定制.改善网页的效果. CSS是对HTML的补充,网页设计师曾 ...

  7. hdu 1253 胜利大逃亡(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目大意:在所给的时间能顺利离开城堡. #include <iostream> #i ...

  8. [NOI2008] [bzoj1061] 志愿者招募

    还是一道费用流的题目.话不多说,进入正题. 题意:给定n个点和m种从l到r覆盖一层的费用,求满足所有点的覆盖层数都大等于权值的最小费用 分析:要做到区间修改,看似比较麻烦. 用差分把区间修改变成单点修 ...

  9. socket 编程 TCP 实现简单聊天功能【转】

    转自:http://blog.csdn.net/liujia2100/article/details/9006479 版权声明:本文为博主原创文章,未经博主允许不得转载. 各个主要函数的功能: .so ...

  10. bzoj 1191 超级英雄Hero

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1191 题解: 裸匈牙利,注意如果出现找不到增广路的情况就直接break #include& ...