2019ICPC宁夏网络赛

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 a_1, a_2, a_3, \cdots , a_na1​,a2​,a3​,⋯,an​. She is amazed at those intervals, which is a consecutive subsequence of a_1, a_2, \cdots , a_na1​,a2​,⋯,an​, with several continuous numbers, and names them continuous intervals.

More precisely, consider a interval a_l, a_{l+1}, \cdots , a_{r-1}, a_ral​,al+1​,⋯,ar−1​,ar​ for instance where 1 \le l \le r \le 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 \le n \le 10^5)n(1≤n≤105) which is the length of the given se- quence. The second line contains nn integers describing all items of the sequence, where the ii-th one is denoted by a_i (1 \le a_i \le 10^9)ai​(1≤ai​≤109).

We guarantee that the sum of nn in all test cases is up to 10^6106.

Output

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

输出时每行末尾的多余空格,不影响答案正确性

样例输入复制

2
4
1 2 1 2
4
1 3 2 4

样例输出复制

Case #1: 10
Case #2: 8

题意:给出一个长度为n的序列,找出这个序列里有多少个连续区间(区间内的数任意两个相邻的差的绝对值小于等于1)。

解法之一:观察出一个连续区间有这样的性质是 Max-Min-1=cnt,其中cnt是区间里数字的种类数,Max和Min是区间最大和最小。移项后得Max-Min-cnt=-1,即维护区间的Max-Min-cnt的值,连续区间的个数即区间最小值 是-1的个数。

#include <iostream>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=100010;
struct gjm
{
    ll va;
    int id;
}a[maxn],x[maxn],y[maxn];
struct node
{
    int l,r;
    ll minv;
    ll cnt;
    ll lazy;
}tree[maxn<<2];
void pushup(int rt)
{
    tree[rt].minv=min(tree[rt<<1].minv,tree[rt<<1|1].minv);
    if(tree[rt<<1|1].minv==tree[rt<<1].minv)
    {
        tree[rt].cnt=tree[rt<<1].cnt+tree[rt<<1|1].cnt;
    }
    else if(tree[rt<<1].minv==tree[rt].minv)
    {
        tree[rt].cnt=tree[rt<<1].cnt;
    }
    else if(tree[rt<<1|1].minv==tree[rt].minv)
    {
        tree[rt].cnt=tree[rt<<1|1].cnt;
    }
}
void pushdown(int rt)
{
    if(tree[rt].lazy!=0)
    {
        tree[rt<<1].minv+=tree[rt].lazy;
        tree[rt<<1|1].minv+=tree[rt].lazy;
        tree[rt<<1].lazy+=tree[rt].lazy;
        tree[rt<<1|1].lazy+=tree[rt].lazy;
        tree[rt].lazy=0;
    }
}
void build(int rt,int l,int r)
{
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].minv=0,tree[rt].cnt=0;
    tree[rt].lazy=0;
    if(l==r)
    {
        tree[rt].cnt=1;return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void update(int rt,int L,int R,ll x)
{
    if(tree[rt].l>=L&&tree[rt].r<=R)
    {
        tree[rt].minv+=x;
        tree[rt].lazy+=x;
        return ;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=L) update(rt<<1,L,R,x);
    if(mid<R) update(rt<<1|1,L,R,x);
    pushup(rt);
}
ll query(int rt,int L,int R)
{
    if(tree[rt].l>=L&&tree[rt].r<=R)
    {
        if(tree[rt].minv==-1)
        {
            return tree[rt].cnt;
        }
        else return 0;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    ll ans=0;
    if(mid>=L) ans+=query(rt<<1,L,R);
    if(mid<R) ans+=query(rt<<1|1,L,R);
    return ans;
}
int main()
{
    int T;
    int kcase=0;
    scanf("%d",&T);
    while(T--)
    {
        kcase++;
        map<ll,int> ma;
        int n;
        scanf("%d",&n);
        build(1,1,n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].va);
            a[i].id=i;
        }
        int t1=0,t2=0;
        ll res=0;
        for(int i=1;i<=n;i++)
        {
            while(t1>0&&a[i].va>x[t1].va)//维护一个单调递减的栈
            {
                update(1,x[t1-1].id+1,x[t1].id,a[i].va-x[t1].va);//更新以a[i]为最大值的区间
                t1--;
            }
            x[++t1]=a[i];
            while(t2>0&&a[i].va<y[t2].va)//维护一个单调递增的栈
            {
                update(1,y[t2-1].id+1,y[t2].id,y[t2].va-a[i].va);//更新以a[i]为最小值的区间
                t2--;
            }
            y[++t2]=a[i];
            int pos=ma[a[i].va];
            ma[a[i].va]=i;
            update(1,pos+1,i,-1);//更新a[i]对数字种类个数的贡献(pos+1,i)这个区间内贡献为1,所以-1.
            res+=query(1,1,i);
        }
        printf("Case #%d: %lld\n",kcase,res);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值