Advanced Data Structures in competitive programming
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的更多相关文章
- Advanced Data Structures
Advanced Data Structures Advanced Data Structures
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
- CSIS 1119B/C Introduction to Data Structures and Algorithms
CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...
- [Data Structures and Algorithms - 1] Introduction & Mathematics
References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...
- 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 ...
- Python Tutorial 学习(五)--Data Structures
5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...
- 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 ...
随机推荐
- jq一行一行循环读取table中的元素
获取当前tr行号,可依据index 获取当前tr对象 获取某一tr下td的内容
- Django----配置数据库读写分离
Django配置数据库读写分离 https://blog.csdn.net/Ayhan_huang/article/details/78784486 https://blog.csdn.net/ayh ...
- EF 常见语句以及sql语句简单 后续继续添加
1.注意级联删除的时候数据库的外键要设置为开启级联删除,(数据库里sqlserver的外键修改的时候,可以看到级联删除和级联更新) using System;using System.Collecti ...
- App测试从入门到精通之App分类和场景操作系统
App概要 APP是application的缩写.通常指的是手机软件上的应用,或称为手机客户端.手机app就是手机的应用程序.随着智能手机的越发普及,用户越发依赖手机软件商品店,app开发的需求与发展 ...
- (函数)实现strstr函数
题目:实现strstr函数. 这个函数原型 strstr(char *a, char *b),在a中是否包含b这个字符串,包含返回第一个子串的位置,否则返回NULL. 思路:其实这个就是找子串的问题. ...
- Instruments Tutorial for iOS: How To Debug Memory Leaks【转】
If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for vis ...
- jQuery插件编写学习+实例——无限滚动
最近自己在搞一个网站,需要用到无限滚动分页,想想工作两年有余了,竟然都没有写过插件,实在惭愧,于是简单学习了下jQuery的插件编写,然后分享出来. 先说下基础知识,基本上分为两种,一种是对象级别的插 ...
- 【EfF】 贪婪加载和延迟加载 (virtual去掉关闭延迟加载)
EntityFramework(EF)贪婪加载和延迟加载的选择和使用 贪婪加载:顾名思议就是把所有要加载的东西一 次性读取 1 using (var context = new MyDbContext ...
- IEnumerable、GetEnumerator、IEnumerator之间的关系。
了解了这些也就明白了遍历的原理,晚安. using System; using System.Collections; public class Person { public Person(stri ...
- MongoDB插入时间不正确的问题
关于mongodb插入时间不正确的问题 今天在给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00 . ...