Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define pb insert
- int i,j,x,ans,cnt,n,L,R;
- int ar[100007];
- set<int> st;
- set<pair<int,int> > myset;
- unordered_map<int,int> pos;
- int main()
- {
- while(cin>>n>>L>>R)
- {
- for(i=1;i<=n;i++)
- scanf("%d",ar+i);
- pos.clear();
- st.clear();
- myset.clear();
- ans=0;
- if(L==1)
- ans=n, L=2;
- i=1, j=1;
- x=R-L+1;
- while(i<=j && j<=n)
- {
- st.insert(ar[j]) ;
- if(st.size()<(j-i+1))
- {
- int len=min( pos[ar[j]]+1-i+1, R );
- if(len<R && len>0 && (pos[ar[j]]+1)<j)
- myset.pb({i,pos[ar[j]]+1});
- else if((pos[ar[j]]+1)==j && (len-1)<R && (len-1)>0 )
- myset.pb({i,pos[ar[j]]});
- for(i=i;i<=pos[ar[j]];i++)
- if(i<pos[ar[j]])
- st.erase(ar[i]), pos[ar[i]]=0;
- }
- pos[ar[j]]=j;
- if(j-i+1==R)
- myset.pb({i,j});
- else if((j-i+1)>R)
- {
- ans++;
- for(i=i;i<=j-2;i++)
- {
- pos[ar[i]]=0;
- st.erase(ar[i]);
- }
- if(j==n && (j-i+1)>=L && (j-i+1)<=R)
- myset.pb({i,j});
- }
- else if((j-i+1)>=L && j==n)
- myset.pb({i,j});
- j++;
- }
- for(auto von: myset)
- {
- int length=von.second-von.first+1;
- int xx=max(length-L+1,0);
- ans+=1LL*xx*(xx+1)/2;
- }
- cout<<ans<<'\n';
- }
- return 0;
- }
- /*
- 6 1 3
- 1 2 3 4 5 6
- 15
- 6 2 2
- 1 2 1 3 1 3
- 10 1 3
- 3 0 1 0 1 0 0 0 0 2
- 5 1 3
- 1 2 1 2 1
- */
Advertisement
Add Comment
Please, Sign In to add comment