File size: 1,807 Bytes
bff4ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*@ Import Lean
import Data_structures.binary_indexed_tree.lean.spec_lib
open scoped SimpleC
*/

/*@ Extern Coq
      (FenwickLowbit : Z -> Z)
      (FenwickNodeLo : Z -> Z)
      (FenwickNodeSum : list Z -> Z -> Z)
      (FenwickPrefixSum : list Z -> Z -> Z)
      (FenwickAddArray : list Z -> Z -> Z -> list Z)
      (FenwickRep : list Z -> list Z -> Z -> Prop)
      (FenwickIntervalsIntSafe : list Z -> Z -> Prop)
      (FenwickAddProgress : list Z -> list Z -> Z -> Z -> Z -> Z -> Prop)
      (FenwickQueryState : list Z -> Z -> Z -> Z -> Prop)
 */


int lowbit(int x)
/*@ Require 1 <= x && x <= INT_MAX
    Ensure
      __return == FenwickLowbit(x) &&
      1 <= __return && __return <= x
 */
{
  return x & (-x);
}

void add(int *bit, int n, int pos, int delta)
/*@ With (a : list Z) (bit_l : list Z)
    Require
      1 <= n && 2 * n <= INT_MAX &&
      1 <= pos && pos <= n &&
      FenwickRep(a, bit_l, n) &&
      FenwickIntervalsIntSafe(a, n) &&
      FenwickIntervalsIntSafe(FenwickAddArray(a, pos, delta), n) &&
      IntArray::full(bit, n + 1, bit_l)
    Ensure
      exists bit_l1,
        FenwickRep(FenwickAddArray(a, pos, delta), bit_l1, n) &&
        Znth(0, bit_l1, 0) == Znth(0, bit_l, 0) &&
        IntArray::full(bit, n + 1, bit_l1)
 */
{

  while (pos <= n) {
    bit[pos] += delta;
    pos += lowbit(pos);
  }
}

int query(int *bit, int pos)
/*@ With (a : list Z) (bit_l : list Z) (n : Z)
    Require
      1 <= n && n <= INT_MAX &&
      0 <= pos && pos <= n &&
      FenwickRep(a, bit_l, n) &&
      FenwickIntervalsIntSafe(a, n) &&
      IntArray::full(bit, n + 1, bit_l)
    Ensure
      __return == FenwickPrefixSum(a, pos) &&
      IntArray::full(bit, n + 1, bit_l)
 */
{
  int sum = 0;

  while (pos > 0) {
    sum += bit[pos];
    pos -= lowbit(pos);
  }

  return sum;
}