Skip to content

Commit 3dcba5b

Browse files
Add files via upload
1 parent 7ca4605 commit 3dcba5b

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
3+
----------------------------------------------------------
4+
5+
Keep track of the number of lights on in the current interval. Use lazy propagation.
6+
7+
void build(int node, int start, int end)
8+
{
9+
if(start == end)
10+
{
11+
tree[node] = light[start];
12+
return;
13+
}
14+
15+
int mid = (start + end)/2;
16+
build(2*node, start, mid);
17+
build(2*node + 1, mid + 1, end);
18+
19+
tree[node] = tree[2*node] + tree[2*node + 1];
20+
}
21+
22+
T(n) = T(2n) + T(2n + 1)
23+
24+
---------------------------------------------------------------
25+
26+
void propagate(int node, int start, int end)
27+
{
28+
int on_lights = tree[node];
29+
int total_lights = end - (start - 1);
30+
int off_lights = total_lights - on_lights;
31+
32+
if(lazy[node]%2 == 1)
33+
tree[node] = off_lights;
34+
35+
if(start != end)
36+
{
37+
lazy[2*node] += lazy[node];
38+
lazy[2*node + 1] += lazy[node];
39+
}
40+
41+
lazy[node] = 0;
42+
}
43+
44+
If the number of pending operations is odd, then flip all the switches.
45+
46+
-------------------------------------------------------------------------------------
47+
48+
void update(int node, int start, int end, int query_start, int query_end)
49+
{
50+
if(lazy[node] != 0)
51+
propagate(node, start, end);
52+
53+
if(query_start > end || query_end < start)
54+
return;
55+
56+
if(query_start <= start && end <= query_end)
57+
{
58+
int no_of_lights = end - (start - 1);
59+
int no_of_on_lights = tree[node];
60+
int no_of_off_lights = no_of_lights - no_of_on_lights;
61+
62+
tree[node] = no_of_off_lights;
63+
64+
if(start != end)
65+
{
66+
lazy[2*node]++;
67+
lazy[2*node + 1]++;
68+
}
69+
return;
70+
}
71+
72+
int mid = (start + end)/2;
73+
74+
update(2*node, start, mid, query_start, query_end);
75+
update(2*node + 1, mid + 1, end, query_start, query_end);
76+
77+
tree[node] = tree[2*node] + tree[2*node + 1];
78+
}
79+
80+
Make sure to write T(n) = T(2n) + T(2n + 1) at the end. It's important for the changes to go upward as well
81+
Intervals that fit completely inside or outside the interval we are interested in don't need that recurrence relation at the end.
82+
We only need to do lazy propagation till there.
83+
84+
-----------------------------------------------------------------------------------------
85+
86+
int query(int node, int start, int end, int query_start, int query_end)
87+
{
88+
if(lazy[node] != 0)
89+
propagate(node, start, end);
90+
91+
if(query_start > end || query_end < start)
92+
return 0;
93+
94+
if(query_start <= start && end <= query_end)
95+
return tree[node];
96+
97+
int mid = (start + end)/2;
98+
99+
int left_answer = query(2*node, start, mid, query_start, query_end);
100+
int right_answer = query(2*node + 1, mid + 1, end, query_start, query_end);
101+
102+
return (left_answer + right_answer);
103+
}
104+
105+
Make sure the laziness is propagated before querying.
106+

0 commit comments

Comments
 (0)