diff options
author | Anthony Wang | 2022-04-19 15:13:09 -0500 |
---|---|---|
committer | Anthony Wang | 2022-04-19 15:13:09 -0500 |
commit | 03b09cc02a142e5029f42dc29a93e70588742468 (patch) | |
tree | 87d1c40ad83c09cc2d939d361638870a8251150f | |
parent | b95cdd56c2124f7545898e194ee8ad740b161678 (diff) |
Clean up code and fix a typo in segmenttree.c
-rw-r--r-- | cd.c | 34 | ||||
-rw-r--r-- | segmenttree.c | 4 |
2 files changed, 9 insertions, 29 deletions
@@ -33,35 +33,23 @@ int main(int argc, char* argv[]) { /* Connect to db */ sqlite3 *db; - int rc = sqlite3_open(file, &db); - if (rc) { - fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); - abort(); - } + sqlite3_open(file, &db); /* Get number of cards */ sqlite3_stmt *stmt; - rc = sqlite3_prepare_v3(db, "SELECT COUNT(*) FROM cards", -1, 0, &stmt, NULL); - if (rc) { - fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db)); - abort(); - } + sqlite3_prepare_v3(db, "SELECT COUNT(*) FROM cards", -1, 0, &stmt, NULL); sqlite3_step(stmt); int N = sqlite3_column_int(stmt, 0); sqlite3_finalize(stmt); /* Get card weights */ - rc = sqlite3_prepare_v3(db, "SELECT weight FROM cards", -1, 0, &stmt, NULL); - if (rc) { - fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db)); - abort(); - } - seg = malloc(4 * N * sizeof(int)); + sqlite3_prepare_v3(db, "SELECT weight FROM cards", -1, 0, &stmt, NULL); + seg = (int*)malloc(4 * N * sizeof(int)); build(stmt, 0, N - 1, 1); sqlite3_finalize(stmt); if (verbose) { - for (int i = 0; i < N; i++) { + for (int i = 0; i < 4 * N; i++) { printf("%d ", seg[i]); } printf("\n"); @@ -82,11 +70,7 @@ int main(int argc, char* argv[]) { } /* Get card contents from database */ - rc = sqlite3_prepare_v3(db, "SELECT key, val FROM cards WHERE idx=?", -1, 0, &stmt, NULL); - if (rc) { - fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db)); - abort(); - } + sqlite3_prepare_v3(db, "SELECT key, val FROM cards WHERE idx=?", -1, 0, &stmt, NULL); sqlite3_bind_int(stmt, 1, i); sqlite3_step(stmt); char *key, *val; @@ -114,11 +98,7 @@ int main(int argc, char* argv[]) { /* Update segment tree and database */ update(i, w, 0, N - 1, 1); - rc = sqlite3_prepare_v3(db, "UPDATE cards SET weight=? WHERE idx=?", -1, 0, &stmt, NULL); - if (rc) { - fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db)); - abort(); - } + sqlite3_prepare_v3(db, "UPDATE cards SET weight=? WHERE idx=?", -1, 0, &stmt, NULL); sqlite3_bind_int(stmt, 1, w); sqlite3_bind_int(stmt, 2, i); sqlite3_step(stmt); diff --git a/segmenttree.c b/segmenttree.c index 4471369..449f8e7 100644 --- a/segmenttree.c +++ b/segmenttree.c @@ -6,7 +6,7 @@ int * seg; void build(sqlite3_stmt *stmt, int l, int r, int n) { if (l == r) { sqlite3_step(stmt); - seg[l] = sqlite3_column_int(stmt, 0); + seg[n] = sqlite3_column_int(stmt, 0); return; } int m = l + r >> 1; @@ -45,4 +45,4 @@ void query(int *res, int v, int l, int r, int n) { else { query(res, v - seg[n << 1], m + 1, r, n << 1 | 1); } -}
\ No newline at end of file +} |