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

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)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值