Skip to content

Commit 21f2aa5

Browse files
authored
Create even_odd_numbers_in_range.cpp
1 parent 473fd34 commit 21f2aa5

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/help-ashu-1/
2+
3+
/*
4+
C++
5+
encoding: UTF-8
6+
Modified: <03/Oct/2019 04:18:08 PM>
7+
8+
✪ H4WK3yE乡
9+
Mohd. Farhan Tahir
10+
Indian Institute Of Information Technology (IIIT), Gwalior
11+
*/
12+
13+
#include <bits/stdc++.h>
14+
#ifdef LOCAL_PROJECT
15+
# include <prettypr.hpp>
16+
#endif
17+
18+
using namespace std;
19+
20+
// clang-format off
21+
22+
#define ll long long
23+
#define ve vector
24+
#define pb push_back
25+
#define endl "\n"
26+
#define ff first
27+
#define ss second
28+
#define pii pair<int, int>
29+
#define len(v) int(v.size())
30+
#define all(v) v.begin(), v.end()
31+
#define reset(a, b) memset(a, b, sizeof(a));
32+
#define fr(i, s, n) for (int i = s ; i < n ; ++i)
33+
#define dfr(i, s, n) for (int i = s ; i > n ; --i)
34+
35+
template < typename T > void pr (const T& t) {
36+
cout << t << "\n";
37+
}
38+
39+
template < typename T, typename U, typename... ARGS >
40+
void pr (const T& t, const U& u, const ARGS&... args) {
41+
cout << t << " ";
42+
pr (u, args...);
43+
}
44+
45+
// clang-format on
46+
47+
const int N = 1e5 + 5;
48+
int arr[N];
49+
int bit[N];
50+
int n;
51+
52+
void update (int x, int val) {
53+
while (x <= n) {
54+
bit[x] += val;
55+
x += (x & (-x));
56+
}
57+
}
58+
59+
int query (int x) {
60+
int ans = 0;
61+
while (x > 0) {
62+
ans += bit[x];
63+
x -= (x & (-x));
64+
}
65+
return ans;
66+
}
67+
68+
int query (int l, int r) {
69+
return query (r) - query (l - 1);
70+
}
71+
72+
signed main () {
73+
ios_base::sync_with_stdio (false), cin.tie (nullptr);
74+
cin >> n;
75+
fr (i, 1, n + 1) {
76+
cin >> arr[i];
77+
arr[i] &= 1;
78+
if (!arr[i]) update (i, 1);
79+
}
80+
int tc;
81+
cin >> tc;
82+
while (tc--) {
83+
int t, x, y;
84+
cin >> t >> x >> y;
85+
if (t == 0) {
86+
y &= 1;
87+
if (arr[x] and !y) {
88+
update (x, 1);
89+
}
90+
if (!arr[x] and y) {
91+
update (x, -1);
92+
}
93+
arr[x] = y & 1;
94+
} else if (t == 1) {
95+
int ans = query (x, y);
96+
pr (ans);
97+
} else {
98+
int ans = query (x, y);
99+
pr (y - x + 1 - ans);
100+
}
101+
}
102+
return 0;
103+
}

0 commit comments

Comments
 (0)