Skip to content

Commit 526e2a7

Browse files
Add files via upload
1 parent a989546 commit 526e2a7

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
Alright, I see the question and the first thing that comes into my mind is lazy propagation !
3+
4+
However, I saw the comments and people said that there's a simpler solution.
5+
6+
And then I made an observation.
7+
8+
Have an array -
9+
10+
No of Updates Starting Here -- U
11+
12+
For every update, U[l]++, U[r + 1]--;
13+
14+
To construct the final array,
15+
16+
A[i] = A[i - 1] + U[i]
17+
18+
If 10 new updates start at position i, then all of them will be added to A[i], in addition to propagating the updates that affected A[i - 1].
19+
Of course, if an update ends, then it will neutralise each other.
20+
21+
---------------------------------------------------
22+
23+
void solve()
24+
{
25+
int no_of_elements, no_of_updates;
26+
scanf("%d %d", &no_of_elements, &no_of_updates);
27+
28+
vector <int> updates_starting_here(no_of_elements + 2, 0);
29+
while(no_of_updates--)
30+
{
31+
int left, right, value;
32+
scanf("%d %d %d", &left, &right, &value);
33+
34+
updates_starting_here[left + 1] += value;
35+
updates_starting_here[right + 2]-= value;
36+
}
37+
38+
vector <int> element(no_of_elements + 1, 0);
39+
for(int i = 1; i <= no_of_elements; i++)
40+
element[i] = element[i - 1] + updates_starting_here[i];
41+
42+
int no_of_queries;
43+
scanf("%d", &no_of_queries);
44+
45+
while(no_of_queries--)
46+
{
47+
int i;
48+
scanf("%d", &i);
49+
50+
printf("%d\n", element[i + 1]);
51+
}
52+
}

0 commit comments

Comments
 (0)