Skip to content

Instantly share code, notes, and snippets.

View ekzhang's full-sized avatar

Eric Zhang ekzhang

View GitHub Profile
@ekzhang
ekzhang / dream.cpp
Created December 17, 2015 02:31
Solution for USACO 2015 December Gold #3: Dream
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> state; // ((x y) o)
typedef pair<int, state> tstate;
#define MAXN 1005
#define MAXM 1005
#define INF 1<<30
int N, M;
@ekzhang
ekzhang / brckts.cpp
Created January 4, 2016 00:05
SPOJ BRCKTS Solution
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 30000
int N, M;
int word[MAX_N];
int fenwick_sum[MAX_N + 1];
int fenwick_min[MAX_N + 1];
int word_sum;
@ekzhang
ekzhang / player_min.js
Last active March 18, 2016 00:13
Run this code at ALL TIMES while playing mafia, and DO NOT REFRESH. You may run this code via the address bar, a bookmark, or the browser console.
function hashCode(e){var s,a,t,o=0;if(0===e.length)return o;for(s=0,t=e.length;t>s;s++)a=e.charCodeAt(s),o=(o<<5)-o+a,o|=0;return o}var players="",playing=!1,night=!1,dead=!1,my_name=$("div.message-wrapper.is-me")[0].children[0].innerHTML.trim().slice(0,-1),god="eygmath",voted=!1,_send=Classroom.send,new_send=function(e){e.message.toLowerCase().startsWith("-&gt;join")&&(e.message="-&gt;join|"+hashCode(my_name+"_join")),_send(e)},_onPluginMessage=Classroom.socket.onPluginMessage,newmsg=function(e){var s=e.message;if(my_name=my_name.toLowerCase(),"undefined"!=typeof e.message&&0===e.message.indexOf("-&gt;")){if(e.message=e.message.substring(5),e.speaker===god)if(e.message.toLowerCase().startsWith("players"))-1!==e.message.toLowerCase().indexOf(my_name)&&(playing=!0,players=e.message.substring(8));else if(e.message.toLowerCase().startsWith("end"))playing=!1,dead=!1;else if(e.message.toLowerCase().startsWith("toggle")){if(night=!night,voted=!1,night&&!dead){for(var a=prompt("Who would you like to target?");-1===p
@ekzhang
ekzhang / moderator2.js
Created July 18, 2016 19:37
Code for Voting Game in the AoPS Schoolhouse
'use strict';
var ROOM_ID = prompt('What Room ID to use?');
var playing = false;
var cqueue = [];
var mqueue = [];
var votes = {};
var players = new Set([]);
var joinInterval;
var prevMsg = '';

Keybase proof

I hereby claim:

  • I am ekzhang on github.
  • I am ekzhang (https://keybase.io/ekzhang) on keybase.
  • I have a public key whose fingerprint is 5216 7129 355E EA04 54F6 9A63 4E1F 0DCD FFFD 28DD

To claim this, I am signing this object:

@ekzhang
ekzhang / C++11.sublime-build
Created July 10, 2017 23:30
Sublime Text build file for C++11
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "-std=c++0x", "-O2"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cpp",
"variants":
[
{
"name": "Run",
@ekzhang
ekzhang / MinCostMaxFlow.cpp
Last active August 7, 2017 02:00
Minimum-Cost, Maximum-Flow solver using Successive Shortest Paths with Dijkstra and SPFA-SLF.
#include <bits/stdc++.h>
using namespace std;
/* Minimum-Cost, Maximum-Flow solver using Successive Shortest Paths with Dijkstra and SPFA-SLF.
* Requirements:
* - No duplicate or antiparallel edges with different costs.
* - No negative cycles.
* Time Complexity: O(Ef lg V) average-case, O(VE + Ef lg V) worst-case.
*/
template<int V, class T=long long>
@ekzhang
ekzhang / MaxFlow.cpp
Created August 7, 2017 02:06
Maximum-Flow solver using Dinic's Blocking Flow Algorithm.
#include <bits/stdc++.h>
using namespace std;
/* Maximum-Flow solver using Dinic's Blocking Flow Algorithm.
* Time Complexity:
* - O(V^2 E) for general graphs, but in practice ~O(E^1.5)
* - O(V^(1/2) E) for bipartite matching
* - O(min(V^(2/3), E^(1/2)) E) for unit capacity graphs
*/
template<int V, class T=long long>
@ekzhang
ekzhang / WaveletTree.cpp
Last active August 7, 2017 02:10
Solves SPOJ problem MKTHNUM, online version, in O(n lg n + m lg n) using a wavelet tree
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100013
#define MAXW 262144
int N, M, K;
int A[MAXN];
int nums[MAXN];
vector<int> wt[MAXW];
@ekzhang
ekzhang / RangeTree2.cpp
Last active August 7, 2017 02:17
2D, dynamic range tree based on GNU policy-based data structures. X is limited by size, and Y is limited to be distinct for each point.
#include <bits/stdc++.h>
using namespace std;
/**
* 2D, dynamic range tree based on GNU policy-based data structures.
* Allows for fast, O(lg^2 N) range queries and updates, using O(N lg N) memory.
* The first dimension must be in the interval [0..sx] passed into the constructor,
* while the second dimension can be anything. Update adds a point at (x, y),
* while Query finds the number of unique points with coordinates (x' <= x, y' <= y).
*/