Skip to content

Commit a568b9a

Browse files
authored
Merge branch 'master' into fix/cargo-fmt
2 parents 459bd7c + 432e783 commit a568b9a

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

.github/workflows/ci.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: CI
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * 6'
6+
push:
7+
branches:
8+
- '*'
9+
pull_request:
10+
11+
jobs:
12+
rustfmt:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
toolchain:
17+
- 1.42.0-x86_64-unknown-linux-gnu
18+
- stable-x86_64-unknown-linux-gnu
19+
20+
name: Rustfmt (${{ matrix.toolchain }})
21+
runs-on: ubuntu-18.04
22+
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v2
26+
27+
- name: 'Setup `${{ matrix.toolchain }}`'
28+
uses: actions-rs/toolchain@v1
29+
with:
30+
toolchain: ${{ matrix.toolchain }}
31+
override: true
32+
profile: minimal
33+
components: rustfmt
34+
35+
- name: cargo-fmt
36+
uses: actions-rs/cargo@v1
37+
with:
38+
command: fmt
39+
args: --all -- --check
40+
41+
build:
42+
strategy:
43+
fail-fast: false
44+
matrix:
45+
toolchain:
46+
- 1.42.0-x86_64-pc-windows-msvc
47+
- 1.42.0-x86_64-apple-darwin
48+
- 1.42.0-x86_64-unknown-linux-gnu
49+
- stable-x86_64-pc-windows-msvc
50+
- stable-x86_64-apple-darwin
51+
- stable-x86_64-unknown-linux-gnu
52+
include:
53+
- { toolchain: 1.42.0-x86_64-pc-windows-msvc , os: windows-2019 }
54+
- { toolchain: 1.42.0-x86_64-apple-darwin , os: macos-10.15 }
55+
- { toolchain: 1.42.0-x86_64-unknown-linux-gnu, os: ubuntu-18.04 }
56+
- { toolchain: stable-x86_64-pc-windows-msvc , os: windows-2019 }
57+
- { toolchain: stable-x86_64-apple-darwin , os: macos-10.15 }
58+
- { toolchain: stable-x86_64-unknown-linux-gnu, os: ubuntu-18.04 }
59+
60+
name: Build (${{ matrix.toolchain }})
61+
runs-on: ${{ matrix.os }}
62+
63+
steps:
64+
- name: Disable `core.autocrlf`
65+
run: git config --global core.autocrlf false
66+
if: matrix.os == 'windows-2019'
67+
68+
- name: Checkout
69+
uses: actions/checkout@v2
70+
71+
- name: 'Setup `${{ matrix.toolchain }}`'
72+
uses: actions-rs/toolchain@v1
73+
with:
74+
toolchain: ${{ matrix.toolchain }}
75+
override: true
76+
profile: minimal
77+
components: clippy
78+
79+
- name: cargo-clippy
80+
uses: actions-rs/cargo@v1
81+
with:
82+
command: clippy
83+
args: --workspace --all-targets -- -D warnings
84+
85+
- name: cargo-build
86+
uses: actions-rs/cargo@v1
87+
with:
88+
command: build
89+
args: --workspace --all-targets
90+
91+
- name: cargo-test
92+
uses: actions-rs/cargo@v1
93+
with:
94+
command: test
95+
args: --workspace --no-fail-fast
96+
env:
97+
RUST_BACKTRACE: full

src/fenwicktree.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
1+
// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
2+
pub struct FenwickTree<T> {
3+
n: usize,
4+
ary: Vec<T>,
5+
e: T,
6+
}
17

8+
impl<T: Clone + std::ops::AddAssign<T>> FenwickTree<T> {
9+
pub fn new(n: usize, e: T) -> Self {
10+
FenwickTree {
11+
n: n,
12+
ary: vec![e.clone(); n],
13+
e: e,
14+
}
15+
}
16+
pub fn accum(&self, mut idx: usize) -> T {
17+
let mut sum = self.e.clone();
18+
while idx > 0 {
19+
sum += self.ary[idx - 1].clone();
20+
idx &= idx - 1;
21+
}
22+
sum
23+
}
24+
/// performs data[idx] += val;
25+
pub fn add<U: Clone>(&mut self, mut idx: usize, val: U)
26+
where
27+
T: std::ops::AddAssign<U>,
28+
{
29+
let n = self.n;
30+
idx += 1;
31+
while idx <= n {
32+
self.ary[idx - 1] += val.clone();
33+
idx += idx & idx.wrapping_neg();
34+
}
35+
}
36+
/// Returns data[l] + ... + data[r - 1].
37+
pub fn sum(&self, l: usize, r: usize) -> T
38+
where
39+
T: std::ops::Sub<Output = T>,
40+
{
41+
self.accum(r) - self.accum(l)
42+
}
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn fenwick_tree_works() {
51+
let mut bit = FenwickTree::new(5, 0i64);
52+
// [1, 2, 3, 4, 5]
53+
for i in 0..5 {
54+
bit.add(i, i as i64 + 1);
55+
}
56+
assert_eq!(bit.sum(0, 5), 15);
57+
assert_eq!(bit.sum(0, 4), 10);
58+
assert_eq!(bit.sum(1, 3), 5);
59+
}
60+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ mod scc;
1010
mod segtree;
1111
mod string;
1212
mod twosat;
13+
14+
pub use fenwicktree::FenwickTree;

0 commit comments

Comments
 (0)