|
| 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