aboutsummaryrefslogtreecommitdiff
path: root/segmenttree.c
diff options
context:
space:
mode:
Diffstat (limited to 'segmenttree.c')
-rw-r--r--segmenttree.c42
1 files changed, 22 insertions, 20 deletions
diff --git a/segmenttree.c b/segmenttree.c
index 1bf0fcd..4471369 100644
--- a/segmenttree.c
+++ b/segmenttree.c
@@ -1,46 +1,48 @@
-struct SegmentTree {
- int N;
- int * seg;
-}
+#include <sqlite3.h>
+
+int * seg;
-// Build segment tree
-void Build(int l, int r, int n) {
+/* Build segment tree */
+void build(sqlite3_stmt *stmt, int l, int r, int n) {
if (l == r) {
-
+ sqlite3_step(stmt);
+ seg[l] = sqlite3_column_int(stmt, 0);
+ return;
}
int m = l + r >> 1;
- Build(l, m, n << 1);
- Build(m + 1, r, n << 1 | 1);
+ build(stmt, l, m, n << 1);
+ build(stmt, m + 1, r, n << 1 | 1);
seg[n] = seg[n << 1] + seg[n << 1 | 1];
}
-// Update value at index x
-void Update(int x, int v, int l, int r, int n) {
+/* Update value at index x*/
+void update(int x, int v, int l, int r, int n) {
if (l == r) {
seg[n] += v;
return;
}
int m = l + r >> 1;
if (x <= m) {
- Update(x, v, l, m, n<<1);
+ update(x, v, l, m, n<<1);
}
else {
- Update(x, v, m+1, r, n<<1|1);
+ update(x, v, m+1, r, n<<1|1);
}
seg[n] = seg[n << 1] + seg[n << 1 | 1];
}
-// Find element with prefix sum v
-int Query(int v, int l, int r, int n) {
+/* Find element with prefix sum v */
+void query(int *res, int v, int l, int r, int n) {
if (l == r) {
- return s.seg[n], l;
+ res[0] = seg[n];
+ res[1] = l;
+ return;
}
int m = l + r >> 1;
if (seg[n << 1] >= v) {
- return Query(v, l, m, n << 1);
+ query(res, v, l, m, n << 1);
}
else {
- return Query(v - s.seg[n << 1], m + 1, r, n << 1 | 1);
+ query(res, v - seg[n << 1], m + 1, r, n << 1 | 1);
}
-}
-
+} \ No newline at end of file