博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Continuous Intervals Gym - 102222L(2018宁夏邀请赛暨2019银川icpc网络预选赛)
阅读量:4136 次
发布时间:2019-05-25

本文共 3962 字,大约阅读时间需要 13 分钟。

Lamis is a smart girl. She is interested in problems about sequences and their intervals.

Here she shows you a sequence of length nn with positive integers, denoted by a1,a2,a3,⋯,ana1,a2,a3,⋯,an. She is amazed at those intervals, which is a consecutive subsequence of a1,a2,⋯,ana1,a2,⋯,an, with several continuous numbers, and names them continuous intervals.

More precisely, consider a interval al,al+1,⋯,ar−1,aral,al+1,⋯,ar−1,ar for instance where 1≤l≤r≤n1≤l≤r≤n. If, after sorting the interval, the difference of any two neighbouring items is less than or equal to 11, the interval will be considered as continuous.

As her best friends, you came from far and wide and travelled thousands of miles to Ningxia, to help her count the number of continuous intervals of the sequence.

Input

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 10001000.

For each test case, the first line contains an integer n (1≤n≤105)n (1≤n≤105) which is the length of the given sequence. The second line contains nn integers describing all items of the sequence, where the ii-th one is denoted by ai (1≤ai≤109)ai (1≤ai≤109).

We guarantee that the sum of nn in all test cases is up to 106106.

Output

For each test case, output a line containing Case #x: y, where x is the test case number starting from 11, and y is the number of continuous intervals in this test case.

Example

Input
2
4
1 2 1 2
4
1 3 2 4
Output
Case #1: 10
Case #2: 8
题意:找出区间max-区间min+1区间数字种类数的区间个数。
找出max-min+1=cnt的区间个数,就是求max-min-cnt
-1的区间个数。这样就转换成了求区间max-min-cnt最小的问题。并且要维护最小值的个数。对于区间最大值,我们可以用单调栈维护,并且记录下来他们的位置。对于区间最小值也是一样,也是用单调炸维护。对于求区间个数,就是普通操作。具体解释看代码。
代码如下:

#include
#define ll long longusing namespace std;const int maxx=1e5+100;struct node{
int l; int r; int sum; int _max; int lazy;}p[maxx<<2];struct Node{
int pos; int k; Node(int x=0,int y=0){
pos=x;k=y;}}_max[maxx],_min[maxx];int n;inline void pushup(int cur)//维护最小值并且记录最小值的个数{
p[cur]._max=min(p[cur<<1]._max,p[cur<<1|1]._max); if(p[cur<<1]._max==p[cur<<1|1]._max) p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum; else p[cur].sum=min(p[cur<<1]._max,p[cur<<1|1]._max)==p[cur<<1]._max?p[cur<<1].sum:p[cur<<1|1].sum;}inline void pushdown(int cur){
if(p[cur].lazy) {
p[cur<<1]._max+=p[cur].lazy; p[cur<<1|1]._max+=p[cur].lazy; p[cur<<1].lazy+=p[cur].lazy; p[cur<<1|1].lazy+=p[cur].lazy; p[cur].lazy=0; }}inline void build(int l,int r,int cur){
p[cur].l=l; p[cur].r=r; p[cur].lazy=0; p[cur].sum=p[cur]._max=0; if(l==r) {
p[cur].sum=1;//对于每一个数来说,它本身就是那样的一个区间。 return ; } int mid=l+r>>1; build(l,mid,cur<<1); build(mid+1,r,cur<<1|1);}inline void update(int l,int r,int v,int cur){
int L=p[cur].l; int R=p[cur].r; if(l<=L&&R<=r) {
p[cur].lazy+=v; p[cur]._max+=v; return ; } pushdown(cur); int mid=L+R>>1; if(r<=mid) update(l,r,v,cur<<1); else if(l>mid) update(l,r,v,cur<<1|1); else {
update(l,mid,v,cur<<1); update(mid+1,r,v,cur<<1|1); } pushup(cur);}inline ll query(int l,int r,int cur){
int L=p[cur].l; int R=p[cur].r; if(l<=L&&R<=r) {
if(p[cur]._max==-1) return p[cur].sum; else return 0;//只有最小值是-1的时候才是我们要找的那种区间,否则就不是。 } pushdown(cur); int mid=L+R>>1; if(r<=mid) return query(l,r,cur<<1); else if(l>mid) return query(l,r,cur<<1|1); else return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);}inline void solve(){
int t,k,v,pp,kk=0; scanf("%d",&t); while(t--) {
ll ans=0; scanf("%d",&n); build(1,n,1); int s=0,e=0; map
mp; for(int i=1;i<=n;i++) {
scanf("%d",&k); while(s&&_max[s].k
k)//最小值一样。 {
pp=_min[e].pos;v=_min[e].k-k; update(_min[--e].pos+1,pp,v,1); } _min[++e]=Node(i,k); update(mp[k]+1,i,-1,1);//mp[k]记录的是k上一次出现的位置,对于这个位置前的位置,加入的这个k是没有贡献的,只对这个位置到i之间的位置有贡献,数的种类增加,cnt增加,对于max-min-cnt就-1了。 mp[k]=i; ans+=query(1,i,1);//1~i的所要求的区间数。 } printf("Case #%d: %lld\n",++kk,ans); }}int main(){
solve(); return 0;}

努力加油a啊,(o)/~

转载地址:http://vztvi.baihongyu.com/

你可能感兴趣的文章
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
还不会正则表达式?看这篇!
查看>>
100道+ JavaScript 面试题,助你查漏补缺
查看>>
JavaScript深入理解之闭包
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
25个构建Web项目的HTML建议,你需要了解一下!
查看>>
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>