1.bit

#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1) #define modulo(S, N) ((S) & (N - 1)) // returns S % N, where N is a power of 2
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))

2.UF

#include<iostream>
#include<vector>
#define FOR(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
using namespace std;
typedef vector<int> vi; vi p,rank;
void build(int N){
p.assign(N,0);//import
FOR(i,0,N+1) p[i] = i;
rank.assign(N,0);
}
void link(int i,int j){
if(p[i]==p[j]) return ;
if(rank[i] < rank[j]){
p[i] = j;
}
else{
p[j] = i;
if(rank[i] == rank[j])
rank[i]++;
}
}
int find(int i){
return p[i] == i ? i : find(p[i]);
}
bool same(int i,int j){
return find(i) == find(j);
} int main(){
build(5);
link(2,3);
cout << p[2] << " " << p[3] << endl;
cout << same(2,3) << endl;
cout << find(2) << " " << find(3) << endl;
return 0;
}

3.ST

#include<iostream>
#include<vector>
#define FOR(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define lc(p) (p)<<1
#define rc(p) ((p)<<1)+1
using namespace std;
typedef vector<int> vi; vi st,a;
int n;
void initST(vi &b){
a = b; n = b.size();
st.assign(4*n,0);
}
int build(int p,int L,int R){//1,0,n-1
if(L == R) return st[p] = L; //base1
else{
int v1 = build(lc(p),L,(L+R)/2);
int v2 = build(rc(p),(L+R)/2+1,R);
return st[p] = v1 < v2 ? v1:v2; //base2,/********NOTE***/
}
}
int rmq(int p,int L,int R,int i,int j){ if(L > j || R < i) return -1;
if(L >= i && R <= j) return st[p];//trivial condition
//
int p1 = rmq(lc(p),L,(L+R)/2,i,j);
int p2 = rmq(rc(p),(L+R)/2+1,R,i,j);
if(p1 == -1) return p2;
if(p2 == -1) return p1;
return a[p1] < a[p2] ? p1 : p2; }
int rmq(int i,int j){
return rmq(1,0,n-1,i,j);
} int main(){
int arr[] = {0,1,2,3,4,5,6,7,8,9}; vi b(&arr[0],&arr[9]);
initST(b);
//a.assign(10,0); FOR(i,0,10) a[i] = i; st.assign(4*10,0); n = 10;//init
build(1,0,10-1);
cout << st[1] << " " << st[3] << endl;
cout << rmq(0,3) << " " << rmq(1,3) << " " << rmq(3,5) << endl;
return 0;
}

Trie

struct trie{
int id;
trie *next[10];//仅用于存储数字,自行扩展
Node(){
id = -1;
for(i,0,10){
next[i] = NULL;
}
}
};
trie *root = new trie();
void add(char *str,int id){
int len = strlen(str);
trie *u = root;
f(i,0,len){
int v = str[i]-'0';
if(!u->next[v])
u->next[v] = new trie();
u = u->next[v];
if(u->id == -1) //NOTE:首先出现者占有ID
u->id = id;
}
}
int query(char *str){
trie *u = root;
int len = strlen(str);
f(i,0,len){
u = u->next[str[i]-'0'];
if(!u) return -1;
}
return u->id;
}

Fenwick Tree

int v[32003]; //N
int lowbit(int a){
return a&(-a);
}
void update(int r){
while(r<=32000){
v[r]++;
r+=lowbit(r);
}
}
int sum(int r){
int k = 0;
while(r>0){
k+=v[r];
r-=lowbit(r);
}
k+=v[r];//
return k;
}
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define lowbit(b) (b)&(-b)
using namespace std;
vector<int> ft;
int n;
void init(int n){
ft.assign(n+1,0);
}
int rsq(int b){
int sum = 0;
while(b){
sum+=ft[b];
b-=lowbit(b);
}
return sum;
}
int rsq(int a,int b){
if(a-1) return rsq(b)-rsq(a-1);
return rsq(b);
}
void update(int k,int v){
while(k<ft.size()){
ft[k]+=v;
k+=lowbit(k);
}
} int main(){
int f[] = {2,4,5,5,6,6,6,7,7,8,9};
n = 10;//0-based
init(n);
for(int i = 0; i < 11; i++)
update(f[i],1);
cout << rsq(1,1) << endl;//ft[1]
cout << rsq(1,2) << endl;//ft[2]
cout << rsq(1,6) << endl;//ft[6]+ft[4] = 5+2
cout << rsq(1,10) << endl;//ft[10]+ft[8] = 1+10
cout << rsq(3,6) << endl;//rsq(1,6)-rsq(1,2)
update(5,2);
cout << rsq(1,10) << endl;
return 0;
}

FT(2D)

POJ1195

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#define lowbit(b) (b)&(-b)
using namespace std;
int ft[1100][1100];
int n,x,y,m,l,b,r,t;
void init(){
memset(ft,0,sizeof ft);
}
int rsq(int x,int y){
int sum = 0;
for(int i = x; i; i -= lowbit(i)){
for(int j = y; j; j -= lowbit(j)){
sum+=ft[i][j];
}
}
return sum;
}
int rsq(int l,int b,int r,int t){
return rsq(r,t)-rsq(r,b-1)-rsq(l-1,t)+rsq(l-1,b-1);
}
void update(int x,int y,int v){
for(int i = x; i <= n; i += lowbit(i)){
for(int j = y; j <= n; j += lowbit(j)){
ft[i][j] += v;
}
}
}
int main(){
int op;
while(scanf("%d",&op)!=EOF){
if(op==3) continue;
else if(op==0){
scanf("%d",&n);
init();
}
else if(op==1){
scanf("%d%d%d",&x,&y,&m); x++;y++;
update(x,y,m);
}
else{
scanf("%d%d%d%d",&l,&b,&r,&t); l++;b++;r++;t++;
printf("%d\n",rsq(l,b,r,t));
}
}
return 0;
}

More

https://github.com/Dev-XYS/Algorithms

Advanced Data Structures in competitive programming的更多相关文章

  1. Advanced Data Structures

    Advanced Data Structures Advanced Data Structures

  2. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  3. CSIS 1119B/C Introduction to Data Structures and Algorithms

    CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...

  4. [Data Structures and Algorithms - 1] Introduction & Mathematics

    References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...

  5. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  6. Python Tutorial 学习(五)--Data Structures

    5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...

  7. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  8. [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures

    To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...

  9. 20162314 《Program Design & Data Structures》Learning Summary Of The Fifth Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Fifth Week ...

随机推荐

  1. 264E Roadside Trees

    传送门 题目大意 分析 倒着跑LIS表示以i为开头的LIS,于是对于删除可以暴力重算前10棵树.而对于种树,因为高度不超过10且高度两两不同,所以暴力重算比它矮的10棵树即可.对于需要重算的点,将其从 ...

  2. Classification and Prediction

    # coding: utf-8 # In[128]: get_ipython().magic(u'matplotlib inline') import pandas as pd from pandas ...

  3. numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法

    1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...

  4. 通过javascript的日期对象来得到当前的日期

    var currentDate = new Date(); var weekday = ["星期日", "星期一", "星期二", &quo ...

  5. eWebEditor9.x整合教程-Xproer.WordPaster

    版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...

  6. Hadoop深入学习:MapTask详解

    转自:http://flyingdutchman.iteye.com/blog/1878775#bc2337280 Hadoop深入学习:MapTask详解 博客分类: Hadoop MapTask执 ...

  7. 异步串行通信的XON与XOFF

    在单片机的异步串行通信中,putchar函数中的实现中反复用到了XON和XOFF,定义原型如下: #define XON 0x11#define XOFF 0x13 查找ASCII码表,这两个对应的是 ...

  8. Spring:配置文件

    首先是bean.xml,配置所有的bean,一般也叫applicationContext.xml,应用程序上下文.示例: <?xml version="1.0" encodi ...

  9. Mono for Android for Visual Studio 2010安装及试用

    安装 Mono for Android for Visual Studio 2010 需要下面4个步骤: 1.安装 JDK 下载并安装 Java 1.6 (Java 6) JDK. 2.安装 Andr ...

  10. Ubuntu下vi编辑器不听话

    编辑文件/etc/vim/vimrc.tiny,将“compatible”改成“nocompatible”非兼容模式: 并添加一句:set backspace=2