Skip to content

Commit

Permalink
tomato
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun Nawathey committed Apr 5, 2024
1 parent 8abd2f5 commit a9a291a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
2 changes: 1 addition & 1 deletion _posts/2024-03-23-march23.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can greedily do this. Check subtrees in increasing order of their size (smal
You can check tries in this order with depth first search. It's in the name: depth first.


```
```c++
int check(int u, int x, int& num_components) {
int cnt = 1;

Expand Down
81 changes: 81 additions & 0 deletions _posts/2024-04-04-april4.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
layout: post
title: "cses Range Updates and Sums"
date: 2024-04-04 03:13:00 -0700
---
#problem](https://cses.fi/problemset/task/1735/)
[my submission](https://cses.fi/paste/4f4b13d8100becce8830e1/)

The idea is to use a lazy segment tree to get the sum of ranges in log N time complexity.

```c++
int A[N];
long long t[N << 2];
```
The basic segment tree idea is that the sum of a vertex's segment is equal to the sum of vertex's children's segments.
This is made clear in a segment tree's construction.
```c++
void build(int i, int l, int r) {
if (l == r) {
t[i] = A[l];
}
else {
int m = l + (r - l) / 2;

build(i<<1, l, m);
build(i<<1|1, m + 1, r);
t[i] = t[i<<1] + t[i<<1|1];
}
}
```
An eager as opposed to lazy update on a segment tree might affect many ranges, degrading the log N time complexity of operations.
For example, I could 1 to every element in the array would require the entire tree to be calculated again.
You can think of a lazy update to a range as completed for that range, but pending for its sub-range. Prior to any descent into those subranges,
you should propagate the changes.
For example, say I add 1 to all values in the array. I can increase the segment tree's root sum and remember that 1 was added to all values with a lazy tag.
Any reads to that exact range will just return the range's sum. But what if I want the sum of a range within the one I just lazily updated?
Ignore the set operation for now. See the new lazy add member added to each vertex.
```c++
struct node {
ll sum;
ll lz_add;
node() {}
} t[N << 2];
```

```c++
void add(int i, int l, int r, int a, int b, ll x) {
if (a > b) {
return;
}
else if (l == a && r == b) {
t[i].lz_add += x;
t[i].sum += x * (r - l + 1);
}
else {
// 1. what should be done right here?

int m = l + (r - l) / 2;
add(i<<1, l, m, a, min(b, m), x);
add(i<<1|1, m + 1, r, max(a, m + 1), b, x);

// 2. and what should be done here?
}
}
```
1. Prior to any descent into those subranges, you have to propagate the changes you made and clear the lazy tag.
2. Since you are going to update a lower range lazily as well, the segment tree invariant needs to be upholded.
item 2 is quite simple:
```c++
t[i] = t[i<<1] + t[i<<1|1];
```

I will leave item 1 to you. Look at my submission or the usaco guide if you are confused.
[USACO guide](https://usaco.guide/plat/RURQ)
7 changes: 2 additions & 5 deletions about.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ On top of problem editorials, I now post updates on projects and ideas I have.

March 25, 2024
Now I am seriously trying to push my codeforces rank.
This is me on codeforces:
[thejamba](https://codeforces.com/profile/thejamba)

This is me on codeforces: [thejamba](https://codeforces.com/profile/thejamba)
Here's a little about me:
my name is Varun. I study computer science at UCI. I also interned at Intel for some time
Also, right now, I am in London on vacation.
my name is Varun. I study computer science at UCI. I also interned at Intel for some time.

0 comments on commit a9a291a

Please sign in to comment.