Mex

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

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
/*
hdu 4747 线段树 表示开始毫无头绪,总觉得和线段树扯不上什么关系- - 弱TAT 我们要求的是mex[i,j](i~j中不存在的最小非负整数)的和,观察可以发现对于1~n,
mex[1,i]是递增的,因为你当前mex值可以在后面出现 然后假设去掉a[1],可以发现在a[1]再次出现之前.mex值大于a[1]的都会变成a[1]
1 0 2 0 1 -> 0 2 3 3 3
去掉a[1] -> 1 1 1 3 然后按照这个思路弄即可,先处理出mex[1,i]的情况并插入线段树,然后处理出a[i]下次
出现的位置。 利用线段树可以求出在a[i]再次出现之前比a[i]大的最小位置,把这段
全部置为a[i](毕竟这个序列是递增的),并能快速求出和. hhh-2016-03-24 18:15:48
*/
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn = 500550;
struct node
{
int l,r;
ll num;
int Max,add;
int mid()
{
return ((l+r)>>1);
};
} tree[maxn<<2]; int a[maxn],nex[maxn],mex[maxn];
map<int,int> mp; void update_up(int i)
{
tree[i].num = tree[lson].num+tree[rson].num;
tree[i].Max = max(tree[lson].Max,tree[rson].Max);
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].add = 0;
if(l == r)
{
tree[i].num = mex[l];
tree[i].Max = mex[l];
return ;
}
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
update_up(i);
} void update_down(int i)
{
if(tree[i].add)
{
tree[lson].add = 1;
tree[rson].add = 1;
tree[lson].num = (ll)tree[i].Max*(tree[lson].r-tree[lson].l+1);
tree[rson].num = (ll)tree[i].Max*(tree[rson].r-tree[rson].l+1);
tree[lson].Max= tree[i].Max;
tree[rson].Max= tree[i].Max;
tree[i].add = 0;
}
}
void Insert(int i,int l,int r,int val)
{
if(tree[i].l >= l && r >= tree[i].r)
{
tree[i].num = (ll)(tree[i].r-tree[i].l+1)*val;
tree[i].Max = val;
tree[i].add = 1;
return;
}
update_down(i);
int mid = tree[i].mid();
if(l <= mid)
Insert(lson,l,r,val);
if(r > mid)
Insert(rson,l,r,val);
update_up(i);
}
int cur;
void get_k(int i,int k)
{
if(tree[i].l == tree[i].r)
{
cur = tree[i].l;
return ;
}
update_down(i);
//int mid = tree[i].mid();
if(k < tree[lson].Max)
get_k(lson,k);
else
get_k(rson,k);
update_up(i);
} ll query(int i,int l,int r)
{
if(tree[i].l >= l && r >= tree[i].r)
{
return tree[i].num;
}
update_down(i);
int mid = tree[i].mid();
ll ad = 0;
if(l <= mid)
ad += query(lson,l,r);
if(r > mid)
ad += query(rson,l,r);
update_up(i);
return ad;
} int main()
{
int n;
while(scanf("%d",&n) != EOF && n)
{
int flag= 0;
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
} int t = 0;
mp.clear();
for(int i = 1; i <= n; i++)
{
mp[a[i]] = 1;
//cout << mp[t] <<endl ;
while(mp.find(t) != mp.end()) t++;
mex[i] = t;
} build(1,1,n);
mp.clear();
for(int i = n; i >= 1; i--)
{
if(mp[a[i]] == 0) nex[i] = n+1;
else nex[i] = mp[a[i]];
mp[a[i]] = i;
}
ll ans = 0;
for(int i = 1; i <= n; i++)
{
int nx = nex[i];
ans += query(1,i,n);
//cout << ans <<endl;
if(tree[1].Max > a[i])
{
get_k(1,a[i]);
if(cur < nx)
Insert(1,cur,nx-1,a[i]);
// cout << cur <<" "<<nx << " " << a[i] <<endl;
}
}
printf("%I64d\n",ans);
}
return 0;
} /*
Sample Input
3
0 1 3
5
1 0 2 0 1
0 Sample Output
5
24
*/

  

												

hdu 4747 线段树的更多相关文章

  1. hdu 4747 线段树/DP

    先是线段树 可以知道mex(i,i),mex(i,i+1)到mex(i,n)是递增的. 首先很容易求得mex(1,1),mex(1,2)......mex(1,n) 因为上述n个数是递增的. 然后使用 ...

  2. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. hdu 3397 线段树双标记

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  7. hdu 4533 线段树(问题转化+)

    威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  8. hdu 2871 线段树(各种操作)

    Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. RadioButton的图标改变大小(TextView也适用)

    RadioButton的图标大小并没有相应的布局参数,本文通过自定义属性的方式自定义RadioButton,实现控制图片大小. 本文要点: 自定义属性的使用. 解决RadioButton文字上.下.左 ...

  2. php代码开启缓冲的使用方法

    php可以开启缓冲区,就是将内容放到缓冲区,再决定什么时候发送给浏览器. 感谢:http://www.jb51.net/article/38964.htm 解析PHP中ob_start()函数的用法 ...

  3. 前端双引号单引号,正则反向引用,js比较jq

    1.js,jq,css,html属性必须双,如果同时出现需要嵌套使用,属性的规范是双但是也可以用单测试有效 单引号现象举例:jq中获取元素标签是单引号:$('input').click:弹出也是单引号 ...

  4. 移动端H5活动页优化方案

    背景 项目:移动端H5电商项目 痛点:慢!!! 初始方案:最基本的图片懒加载,静态资源放到cdn,predns等等已经都做了.但是还是慢,慢在哪? 显而易见的原因:由于前后端分离,所有的数据都由接口下 ...

  5. JMM简介

    JMM:Java Memory Model(Java内存模型),围绕着在并发过程中如何处理可见性.原子性.有序性这三个特性而建立的模型. 可见性:JMM提供了volatile变量定义.final.sy ...

  6. emqtt 试用(四)emq 的主题访问控制 acl.conf

    访问控制(ACL) EMQ 消息服务器通过 ACL(Access Control List) 实现 MQTT 客户端访问控制. ACL 访问控制规则定义: 允许(Allow)|拒绝(Deny) 谁(W ...

  7. 新概念英语(1-15)Your passports please

    Is there a problem wtih the Customers officer? A:Are you Swedish? B:No. We are not. We are Danish. A ...

  8. Docker学习笔记 - Docker的守护进程

    学习目标:  查看Docker守护进程的运行状态 启动.停止.重启Docker守护进程 Docker守护进程的启动选项 修改和查看Docker守护进程的启动选项 1.# 查看docker运行状态  方 ...

  9. 新概念英语(1-45)The boss's letter

    新概念英语(1-45)The boss's letter Why can't Pamela type the letter? A:Can you come here a minute, please, ...

  10. OAuth2.0学习(1-12)开源的OAuth2.0项目和比较

    OAuth2.0学习(2-1)OAuth的开源项目   1.开源项目列表 http://www.oschina.net/project/tag/307/oauth?lang=19&sort=t ...