Skip to content

Instantly share code, notes, and snippets.

Binary Indexed Trees and Segment Trees

The Problem

We want to have a data structure that allows us to do two things

  1. Make queries of the form f(a[i], ..., a[j]) for i <= j (the query)
  2. Update a[i] = v for some i (the update)

Naively, one could do this in such a way that either the query or the update is O(1) and the other is O(n). The most basic method would be to simply store a[i] and then for the query, simply compute the function in O(n). However, that will obviously not be good enough for practical applications.

(function() {
var form = document.querySelector('form[name=activity_select_form]')
var matches = document.location.href.match(/\/bids\/([0-9]*)/)
if (form && matches[1]) {
form.innerHTML += '<input type="hidden" name="bid" value="' + matches[1] + '" />'
form.action = 'https://iodine.tjhsst.edu/api/eighth/signup_activity'
form.target = '_blank'
form.addEventListener('submit', setTimeout.bind(null, window.history.back.bind(window.history), 1000))
document.querySelector('div.blurb').innerHTML += ' API Signup enabled.'
}
package com.sk.bot;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
>+[->,[------------------------------------------------<<[->+<]>[-<++++++++++>]>[-<<+>>]<+>]<][-]<
/*
KeyboardAndMouseControl
Controls the mouse from five pushbuttons on an Arduino Leonardo or Micro.
Hardware:
* 5 pushbuttons attached to D2, D3, D4, D5, D6
The mouse movement is always relative. This sketch reads
alert('hi');
// ==UserScript==
// @name Youtube Download Video
// @namespace http://www.strikeskids.com
// @version 0.05
// @description Creates a download button on youtube videos to enable them to be downloaded
// @match http*://*.youtube.com/watch*v=*
// @copyright 2014+, Strikeskids
// @require http://code.jquery.com/jquery-latest.js
// @run-at document-end
// @author Strikeskids
final Scanner in = new Scanner(System.in);
System.out.println("Enter initial value of i: ");
while(!in.hasNextInt()){
in.next();
}
int i = in.nextInt();
in.close();
int len = State.values().length;
// ==UserScript==
// @name Fix Iodine
// @namespace http://www.strikeskids.com
// @version 0.05
// @description Fixes iodine's annoying either period activities
// @match https://iodine.tjhsst.edu/eighth/vcp_schedule/choose/*
// @copyright 2014+, Strikeskids
// ==/UserScript==
$(".activityInfoInner input").each(function() {
#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo;
void setup()
{
servo.attach(2);
Serial.begin(9600);
}
void loop()