Untitled
unknown
plain_text
a year ago
647 B
2
Indexable
Never
#include<bits/stdc++.h> using namespace std; /* 2, 0, 11, 3, 0 i=0 j=2 ((j-i+1) * (j-i+2))/2 3*4/2 = 6 2 2 0 2 0 11 cnt = 0+1+1 */ int cntSubarray(int n){ return (n*(n+1))/2; } int main(){ int l=1, r=10; vector<int> arr = {2, 0, 11, 3, 0}; int res=0, i=0, e=0; for(int t: arr){ if(t>r){ res+=(cntSubarray(i)-cntSubarray(e)); i=0; e=0; }else if(t<l){ i++; e++; }else{ res-=cntSubarray(e); e=0; } } res+=(cntSubarray(i)-cntSubarray(e)); cout<<res<<endl; return 0; }