fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. pair<int,int> csl(vector<int>&a,int k){
  4. unordered_map<int,int>m;
  5. int s=0,minl=INT_MAX,maxl=0,cntm=0,cntx=0;
  6. m[0]=-1;
  7. for(int i=0;i<a.size();i++){
  8. s+=a[i];
  9. if(m.find(s-k)!=m.end()){
  10. int len=i-m[s-k];
  11. if(len<minl){
  12. minl=len;
  13. cntm=1;
  14. }
  15. else if(len==minl){
  16. cntm++;
  17. }
  18. if(len>maxl){
  19. maxl=len;
  20. cntx=1;
  21. }
  22. else if(len==maxl){
  23. cntx++;
  24. }
  25. }
  26. if(m.find(s)==m.end()){
  27. m[s]=i;
  28. }
  29. }
  30. return {cntm,cntx};
  31. }
  32. int main(){
  33. vector<int>a={1,2,3,3,2};int k=5;
  34. auto res=csl(a,k);
  35. cout<<res.first<<" "<<res.second<<"\n";
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
2 2