File size: 2,699 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*@ 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)
 */
{
  /*@ Inv Assert
      exists bit_cur,
        bit == bit@pre && n == n@pre && delta == delta@pre &&
        1 <= n@pre && 2 * n@pre <= INT_MAX &&
        1 <= pos@pre && pos@pre <= n@pre &&
        1 <= pos && pos <= 2 * n@pre &&
        FenwickRep(a, bit_l, n@pre) &&
        FenwickIntervalsIntSafe(a, n@pre) &&
        FenwickIntervalsIntSafe(
          FenwickAddArray(a, pos@pre, delta@pre), n@pre) &&
        FenwickAddProgress(
          bit_l, bit_cur, n@pre, pos@pre, pos, delta@pre) &&
        IntArray::full(bit@pre, n@pre + 1, bit_cur)
   */
  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;

  /*@ Inv Assert
      bit == bit@pre &&
      1 <= n && n <= INT_MAX &&
      0 <= pos && pos <= pos@pre && pos@pre <= n &&
      INT_MIN <= sum && sum <= INT_MAX &&
      FenwickRep(a, bit_l, n) &&
      FenwickIntervalsIntSafe(a, n) &&
      FenwickQueryState(a, pos@pre, pos, sum) &&
      IntArray::full(bit@pre, n + 1, bit_l)
   */
  while (pos > 0) {
    sum += bit[pos];
    pos -= lowbit(pos);
  }

  return sum;
}