当前位置:网站首页>HDU 4747 Mex「建议收藏」
HDU 4747 Mex「建议收藏」
2022-07-07 21:51:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
题意:
给出一段数字a 定义mex(l,r)表示a[l]…a[r]中最小的不连续的数字 求出全部mex(l,r)的和
思路:
首先能够想到由l開始到n的全部数字的mex值必定是递增的 那么就能够求出以1開始到n的全部数字的mex 从前到后扫一遍就可以 这时能够求出[1,r]全部区间的mex和 利用线段树就可以
接着考虑怎样求[2,r]、[3,r]…. 由[1,r]到[2,r]的转变无非是去掉第一个数字 那么去掉一个数字的影响是什么呢?
比方去掉一个2 那他最多影响到下一个2出现的地方 并且 他仅仅是把mex>2的地方改成了2 即从2处截断 又由于之前所说的递增关系 所以影响的区间也一定是连续的!
那么我们就能够每次删掉一个数字 利用线段树找出他影响的区间 并把这个区间覆盖为那个删掉的数字
最后每次求和就是答案
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef __int64 ll;
#define N 201000
#define L(x) (x<<1)
#define R(x) ((x<<1)|1)
struct node
{
int l,r,val,lazy;
ll sum;
}tree[N*4];
int a[N],mex[N],next[N];
int n,l,r;
map<int,int> mp;
ll ans;
void up(int i)
{
tree[i].val=max(tree[L(i)].val,tree[R(i)].val);
tree[i].sum=tree[L(i)].sum+tree[R(i)].sum;
}
void down(int i)
{
if(tree[i].lazy!=-1)
{
tree[L(i)].lazy=tree[i].lazy;
tree[L(i)].val=tree[i].lazy;
tree[L(i)].sum=(tree[L(i)].r-tree[L(i)].l+1)*tree[i].lazy;
tree[R(i)].lazy=tree[i].lazy;
tree[R(i)].val=tree[i].lazy;
tree[R(i)].sum=(tree[R(i)].r-tree[R(i)].l+1)*tree[i].lazy;
tree[i].lazy=-1;
}
}
void init(int l,int r,int i)
{
tree[i].l=l; tree[i].r=r; tree[i].lazy=-1;
if(l==r)
{
tree[i].val=mex[l];
tree[i].sum=mex[l];
return ;
}
int mid=(l+r)>>1;
init(l,mid,L(i));
init(mid+1,r,R(i));
up(i);
}
void update(int l,int r,int i,int k)
{
if(l==tree[i].l&&r==tree[i].r)
{
tree[i].sum=(tree[i].r-tree[i].l+1)*k;
tree[i].val=k;
tree[i].lazy=k;
return ;
}
down(i);
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid) update(l,r,L(i),k);
else if(l>mid) update(l,r,R(i),k);
else
{
update(l,mid,L(i),k);
update(mid+1,r,R(i),k);
}
up(i);
}
void query(int i,int k)
{
if(tree[i].l==tree[i].r)
{
if(tree[i].val>k) l=tree[i].l;
else l=n+1;
return ;
}
down(i);
if(tree[L(i)].val>k) query(L(i),k);
else query(R(i),k);
up(i);
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
if(!n) break;
mp.clear(); // get mex
j=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
mp[a[i]]=1;
while(mp[j]) j++;
mex[i]=j;
}
mp.clear(); // get next
for(i=1;i<=n;i++) mp[a[i]]=n+1;
for(i=n;i>=1;i--)
{
next[i]=mp[a[i]];
mp[a[i]]=i;
}
//for(i=1;i<=n;i++) printf("%d %d\n",mex[i],next[i]);
init(1,n,1);
for(i=1,ans=0;i<=n;i++)
{
ans+=tree[1].sum;
query(1,a[i]);
r=next[i];
//printf("%d %d\n",l,r);
if(l<r)
{
update(l,r-1,1,a[i]);
}
update(i,i,1,0);
}
printf("%I64d\n",ans);
}
return 0;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116215.html原文链接:https://javaforall.cn
边栏推荐
- opencv scalar传入三个参数只能显示黑白灰问题解决
- Wechat forum exchange applet system graduation design (3) background function
- 微信论坛交流小程序系统毕业设计毕设(5)任务书
- How to generate unique file names
- STL标准模板库(Standard Template Library)一周学习总结
- LDO稳压芯片-内部框图及选型参数
- GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
- 微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
- 网络安全-burpsuit
- 智慧社區和智慧城市之間有什麼异同
猜你喜欢

Wechat forum exchange applet system graduation design completion (1) development outline

Specific method example of V20 frequency converter manual automatic switching (local remote switching)

ArcGIS: two methods of attribute fusion of the same field of vector elements

ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
![[microservices SCG] gateway integration Sentinel](/img/f3/410d7228b4b253ebf41015a785099f.png)
[microservices SCG] gateway integration Sentinel

微信论坛交流小程序系统毕业设计毕设(5)任务书

经纬度PLT文件格式说明

Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle

JMeter-接口自动化测试读取用例,执行并结果回写

漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
随机推荐
微信论坛交流小程序系统毕业设计毕设(3)后台功能
LeeCode -- 6. Zigzag transformation
Network security - information query of operating system
微信论坛交流小程序系统毕业设计毕设(4)开题报告
二叉树(Binary Tree)
Clean C disk
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
USB (十七)2022-04-15
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
RE1 attack and defense world reverse
系统架构设计师备考经验分享:论文出题方向
VS扩展工具笔记
Network security - joint query injection
1. Sum of two numbers
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
PMP项目管理考试过关口诀-1
Wechat forum exchange applet system graduation design completion (4) opening report
USB(十六)2022-04-28
为什么市场需要低代码?