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. redis 主从 哨兵

    数据库为什么要读写分离 写代码好多年了,大家先抛弃在代码框架里面各种花哨的设计之外,写的代码到最后无非就是为了增删查改数据库.一般项目数据库刚开始只是但一个库,随着数据量的增大,就开始优化数据库(抛开 ...

  2. Windows10下Anaconda+Tensorflow+Keras环境配置

    注意!注意!!注意!!! (重要的事情说三遍) 安装前检查: 1.Tensorflow不支持Anaconda2,Tensorflow也不支持python2.7和python3.7(满满的辛酸泪!) 2 ...

  3. Java正则表达式获取中括号之间的内容

    参考: 求一个正则表达式提取中括号里的内容 [问题点数:80分]CSDN论坛 > Java > Web 开发 正则表达式 - 菜鸟教程 不包含中括号 正则表达式如下: \\[(.*?)] ...

  4. jdk8新特性--使用lambda表达式的延迟执行特性优化性能

    使用lambda表达式的延迟加载特性对代码进行优化:

  5. spring 条件化配置

    步骤一: 实现接口:org.springframework.context.annotation.Condition import org.springframework.context.annota ...

  6. hdu 2353 n皇后问题

    Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N, ...

  7. Java 之 字符缓冲流

    一.字符缓冲输出流 java.io.BufferedWriter extends Writer BufferedWriter:字符缓冲输出流. 继承自父类的共性成员方法: void write(int ...

  8. SD-定义定价用途的条件表(Condition Table)

    https://www.fenginfo.com/815.html 条件表(Condition Table)是SAP条件技术体系中最基础的数据源,它们是一群特定的数据表,表名是以特定字母开头 + 三位 ...

  9. DBShop前台RCE

    前言 处理重装系统的Controller在判断是否有锁文件后用的是重定向而不是exit,这样后面的逻辑代码还是会执行,导致了数据库重装漏洞和RCE. 正文 InstallController.php中 ...

  10. iOS开发-NSString去掉所有换行及空格

    1.去掉字符串两端的空格及回车 - (NSString *)removeSpaceAndNewline:(NSString *)str{ NSString *temp = [str stringByT ...