Skip to content

Commit ba0ce0e

Browse files
Merge pull request #247 from H4wk-3yE/patch-3
Create round_numbers.cpp
2 parents 65ab58f + e2ad6a1 commit ba0ce0e

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

fenwick_tree/round_numbers.cpp

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

0 commit comments

Comments
 (0)