Skip to content

Commit 54451ee

Browse files
committed
.
1 parent 602f9c1 commit 54451ee

10 files changed

+518
-212
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"Playing with digits","group":"HackerEarth","url":"https://www.hackerearth.com/problem/algorithm/playing-with-digits-4e25844f/","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"input":"5 86 4\n","output":"7\n","id":1618372966642}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"PlayingWithDigits"}},"batch":{"id":"2bfe465b-a194-49fb-b80b-5a0510776f00","size":1},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Playing_with_digits.cpp"}
1+
{"name":"Playing with digits","group":"HackerEarth","url":"https://www.hackerearth.com/problem/algorithm/playing-with-digits-4e25844f/","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1618372966642,"input":"5 86 4","output":"7"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"PlayingWithDigits"}},"batch":{"id":"2bfe465b-a194-49fb-b80b-5a0510776f00","size":1},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Playing_with_digits.cpp"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Prime Time","group":"Google Coding Competitions - Round 1A 2021 - Code Jam 2021","url":"https://codingcompetitions.withgoogle.com/codejam/round/000000000043585d/00000000007543d8","interactive":false,"memoryLimit":1024,"timeLimit":45000,"tests":[{"id":1618375783450,"input":"4\n5\n2 2\n3 1\n5 2\n7 1\n11 1\n1\n17 2\n2\n2 2\n3 1\n1\n2 7","output":"Case #1: 25\nCase #2: 17\nCase #3: 0\nCase #4: 8"}],"testType":"multiNumber","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Solution","taskClass":"PrimeTime"}},"batch":{"id":"c2f72542-f2ad-4acf-b9d6-8f62b9f79036","size":1},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Prime_Time.cpp"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"C. Number of Minimums on a Segment","group":"Codeforces - ITMO Academy: pilot course - Segment Tree, part 1 - Step 1","url":"https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/C","interactive":false,"memoryLimit":1024,"timeLimit":1000,"tests":[{"id":1618482535275,"input":"5 5\n3 4 3 5 2\n2 0 3\n1 1 2\n2 0 3\n1 0 2\n2 0 5","output":"3 2\n2 1\n2 3"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CNumberOfMinimumsOnASegment"}},"batch":{"id":"4e2fc343-76ce-4d64-a913-e1eb882a898c","size":1},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\CodeForces Edu\\Segment Tree 1\\C_Number_of_Minimums_on_a_Segment.cpp"}
9.58 KB
Binary file not shown.

Practice/CodeForces Edu/Segment Tree 1/A_Segment_Tree_for_the_Sum.cpp

Lines changed: 61 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -18,171 +18,116 @@ using namespace std;
1818
int T; \
1919
cin >> T; \
2020
while (T--)
21-
#define int long long int
21+
// #define int long long int
2222
#define mod 1000000007ll
2323
#define endl "\n"
24-
//------------------------------------------------------------------------------
25-
// Any fucntion can be called using Math.function_name();
26-
//------------------------------------------------------------------------------
27-
class Math
28-
{
29-
public:
30-
//Returns gcd of two numbers
31-
int gcd(int a, int b)
32-
{
33-
return (a % b == 0) ? b : gcd(b, a % b);
34-
}
3524

36-
//Returns lcm of two numbers
37-
int lcm(int a, int b)
38-
{
39-
return a * (b / gcd(a, b));
40-
}
41-
42-
// Returns flag array isPrime
43-
// isPrime[i] = true (if i is Prime)
44-
// isPrime[i] = false (if i is not Prime)
45-
vector<bool> *seiveOfEratosthenes(const int N)
46-
{
47-
vector<bool> *isPrime = new vector<bool>(N + 1, true);
48-
(*isPrime)[0] = (*isPrime)[1] = false;
49-
for (int i = 2; i * i <= N; ++i)
50-
if ((*isPrime)[i])
51-
for (int j = i * i; j <= N; j += i)
52-
(*isPrime)[j] = false;
53-
54-
return isPrime;
55-
}
56-
57-
//Returns (x ^ n)
58-
int pow(const int &x, int n)
59-
{
60-
if (n == 0)
61-
return 1;
62-
int h = pow(x, n / 2);
63-
return (n & 1) ? h * h * x : h * h;
64-
}
65-
66-
//Returns (x ^ n) % M
67-
int pow(const int &x, int n, const int &M)
68-
{
69-
if (n == 0)
70-
return 1;
71-
int h = pow(x, n / 2) % M;
72-
return (n & 1) ? (h * h * x) % M : (h * h) % M;
73-
}
74-
75-
//Returns all Primes <= N
76-
vector<int> *primesUptoN(const int N)
77-
{
78-
vector<bool> *isPrime = seiveOfEratosthenes(N);
79-
vector<int> *Primes = new vector<int>;
80-
if (2 <= N)
81-
(*Primes).push_back(2);
82-
for (int i = 3; i <= N; i += 2)
83-
if ((*isPrime)[i])
84-
(*Primes).push_back(i);
85-
return Primes;
86-
}
87-
88-
} Math;
89-
//------------------------------------------------------------------------------
25+
struct Node
26+
{
27+
int val = 0;
28+
};
9029

91-
struct SegTree
30+
struct SegmentTree
9231
{
93-
v(int) t;
32+
private:
33+
v(Node) tree;
9434
int size;
35+
Node neutralValue;
36+
Node base;
9537

96-
int init = 0;
97-
98-
int f(int &a, int &b)
38+
Node merge(Node &a, Node &b)
9939
{
100-
return a + b;
101-
}
102-
103-
SegTree(v(int) & A)
104-
{
105-
size = 1;
106-
int n = A.size();
107-
while (size < n)
108-
size *= 2;
109-
t.assign(2 * size, INT32_MAX);
110-
build(A, 0, 0, size);
40+
Node ans;
41+
ans.val = a.val + b.val;
42+
return ans;
11143
}
11244

11345
void build(v(int) & A, int x, int lx, int rx)
11446
{
47+
11548
if (rx - lx == 1)
11649
{
11750
if (lx < A.size())
118-
t[x] = A[lx];
51+
{
52+
base.val = A[lx];
53+
tree[x] = base;
54+
}
11955
return;
12056
}
12157

122-
int m = (lx + rx) >> 1;
123-
124-
build(A, 2 * x + 1, lx, m);
125-
build(A, 2 * x + 2, m, rx);
58+
int mx = (lx + rx) >> 1;
59+
build(A, (2 * x) + 1, lx, mx);
60+
build(A, (2 * x) + 2, mx, rx);
12661

127-
t[x] = f(t[2 * x + 1], t[2 * x + 2]);
62+
tree[x] = merge(tree[(2 * x) + 1], tree[(2 * x) + 2]);
12863
}
12964

130-
void set(int &i, int &v, int x, int lx, int rx)
65+
void update(int &idx, int &val, int x, int lx, int rx)
13166
{
67+
13268
if (rx - lx == 1)
13369
{
134-
t[x] = v;
70+
base.val = val;
71+
tree[x] = base;
13572
return;
13673
}
13774

138-
int m = (lx + rx) >> 1;
75+
int mx = (lx + rx) >> 1;
13976

140-
if (i < m)
141-
set(i, v, 2 * x + 1, lx, m);
77+
if (idx < mx)
78+
update(idx, val, (2 * x) + 1, lx, mx);
14279
else
143-
set(i, v, 2 * x + 2, m, rx);
80+
update(idx, val, (2 * x) + 2, mx, rx);
14481

145-
t[x] = f(t[2 * x + 1], t[2 * x + 2]);
82+
tree[x] = merge(tree[(2 * x) + 1], tree[(2 * x) + 2]);
14683
}
14784

148-
int query(int &l, int &r, int x, int lx, int rx)
85+
Node query(int &l, int &r, int x, int lx, int rx)
14986
{
15087
if (lx >= r or l >= rx)
151-
return init;
152-
153-
if (l <= lx and rx <= r)
154-
return t[x];
88+
return neutralValue;
15589

156-
int m = (lx + rx) >> 1;
90+
if (rx - lx == 1)
91+
return tree[x];
15792

158-
int ans1 = query(l, r, 2 * x + 1, lx, m);
159-
int ans2 = query(l, r, 2 * x + 2, m, rx);
93+
int mx = (lx + rx) >> 1;
94+
Node ans1 = query(l, r, 2 * x + 1, lx, mx);
95+
Node ans2 = query(l, r, 2 * x + 2, mx, rx);
96+
Node ans = merge(ans1, ans2);
97+
return ans;
98+
}
16099

161-
return f(ans1, ans2);
100+
public:
101+
SegmentTree(v(int) & A)
102+
{
103+
size = 1;
104+
int n = A.size();
105+
while (size < n)
106+
size *= 2;
107+
tree.assign((2 * size) + 1, neutralValue);
108+
build(A, 0, 0, size);
162109
}
163110

164-
void set(int &i, int &v)
111+
void update(int &idx, int &val)
165112
{
166-
set(i, v, 0, 0, size);
113+
update(idx, val, 0, 0, size);
167114
}
168115

169-
int query(int &l, int &r)
116+
Node query(int &l, int &r)
170117
{
171118
return query(l, r, 0, 0, size);
172119
}
173120
};
174121

122+
//------------------------------------------------------------------------------
175123
void solve()
176124
{
177125
int n, q;
178126
cin >> n >> q;
179-
180127
v(int) A(n);
181-
for (int &x : A)
182-
cin >> x;
183-
184-
SegTree st(A);
185-
128+
for (int i = 0; i < n; i++)
129+
cin >> A[i];
130+
SegmentTree st(A);
186131
while (q--)
187132
{
188133
int op;
@@ -192,13 +137,14 @@ void solve()
192137
{
193138
int i, v;
194139
cin >> i >> v;
195-
st.set(i, v);
140+
st.update(i, v);
196141
}
197142
else
198143
{
199144
int l, r;
200145
cin >> l >> r;
201-
cout << st.query(l, r) << endl;
146+
Node ans = st.query(l, r);
147+
cout << ans.val << endl;
202148
}
203149
}
204150
}
10.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)