Skip to content

Commit 378e803

Browse files
committed
add proper identation to code to issue #422
1 parent 74ba991 commit 378e803

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

segment_tree/segment_tree.c

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,73 @@
1010

1111
ll min(ll a, ll b) //min function to compute minimum of 2 values
1212
{
13-
if (a < b)
14-
return a;
15-
return b;
13+
if (a < b)
14+
return a;
15+
return b;
1616
}
1717
void build(ll left, ll right, ll tree[], ll arr[], ll index) //to recursively contruct the segment tree in O(n)
1818
{
19-
if (left == right)
20-
{
21-
tree[index] = arr[left];
22-
return;
23-
}
24-
else
25-
{
26-
ll mid = (left + right) / 2;
27-
build(left, mid, tree, arr, 2 * index + 1); //to build the left subtree
28-
build(mid + 1, right, tree, arr, 2 * index + 2); //to build the right subtree
29-
tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]); // to build the parent node of left and right subtree.
30-
return;
31-
}
19+
if (left == right)
20+
{
21+
tree[index] = arr[left];
22+
return;
23+
}
24+
else
25+
{
26+
ll mid = (left + right) / 2;
27+
build(left, mid, tree, arr, 2 * index + 1); //to build the left subtree
28+
build(mid + 1, right, tree, arr, 2 * index + 2); //to build the right subtree
29+
tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]); // to build the parent node of left and right subtree.
30+
return;
31+
}
3232
}
3333
void update(ll left, ll right, ll tree[], ll arr[], ll index, ll ind, ll value) //to update a given value at a particular index in O(logn)
3434
{
35-
if (right < ind || left > ind)
36-
return;
35+
if (right < ind || left > ind)
36+
return;
3737

38-
if (left == right && left == ind)
39-
{
40-
tree[index] = value;
41-
return;
42-
}
38+
if (left == right && left == ind)
39+
{
40+
tree[index] = value;
41+
return;
42+
}
4343

44-
ll mid = (left + right) / 2;
45-
update(left, mid, tree, arr, 2 * index + 1, ind, value);
46-
update(mid + 1, right, tree, arr, 2 * index + 2, ind, value);
47-
tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]);
44+
ll mid = (left + right) / 2;
45+
update(left, mid, tree, arr, 2 * index + 1, ind, value);
46+
update(mid + 1, right, tree, arr, 2 * index + 2, ind, value);
47+
tree[index] = min(tree[2 * index + 1], tree[2 * index + 2]);
4848
}
4949
ll ans(ll left, ll right, ll initial, ll final, ll tree[], ll arr[], ll index) // to provide the minimum value within a given range in O(logn)
5050
{
51-
if (left <= initial && right >= final)
52-
return tree[index];
53-
if (right < initial || left > final)
54-
return 1e18;
51+
if (left <= initial && right >= final)
52+
return tree[index];
53+
if (right < initial || left > final)
54+
return 1e18;
5555

56-
ll mid = (initial + final) / 2;
57-
return min(ans(left, right, initial, mid, tree, arr, 2 * index + 1), ans(left, right, mid + 1, final, tree, arr, 2 * index + 2));
56+
ll mid = (initial + final) / 2;
57+
return min(ans(left, right, initial, mid, tree, arr, 2 * index + 1), ans(left, right, mid + 1, final, tree, arr, 2 * index + 2));
5858
}
5959
int main()
6060
{
6161

62-
ll tc = 1;
62+
ll tc = 1;
6363

64-
while (tc--)
65-
{
66-
ll n;
64+
while (tc--)
65+
{
66+
ll n;
6767

68-
ll arr[] = {9, 4, 5, 3, 6, 2, 10};
69-
n = 7;
68+
ll arr[] = {9, 4, 5, 3, 6, 2, 10};
69+
n = 7;
7070

71-
ll tree[12] = {0}; //to contruct a seg tree in the form of array
72-
build(0, n - 1, tree, arr, 0); //to construct segment tree in O(n)
71+
ll tree[12] = {0}; //to contruct a seg tree in the form of array
72+
build(0, n - 1, tree, arr, 0); //to construct segment tree in O(n)
7373

74-
ll l = 1, r = 5;
75-
printf("the min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0));
74+
ll l = 1, r = 5;
75+
printf("the min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0));
7676

77-
ll ind = 3, value = -20;
78-
update(0, n - 1, tree, arr, 0, ind - 1, value); //to update the value at index ind in array,thereby updating whole tree
77+
ll ind = 3, value = -20;
78+
update(0, n - 1, tree, arr, 0, ind - 1, value); //to update the value at index ind in array,thereby updating whole tree
7979

80-
printf("the new min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0));
81-
}
80+
printf("the new min value in the range 1 to 5 inclusive is:%lld\n", ans(l - 1, r - 1, 0, n - 1, tree, arr, 0));
81+
}
8282
}

0 commit comments

Comments
 (0)