Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

make language consistent#1563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Sosuke23 wants to merge1 commit intocp-algorithms:main
base:main
Choose a base branch
Loading
fromSosuke23:main
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletionssrc/data_structures/fenwick.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -431,30 +431,37 @@ Thus we can use $B_2$ for shaving off extra terms when we multiply $B_1[i]\times

We can find arbitrary range sums by computing the prefix sums for $l-1$ and $r$ and taking the difference of them again.

```python
def add(b, idx, x):
while idx <= N:
b[idx] += x
idx += idx & -idx
```cpp
void add(std::vector<int> b, int idx, int x) {
while (idx <= n) {
b[idx] += x;
idx += idx & -idx;
}
}

def range_add(l,r,x):
void range_add(int l, int r, int x) {
add(B1, l, x)
add(B1, r+1, -x)
add(B2, l, x*(l-1))
add(B2, r+1, -x*r)
add(B1, r + 1, -x)
add(B2, l, x * (l - 1))
add(B2, r + 1, -x * r)
}

def sum(b, idx):
total = 0
while idx > 0:
total += b[idx]
idx -= idx & -idx
return total
int sum(std::vector<int> b, int idx) {
int total = 0;
while (idx > 0) {
total += b[idx];
idx -= idx & -idx;
}
return total;
}

def prefix_sum(idx):
return sum(B1, idx)*idx - sum(B2, idx)
int prefix_sum(int idx) {
return sum(B1, idx) * idx - sum(B2, idx);
}

def range_sum(l, r):
return prefix_sum(r) - prefix_sum(l-1)
int range_sum(int l, int r) {
return prefix_sum(r) - prefix_sum(l - 1);
}
```

## Practice Problems
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp