diff options
-rw-r--r-- | sd.go | 32 |
1 files changed, 25 insertions, 7 deletions
@@ -4,6 +4,7 @@ import ( "database/sql" "flag" "fmt" + "math/rand" "os" "os/exec" @@ -12,11 +13,14 @@ import ( var file = flag.String("f", "cards", "cards file") +var sum = 0 var seg = []int{} func build(a *sql.Rows, l int, r int, n int) { if l == r { + a.Next() a.Scan(&seg[n]) + sum += seg[n] return } m := (l + r) >> 1 @@ -27,7 +31,7 @@ func build(a *sql.Rows, l int, r int, n int) { func update(x int, v int, l int, r int, n int) { if l == r { - seg[n] += v + seg[n] = v return } m := (l + r) >> 1 @@ -41,7 +45,7 @@ func update(x int, v int, l int, r int, n int) { func query(v int, l int, r int, n int) (int, int) { if l == r { - return seg[n], n + return seg[n], l } m := (l + r) >> 1 if seg[n<<1] < v { @@ -65,7 +69,7 @@ func main() { seg = make([]int, 4*N) // Build segment tree - rows, err := db.Query("SELECT * FROM cards") + rows, err := db.Query("SELECT weight FROM cards") if err != nil { panic(err) } @@ -80,16 +84,30 @@ func main() { defer exec.Command("stty", "-F", "/dev/tty", "echo").Run() for { - fmt.Println("hello world") + // Choose a random card + x := rand.Intn(sum) + fmt.Println(x) + w, i := query(x, 0, N-1, 1) + fmt.Println(w) + fmt.Println(i) + + var key, val string + db.QueryRow("SELECT * FROM cards WHERE idx=?", i).Scan(&i, &w, &key, &val) + fmt.Println(key) + fmt.Println(val) var b []byte = make([]byte, 1) os.Stdin.Read(b) if b[0] == byte('y') { - + w >>= 1 + sum -= w } else if b[0] == byte('n') { - + sum += w + w <<= 1 } else { - + os.Exit(0) } + update(i, w, 0, N-1, 1) + db.Query("UPDATE cards SET weight=? WHERE idx=?", w, i) } } |