Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fsh0524 committed Oct 11, 2018
0 parents commit 8d8235d
Show file tree
Hide file tree
Showing 63 changed files with 4,091 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NCTU_Jaguar
85 changes: 85 additions & 0 deletions code/DataStructure/KDTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// from BCW

const int MXN = 100005;

struct KDTree {
struct Node {
int x,y,x1,y1,x2,y2;
int id,f;
Node *L, *R;
}tree[MXN];
int n;
Node *root;

long long dis2(int x1, int y1, int x2, int y2) {
long long dx = x1-x2;
long long dy = y1-y2;
return dx*dx+dy*dy;
}
static bool cmpx(Node& a, Node& b){ return a.x<b.x; }
static bool cmpy(Node& a, Node& b){ return a.y<b.y; }
void init(vector<pair<int,int>> ip) {
n = ip.size();
for (int i=0; i<n; i++) {
tree[i].id = i;
tree[i].x = ip[i].first;
tree[i].y = ip[i].second;
}
root = build_tree(0, n-1, 0);
}
Node* build_tree(int L, int R, int dep) {
if (L>R) return nullptr;
int M = (L+R)/2;
tree[M].f = dep%2;
nth_element(tree+L, tree+M, tree+R+1, tree[M].f ? cmpy : cmpx);
tree[M].x1 = tree[M].x2 = tree[M].x;
tree[M].y1 = tree[M].y2 = tree[M].y;

tree[M].L = build_tree(L, M-1, dep+1);
if (tree[M].L) {
tree[M].x1 = min(tree[M].x1, tree[M].L->x1);
tree[M].x2 = max(tree[M].x2, tree[M].L->x2);
tree[M].y1 = min(tree[M].y1, tree[M].L->y1);
tree[M].y2 = max(tree[M].y2, tree[M].L->y2);
}

tree[M].R = build_tree(M+1, R, dep+1);
if (tree[M].R) {
tree[M].x1 = min(tree[M].x1, tree[M].R->x1);
tree[M].x2 = max(tree[M].x2, tree[M].R->x2);
tree[M].y1 = min(tree[M].y1, tree[M].R->y1);
tree[M].y2 = max(tree[M].y2, tree[M].R->y2);
}

return tree+M;
}
int touch(Node* r, int x, int y, long long d2){
long long dis = sqrt(d2)+1;
if (x<r->x1-dis || x>r->x2+dis || y<r->y1-dis || y>r->y2+dis)
return 0;
return 1;
}
void nearest(Node* r, int x, int y, int &mID, long long &md2) {
if (!r || !touch(r, x, y, md2)) return;
long long d2 = dis2(r->x, r->y, x, y);
if (d2 < md2 || (d2 == md2 && mID < r->id)) {
mID = r->id;
md2 = d2;
}
// search order depends on split dim
if ((r->f == 0 && x < r->x) ||
(r->f == 1 && y < r->y)) {
nearest(r->L, x, y, mID, md2);
nearest(r->R, x, y, mID, md2);
} else {
nearest(r->R, x, y, mID, md2);
nearest(r->L, x, y, mID, md2);
}
}
int query(int x, int y) {
int id = 1029384756;
long long d2 = 102938475612345678LL;
nearest(root, x, y, id, d2);
return id;
}
}tree;
23 changes: 23 additions & 0 deletions code/DataStructure/SparseTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const int MAXN = 200005;
const int lgN = 20;

struct SP{ //sparse table
int Sp[MAXN][lgN];
function<int(int,int)> opt;
void build(int n, int *a){ // 0 base
for (int i=0 ;i<n; i++) Sp[i][0]=a[i];

for (int h=1; h<lgN; h++){
int len = 1<<(h-1), i=0;
for (; i+len<n; i++)
Sp[i][h] = opt( Sp[i][h-1] , Sp[i+len][h-1] );
for (; i<n; i++)
Sp[i][h] = Sp[i][h-1];
}
}
int query(int l, int r){
int h = __lg(r-l+1);
int len = 1<<h;
return opt( Sp[l][h] , Sp[r-len+1][h] );
}
};
103 changes: 103 additions & 0 deletions code/DataStructure/Treap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include<bits/stdc++.h>
using namespace std;
template<class T,unsigned seed>class treap{
public:
struct node{
T data;
int size;
node *l,*r;
node(T d){
size=1;
data=d;
l=r=NULL;
}
inline void up(){
size=1;
if(l)size+=l->size;
if(r)size+=r->size;
}
inline void down(){
}
}*root;
inline int size(node *p){return p?p->size:0;}
inline bool ran(node *a,node *b){
static unsigned x=seed;
x=0xdefaced*x+1;
unsigned all=size(a)+size(b);
return (x%all+all)%all<size(a);
}
void clear(node *&p){
if(p)clear(p->l),clear(p->r),delete p,p=NULL;
}
~treap(){clear(root);}
void split(node *o,node *&a,node *&b,int k){
if(!k)a=NULL,b=o;
else if(size(o)==k)a=o,b=NULL;
else{
o->down();
if(k<=size(o->l)){
b=o;
split(o->l,a,b->l,k);
b->up();
}else{
a=o;
split(o->r,a->r,b,k-size(o->l)-1);
a->up();
}
}
}
void merge(node *&o,node *a,node *b){
if(!a||!b)o=a?a:b;
else{
if(ran(a,b)){
a->down();
o=a;
merge(o->r,a->r,b);
}else{
b->down();
o=b;
merge(o->l,a,b->l);
}
o->up();
}
}
void build(node *&p,int l,int r,T *s){
if(l>r)return;
int mid=(l+r)>>1;
p=new node(s[mid]);
build(p->l,l,mid-1,s);
build(p->r,mid+1,r,s);
p->up();
}
inline int rank(T data){
node *p=root;
int cnt=0;
while(p){
if(data<=p->data)p=p->l;
else cnt+=size(p->l)+1,p=p->r;
}
return cnt;
}
inline void insert(node *&p,T data,int k){
node *a,*b,*now;
split(p,a,b,k);
now=new node(data);
merge(a,a,now);
merge(p,a,b);
}
inline void remove(node *&p, int k) {
node *a, *b, *res, *die;
split(p, a, res, k);
if (res == NULL) return;
split(res, die, b, 1);
merge(a, a, b);
if (size(a) > size(b)) p = a;
else p = b;
clear(die);
}
};
treap<T ,20141223>bst;
int main(){
bst.remove(bst.root, bst.rank(E));
bst.insert(bst.root, E, bst.rank(E));
}
20 changes: 20 additions & 0 deletions code/DataStructure/ext_heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/extc++.h>
typedef __gnu_pbds::priority_queue<int> heap_t;
heap_t a,b;

int main() {
a.clear();
b.clear();
a.push(1);
a.push(3);
b.push(2);
b.push(4);
assert(a.top() == 3);
assert(b.top() == 4);
// merge two heap
a.join(b);
assert(a.top() == 4);
assert(b.empty());

return 0;
}
132 changes: 132 additions & 0 deletions code/DataStructure/link_cut_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// from bcw codebook

const int MXN = 100005;
const int MEM = 100005;

struct Splay {
static Splay nil, mem[MEM], *pmem;
Splay *ch[2], *f;
int val, rev, size;
Splay () : val(-1), rev(0), size(0) {
f = ch[0] = ch[1] = &nil;
}
Splay (int _val) : val(_val), rev(0), size(1) {
f = ch[0] = ch[1] = &nil;
}
bool isr() {
return f->ch[0] != this && f->ch[1] != this;
}
int dir() {
return f->ch[0] == this ? 0 : 1;
}
void setCh(Splay *c, int d) {
ch[d] = c;
if (c != &nil) c->f = this;
pull();
}
void push() {
if (rev) {
swap(ch[0], ch[1]);
if (ch[0] != &nil) ch[0]->rev ^= 1;
if (ch[1] != &nil) ch[1]->rev ^= 1;
rev=0;
}
}
void pull() {
size = ch[0]->size + ch[1]->size + 1;
if (ch[0] != &nil) ch[0]->f = this;
if (ch[1] != &nil) ch[1]->f = this;
}
} Splay::nil, Splay::mem[MEM], *Splay::pmem = Splay::mem;
Splay *nil = &Splay::nil;

void rotate(Splay *x) {
Splay *p = x->f;
int d = x->dir();
if (!p->isr()) p->f->setCh(x, p->dir());
else x->f = p->f;
p->setCh(x->ch[!d], d);
x->setCh(p, !d);
p->pull(); x->pull();
}

vector<Splay*> splayVec;
void splay(Splay *x) {
splayVec.clear();
for (Splay *q=x;; q=q->f) {
splayVec.push_back(q);
if (q->isr()) break;
}
reverse(begin(splayVec), end(splayVec));
for (auto it : splayVec) it->push();
while (!x->isr()) {
if (x->f->isr()) rotate(x);
else if (x->dir()==x->f->dir()) rotate(x->f),rotate(x);
else rotate(x),rotate(x);
}
}

Splay* access(Splay *x) {
Splay *q = nil;
for (;x!=nil;x=x->f) {
splay(x);
x->setCh(q, 1);
q = x;
}
return q;
}
void evert(Splay *x) {
access(x);
splay(x);
x->rev ^= 1;
x->push(); x->pull();
}
void link(Splay *x, Splay *y) {
// evert(x);
access(x);
splay(x);
evert(y);
x->setCh(y, 1);
}
void cut(Splay *x, Splay *y) {
// evert(x);
access(y);
splay(y);
y->push();
y->ch[0] = y->ch[0]->f = nil;
}

int N, Q;
Splay *vt[MXN];

int ask(Splay *x, Splay *y) {
access(x);
access(y);
splay(x);
int res = x->f->val;
if (res == -1) res=x->val;
return res;
}
int main(int argc, char** argv) {
scanf("%d%d", &N, &Q);
for (int i=1; i<=N; i++)
vt[i] = new (Splay::pmem++) Splay(i);
while (Q--) {
char cmd[105];
int u, v;
scanf("%s", cmd);
if (cmd[1] == 'i') {
scanf("%d%d", &u, &v);
link(vt[v], vt[u]);
} else if (cmd[0] == 'c') {
scanf("%d", &v);
cut(vt[1], vt[v]);
} else {
scanf("%d%d", &u, &v);
int res=ask(vt[u], vt[v]);
printf("%d\n", res);
}
}

return 0;
}
Loading

0 comments on commit 8d8235d

Please sign in to comment.