'Number of SubArrays' Techniques
Problem Types
You are told to compute number of SubArrays with certain property .
Approach 1
- Scan line from left to right .
- For each right end (
r
) , calculate how many SubArrays starting with somel
such that l<r . Add this count ofl
Approach 2
- Fix Border
[l,r]
with the given candidate property - For each border add border length to the SubArray
Example 1
Calculate the number of SubArrays such that Sum(l,r) = k
Solution Sketch
1 | Sum(l,r) = k |
Code
1 |
|
Example 2
Calculate the number of SubArrays such that Sum(l,r) = SubArray's length
Solution Sketch
The solution is almost identical except there is something more to learn in here . As the paramter k
consists of l
and r
, the idea is to put them in such a way that f(l)
and f(r)
are on diffrent sides.
1 | Sum(l,r) = r-l+1 |
Code
1 |
|
More Practice Problems
Count the Subarrays (HackerEarth)
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/** Which of the favors of your Lord will you deny ? **/
void solveTC()
{
int n,k;
cin>>n>>k;
vector<int>v(n+1),p(n+1);
for(int i=1;i<=n;i++) cin>>v[i];
for(int i=1;i<=n;i++) p[i] = p[i-1] + v[i];
sort(ALL(p));
DBG(p);
/**
p(r) > p(l-1) + k
for every l-1 we count how many r's such that p(r) > p(l-1) + k
**/
int ans = 0;
for(int i=1;i<=n;i++)
{
int need = p[i-1] + k;
int less = upper_bound(ALL(p),need) - p.begin() - 1;
DBG(less);
ans += n-less;
}
cout<<ans<<endl;
}Number of Subarrays with Bounded Maximum (LeetCode)
Hint
2 Pointer . Find the candidate borders [l,r]
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Solution {
public:
int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
int n = A.size();
int left = -1 , right = -1 , ans = 0;
for(int i=0;i<n;i++)
{
if(A[i]>=L) right = i;
if(A[i]>R) left = i;
ans += (right-left);
}
return ans;
}
};
Increasing SubArrays (CodeChef)
Hint
2 Pointer . Find the candidate borders [l,r]
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void solveTC()
{
int n;
cin>>n;
vector<int>v(n+1);
for(int i=1;i<=n;i++) cin>>v[i];
int left = 0, right = 0, ans = 0;
for(int i=1; i<=n; i++)
{
if(v[i]>=v[i-1])
right = i;
else if(v[i]<v[i-1])
left = i-1 , right = i;
DBG(right-left);
ans += (right-left); /// number of candidate subarrays in the [left+1,right] border
}
cout<<ans<<endl;
}Three Occurreneces (Codeforces)
Hint 1
Can you think of a way to solve the number of distinct elements in ranges
[i,fixed R]
using Segment Tree ?Hint 2
Can you think of a way to track the number of numbers that have frequency 3 in ranges
[i,fixed R]
using the same Segment Tree ?Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294/** Which of the favors of your Lord will you deny ? **/
using namespace std;
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);
inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int INF = 1e9;
struct node
{
PLL mx;
LL lazy;
node()
{
mx = {-INF,0};
lazy = 0;
}
node(PLL mx)
{
this-> mx = mx;
lazy = 0;
}
void create_leaf(LL val)
{
mx = {val,1};
lazy = 0;
}
};
struct LazySegmentTree
{
vector<node>Tree;
vector<int>ara;
int n;
LazySegmentTree(int n)
{
this->n = n;
int len = n+1;
ara = vector<int>(len);
Tree = vector<node>(len<<2);
}
void build()
{
__build(1,1,n);
}
void update(int L,int R,LL add)
{
__update(1,1,n,L,R,add);
}
node query(int L,int R)
{
return __query(1,1,n,L,R);
}
/**-------------------------------------**/
node merge_nodes(node &A,node &B)
{
if(A.mx.F>B.mx.F)
return A;
else if(A.mx.F<B.mx.F)
return B;
else
return node({A.mx.F,A.mx.S+B.mx.S});
}
void node_update(int v,LL add)
{
Tree[v].mx.F += add;
Tree[v].lazy += add;
}
void push(int v,int st,int en)
{
int lc = v<<1, rc = lc|1;
if(st != en)
{
node_update(lc,Tree[v].lazy);
node_update(rc,Tree[v].lazy);
}
Tree[v].lazy = 0;
}
void __build(int cur,int start,int end) /** build the segment tree **/
{
if(start==end)
{
Tree[cur].create_leaf(0);
return;
}
int mid = (start+end)>>1;
int lc = cur<<1, rc = lc|1;
__build(lc,start,mid);
__build(rc,mid+1,end);
Tree[cur] = merge_nodes(Tree[lc],Tree[rc]);
}
void __update(int cur, int start, int end, int l,int r, LL add)
{
push(cur,start,end); /** Pushdown to children node **/
if(r<start || end<l) return;
else if (l<=start && r>=end)
{
node_update(cur,add);
push(cur,start,end); /** Pushdown to children node **/
}
else
{
int mid = (start + end)>>1;
int lc = cur<<1, rc = lc|1;
__update(lc, start, mid,l,min(r,mid), add);
__update(rc, mid+1, end,max(l,mid+1),r, add);
Tree[cur] = merge_nodes(Tree[lc],Tree[rc]);
}
}
node __query(int cur,int start,int end,int l,int r) /** RANGE query **/
{
push(cur,start,end); /** Pushdown to children node **/
if(l>r) return node({-INF,0});
if(start>=l && end<=r)
{
return Tree[cur];
}
int mid = (start+end)>>1;
int lc = cur<<1, rc = lc|1;
node p1 = __query(lc,start,mid,l,min(r,mid));
node p2 = __query(rc,mid+1,end,max(l,mid+1),r);
return merge_nodes(p1,p2);
}
};
void UNDO(int x, LazySegmentTree &lst, vector<vector<int>> &pos)
{
int sz = pos[x].size();
if(sz<4)
return;
int L = pos[x][sz-4]+1;
int R = pos[x][sz-3];
lst.update(L,R,-1);
}
void DO(int x, LazySegmentTree &lst, vector<vector<int>> &pos)
{
int sz = pos[x].size();
if(sz<4)
return;
int L = pos[x][sz-4]+1;
int R = pos[x][sz-3];
lst.update(L,R,+1);
}
int32_t main()
{
optimizeIO();
int n;
cin>>n;
LazySegmentTree LST(n);
LST.build();
vector<int>v(n+1);
for(int i=1; i<=n; i++)
cin>>v[i];
int max_num = n+1;
vector<int>last(max_num,0);
vector<vector<int>>pos(max_num);
node q = LST.query(1,n);
DBG(q.mx);
LL ans = 0;
for(int i=1; i<=n; i++)
{
int el = v[i];
if(pos[el].empty())
pos[el].push_back(0);
UNDO(el,LST,pos);
pos[el].push_back(i);
DO(el,LST,pos);
int L = last[el]+1;
int R = i;
last[el] = i;
LST.update(L,R,-1);
node q = LST.query(1,i);
DBG(q.mx);
if(q.mx.F==0) ans += q.mx.S;
}
cout<<ans<<endl;
return 0;
}
/**
**/
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
os<<"{"<<p.first<<", "<<p.second<<"} ";
return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" " ;
}
os<<" ]";
return os;
}
template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" ";
}
os<<" ]";
return os;
}