日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)/~

總結

以上是生活随笔為你收集整理的Continuous Intervals Gym - 102222L(2018宁夏邀请赛暨2019银川icpc网络预选赛)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。