Guest User

Untitled

a guest
Apr 27th, 2017
1,733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define pb insert
  6. int i,j,x,ans,cnt,n,L,R;
  7. int ar[100007];
  8. set<int> st;
  9. set<pair<int,int> > myset;
  10. unordered_map<int,int> pos;
  11. int main()
  12. {
  13. while(cin>>n>>L>>R)
  14. {
  15. for(i=1;i<=n;i++)
  16. scanf("%d",ar+i);
  17.  
  18. pos.clear();
  19. st.clear();
  20. myset.clear();
  21.  
  22. ans=0;
  23.  
  24. if(L==1)
  25. ans=n, L=2;
  26.  
  27. i=1, j=1;
  28. x=R-L+1;
  29.  
  30. while(i<=j && j<=n)
  31. {
  32. st.insert(ar[j]) ;
  33.  
  34. if(st.size()<(j-i+1))
  35. {
  36. int len=min( pos[ar[j]]+1-i+1, R );
  37.  
  38. if(len<R && len>0 && (pos[ar[j]]+1)<j)
  39. myset.pb({i,pos[ar[j]]+1});
  40. else if((pos[ar[j]]+1)==j && (len-1)<R && (len-1)>0 )
  41. myset.pb({i,pos[ar[j]]});
  42.  
  43. for(i=i;i<=pos[ar[j]];i++)
  44. if(i<pos[ar[j]])
  45. st.erase(ar[i]), pos[ar[i]]=0;
  46. }
  47.  
  48. pos[ar[j]]=j;
  49.  
  50. if(j-i+1==R)
  51. myset.pb({i,j});
  52. else if((j-i+1)>R)
  53. {
  54. ans++;
  55. for(i=i;i<=j-2;i++)
  56. {
  57. pos[ar[i]]=0;
  58. st.erase(ar[i]);
  59. }
  60.  
  61. if(j==n && (j-i+1)>=L && (j-i+1)<=R)
  62. myset.pb({i,j});
  63.  
  64. }
  65. else if((j-i+1)>=L && j==n)
  66. myset.pb({i,j});
  67.  
  68. j++;
  69. }
  70.  
  71. for(auto von: myset)
  72. {
  73. int length=von.second-von.first+1;
  74. int xx=max(length-L+1,0);
  75. ans+=1LL*xx*(xx+1)/2;
  76. }
  77.  
  78. cout<<ans<<'\n';
  79.  
  80. }
  81.  
  82. return 0;
  83. }
  84.  
  85. /*
  86. 6 1 3
  87. 1 2 3 4 5 6
  88. 15
  89.  
  90. 6 2 2
  91. 1 2 1 3 1 3
  92. 10 1 3
  93. 3 0 1 0 1 0 0 0 0 2
  94. 5 1 3
  95. 1 2 1 2 1
  96. */
Advertisement
Add Comment
Please, Sign In to add comment