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<bits/stdc++.h>
#define ll long long
using 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<int,int> mp;for(int i=1;i<=n;i++){scanf("%d",&k);while(s&&_max[s].k<k)//单调炸维护区间最大值,如果当前值大于之前最大值。{pp=_max[s].pos;v=k-_max[s].k;//v是加入k之后,对于那个区间max-min-cnt的贡献update(_max[--s].pos+1,pp,v,1);//更新k是最大值的那个区间}_max[++s]=Node(i,k);//把当前值加入到栈中。while(e&&_min[e].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)/~

Continuous Intervals Gym - 102222L(2018宁夏邀请赛暨2019银川icpc网络预选赛)相关推荐

  1. Factories Gym - 102222G(2018宁夏邀请赛暨2019银川icpc网络预选赛)

    第一场icpc网络赛,出的去年宁夏邀请赛原题.c题还没有读完就有ak的了..(滑稽) Byteland has nn cities numbered from 11 to nn, and n−1n−1 ...

  2. 2018宁夏邀请赛 - Goldbach(米勒罗宾素数测试)

    题目链接:点击查看 题目大意:给出一个偶数,将其拆为两个素数之和 题目分析:因为将一个偶数拆为两个素数和时,答案会有多种情况,挑较小的素数最小时,一般此素数会非常小,最大也超不过一万,所以我们需要解决 ...

  3. 2018宁夏邀请赛 - Copy and Submit II(推公式)

    题目链接:点击查看 题目大意:给出一段程序,你可以选择直接提交,也可以不直接提交 题目分析:挺好玩的一个题,内存只给了512K,如果直接提交原代码,数组都开不出来,更别说递归了,这个题显然不能用记忆化 ...

  4. 2018宁夏邀请赛网赛 G.Trouble of Tyrant(单调栈)

    题意: 有n个点,2n-3条边的图.点 1 到每个点有一条边,编号相邻的两个点有一条边.q次询问,每次询问一个增量d,问图中每条边都增加 d 后,1 到 n 的最短路是多少.增量独立,不累加. 1 & ...

  5. L. Continuous Intervals(单调栈 + 线段树 + 思维)

    L. Continuous Intervals 给定一个长度为nnn的数组,问里面有多少个区间[l,r][l, r][l,r],满足,对这个区间排序后,两两差值$ \leq 1$,输出区间个数. 如果 ...

  6. 2018宁夏网络赛 B Goldbach (米勒拉宾素数测试)

    2018宁夏网络赛 B Goldbach (米勒拉宾素数测试) 题目链接 题目大意: 给你一个偶数n (2<n<=1e18) 让你把n分解成两个素数的和.(如果有多个输出任意一个) 解题思 ...

  7. 2018 湘潭邀请赛 部分题解

    2018 湘潭邀请赛 题解 A C F G K .其它题解,后续添加 A 题 没啥好讲的,签到题 从后面往前面数,大于个数的时候直接输出就行了. AC代码: #include<iostream& ...

  8. 2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛题解

    2020 年 "联想杯"全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛题解 萌新又来写题解啦 原题链接 (不是按照题号顺序来的QWQ) L. Lottery Ticke ...

  9. 徐敏 计算机科学教育,计算机学院举办梦想公开课暨2019年暑期社会实践动员大会...

    5月6日下午,计算机科学与技术学院/人工智能学院在将军路校区2110教室举办梦想公开课暨2019年暑期社会实践动员大会.此次公开课由学院团委书记耿瑞.团委副书记蔡月啸和朱逸帆担任主讲人,学院2018级 ...

最新文章

  1. 什么是XLNet,它为什么比BERT效果好?
  2. spring-使用配置文件完成JdbcTemplate操作数据库-c3p0
  3. dlp防泄密系统卸载_浙江好用的企业图纸防泄密软件推荐,局域网内部图纸透明加密方案...
  4. 默认标题栏字体_不喜欢Windows10上的默认系统字体,可以这样更改
  5. Java求n以内素数_求0到n之间素数个数的序列(Java)
  6. usnews 计算机专业排名,2019美国大学USNews计算机专业排名
  7. oracle查询并列,【问】oracle-查询各门课程的前2名和后2名
  8. 华师大数据科学考研_华东师范大学数据科学与工程需要复习哪些内容?
  9. ELK收集tomcat日志
  10. 计算机网络复习-物理层
  11. [剑指Offer] 第5章课后题详解
  12. Proteus 数字示波器
  13. U盘文件被隐藏后的修复方法
  14. 矩阵知识:线性方程组解的情况
  15. 川大计算机学硕调剂专硕,19双非考研川大计算机学院专硕经验~
  16. oracle怎么截取long类型,如何把long类型的值取出来
  17. Android手机截图怎么做,怎样在手机上截图(安卓手机、苹果手机截图方法)
  18. 操作系统进程线程区别、并发和并行、内存和外存
  19. 饿饿,饭饭「每日一题」
  20. 2.6-2.7 向量运算的基本性质零向量

热门文章

  1. 一文搞定Swing和Qt按钮和文本框的创建
  2. php 什么时候销毁对象,什么决定什么时候在PHP中销毁类对象?
  3. mysqldump导出数据库视图_mysql数据库的基本操作:索引、视图,导入和导出,备份和恢复...
  4. kafka时间轮linux时间轮,Kafka解惑之时间轮 (TimingWheel)
  5. map怎么转化dto_阿里面试题:为什么Map桶中个数超过8才转为红黑树
  6. goland go test_七天用Go写个docker(第一天)
  7. bootstrap 数据加载中提示_解决Quartz定时器中查询懒加载数据no session的问题
  8. Scrapped or attached views may not be recycled. isScrap:false isAttached:true错误
  9. localhost、127.0.0.1对网络编程造成不通的说明
  10. Timer与ScheduledExecutorService间的选择