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. PHP-FPM配置与调优

    PHP-FPM是啥? PHP-FPM是一个PHP FastCGI的管理器,它实际上就是PHP源代码的补丁,旨在将FastCGI进程管理引进到PHP软件包中. CGI是啥? CGI全称"公共网 ...

  2. vue之项目打包部署生产环境前需要注意的问题!

    我们在本地写好代码,然后打包上线前需要修改相关的配置文件,否则项目无法正常运行,主要是路径不对,找不到相关文件. 1.修改config > index.js(如下图) 2.在build > ...

  3. git当前项目免密提交

    在项目目录下,执行: vim .git/config 在url后边添加用户名密码即可 ``` [core] repositoryformatversion = filemode = true bare ...

  4. springboot的propteis的基本配置参考

    其中mybatis.cfg.xml文件可以不加,这个文件最主要是开启mybatis的二级缓存:  

  5. models环境配置和表查询

    一般操作 在进行一般操作时先配置一下参数,使得我们可以直接在Django页面中运行我们的测试脚本 在Python脚本中调用Django环境 模型转为mysql数据库中的表settings配置 需要在s ...

  6. python 之 面向对象基础(组合和封装)

    7.4 组合 解决类与类之间代码冗余问题有两种解决方案: 1.继承:描述的是类与类之间,什么是什么的关系 2.组合:描述的是类与类之间的关系,是一种什么有什么的关系 一个类产生的对象,该对象拥有一个属 ...

  7. Python 操作MySQL 数据库

    Python 操作 MySQL 数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的 ...

  8. Feign 客户端调用错误

    1.@RequestBody 必须要写在实现接口中 2.Feign 客户端调用的时候如果有参数的话,默认是发送post请求 3.服务接口中的请求参数必须要加上@RequestParam("r ...

  9. C++ 去掉字符串的首尾空格和全部空格

    #include <iostream>#include <string>using namespace std; //去掉收尾空格string& ClearHeadTa ...

  10. Informix从一个表更新多选数据到另一个表

    功能如题, Informix从一个表更新多选数据到另一个表 例如, 要更新tab01的几个字段数据, 这些数据来自tab02, tab01和tab02之间通过id关联 参考语句: update tab ...