E. Range Deleting
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an and an integer xx. It is guaranteed that for every ii, 1≤ai≤x1≤ai≤x.

Let's denote a function f(l,r)f(l,r) which erases all values such that l≤ai≤rl≤ai≤r from the array aa and returns the resulting array. For example, if a=[4,1,1,4,5,2,4,3]a=[4,1,1,4,5,2,4,3], then f(2,4)=[1,1,5]f(2,4)=[1,1,5].

Your task is to calculate the number of pairs (l,r)(l,r) such that 1≤l≤r≤x1≤l≤r≤x and f(l,r)f(l,r) is sorted in non-descending order. Note that the empty array is also considered sorted.

Input

The first line contains two integers nn and xx (1≤n,x≤1061≤n,x≤106) — the length of array aa and the upper limit for its elements, respectively.

The second line contains nn integers a1,a2,…ana1,a2,…an (1≤ai≤x1≤ai≤x).

Output

Print the number of pairs 1≤l≤r≤x1≤l≤r≤x such that f(l,r)f(l,r) is sorted in non-descending order.

Examples
input

Copy
3 3
2 3 1
output

Copy
4
input

Copy
7 4
1 3 1 2 2 4 3
output

Copy
6
Note

In the first test case correct pairs are (1,1)(1,1), (1,2)(1,2), (1,3)(1,3) and (2,3)(2,3).

In the second test case correct pairs are (1,3)(1,3), (1,4)(1,4), (2,3)(2,3), (2,4)(2,4), (3,3)(3,3) and (3,4)(3,4).

题意:有一个含有n个元素的数组,数组元素的范围是 1<=ai<=x,你可以删除值在(l,r)的元素,问有多少种方案使删除后数组成为非严格单调递增数组

题解:固定左边界L,则右边界呈现单调性,所以可以二分。判断条件为考虑删除(L,R)之后,(1)L左边的呈现非严格单调递增,(2)R右边呈现非严格单调递增,(3)并且原数组中比L左边某一元素大并且在该元素左边的最大值应<=R,预处理一下即可二分

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<"["<<#x<<"]"<<x<<endl;
const int maxn=1e6+;
const int inf=1e8;
int a[maxn],b[maxn],maxx2;
bool check(ll x0,ll len){
if(b[x0-]>x0+len-)return false;
if(x0+len-<maxx2)return false;
return true;
}
int main() {
int n,x;
scanf("%d%d",&n,&x);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
int maxx=a[n];
int minn=x;
for(int i=n-;i>=;i--){
if(maxx>=a[i]){
maxx=a[i];
}
else{
minn=min(a[i],minn);
}
}
maxx=a[];
for(int i=;i<=n;i++){
if(maxx>a[i]){
b[a[i]]=max(b[a[i]],maxx);
maxx2=max(maxx2,a[i]);
}
else{
maxx=a[i];
}
}
for(int i=;i<=x;i++){
b[i]=max(b[i],b[i-]);
}
ll aans=;
for(int i=;i<=minn;i++){
ll l=;
ll r=x-i+;
ll ans=-;
while(l<=r){
ll mid=(l+r)/;
if(check(i,mid)){
ans=mid;
r=mid-;
}
else{
l=mid+;
}
}
if(ans!=-){
aans+=x-(i+ans-)+;
}
}
printf("%lld\n",aans);
return ;
} /*
*/

[ Educational Codeforces Round 65 (Rated for Div. 2)][二分]的更多相关文章

  1. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  2. Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS

    链接:https://codeforces.com/contest/1167/problem/D 题意: A string is called bracket sequence if it does ...

  3. Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution

    链接:https://codeforces.com/contest/1167/problem/C 题意: In some social network, there are nn users comm ...

  4. Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers

    链接:https://codeforces.com/contest/1167/problem/B 题意: This is an interactive problem. Remember to flu ...

  5. Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number

    链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11  ...

  6. Educational Codeforces Round 65 (Rated for Div. 2)B. Lost Numbers(交互)

    This is an interactive problem. Remember to flush your output while communicating with the testing p ...

  7. Educational Codeforces Round 65 (Rated for Div. 2)

    A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...

  8. Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)

    传送门 参考资料: [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693 题意: 给你一个包含 n 个数的序列 a,并且 ...

  9. Educational Codeforces Round 65 (Rated for Div. 2)(ACD)B是交互题,不怎么会

    A. Telephone Number A telephone number is a sequence of exactly 11 digits, where the first digit is  ...

随机推荐

  1. phaser三个学生做题目

    3个学生一起参加考试,一共有三道题,要求所有学生到齐才能开始考试,全部同学都做完第一题,学生才能继续做第二题,全部学生做完了第二题,才能做第三题,所有学生都做完的第三题,考试才结束 public cl ...

  2. java微服务的统一配置中心

    为了更好的管理应用的配置,也为了不用每次更改配置都重启应用,我们可以使用配置中心 关于eureka的服务注册和rabbitMQ的安装使用(自动更新配置需要用到rabbitMQ)这里不赘述,只关注配置中 ...

  3. AVR单片机教程——烧写hex文件

    每一次build项目,编译器都会生成多个文件,其中有一个就是hex文件.之前在IDE中配置的external tools,就是把这个hex文件烧写到单片机中去的. 然而,有些时候你想运行别人的程序,但 ...

  4. Python学习路线2019升级版(课程大纲+视频教程+网盘资源下载)

    2019最新Python全栈+人工智能学习路线升级版 全面涵盖前端.后端.爬虫.数据挖掘.人工智能等课程(课程大纲+视频教程+网盘资源下载)! 学习路线四大亮点: 1.人工智能三大主流框架全覆盖 2. ...

  5. [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)

    暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...

  6. Maven添加依赖后如何在IDEA中引用

    使用idea打开/创建maven项目,可以正常使用maven命令编译发布,但idea里智能提示.代码均找不到包,原因是idea并未引用依赖的包,这时需要添加idea引用即可 解决方法 在IDEA右侧的 ...

  7. 怎样查看系统安装的python版本

    方法一:  在命令行下使用python -V 方法二:  在命令行下进入python交互模式, 可以在第一行看到python的版本信息

  8. English-培训6-Do you like rap?

  9. AM--消息队列

    kafka rocketMq零拷贝对比 https://cloud.tencent.com/developer/news/333695 还有Linux目录下的基本原理 RocketMQ Kafka C ...

  10. 巧用XML格式数据传入存储过程转成表数据格式

    1.首先将后台数据转成对应的XML数据格式 /// <summary> /// 集合转XML数据格式 /// </summary> /// <param name=&qu ...