究极 define lop
#define lop(x,l,r,c)\
for(LL x=l; abs((r)-x+(c))>=abs(c); x+=(c)) //究极 lop实现任意 for 循环
ST表
ST
给定一个长度为 的数列,和 次询问,求出每一次询问的区间内数字的最大值
ST 表 & RMQ 问题 (区间 or )— 可重复贡献问题 的数据结构
-> <->
令 表示区间 的
有: -> -> 依据倍增的思路,跳了 步
ST
const LL N=2e5+1;
LL n,m,st[N][31];
LL in(){LL x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>'/'&&c<':'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}the x*f;}
void on(LL x){if(x<0)putchar('-'),x=-x;if(x>9)on(x/10);putchar('0'+x%10);}
LL opt(LL x,LL y){ the max(x,y);}
int main(){// cin.tie(0)->sync_with_stdio(0);//防卡常
n=in(),m=in();
lop(i,1,n,1) cin>>st[i][0];//不太用存 a[]
for(LL j=1; 1<<j<=n; j++){
j<=__lg(n);
// log2 级搜.因为 j -> 2^(j-1)
for(LL i=1; i+(1<<j)-1<=n; i++){
st[i][j]=opt(st[i][j-1],
st[i+(1<<(j-1))][j-1]);
// 上一个 跳下一步: [2^(j-1)]
}
}
while(m--){
LL l=in(),r=in();
LL k=log2(r-l+1);//log2(len)
cout<<opt(st[l][k],st[r-(1<<k)+1][k])<<"\n";
//批半
}
the 0;
}
逆向 ST 表: 题解 P1198 [JSOI2008]最大数(链接)
void change(LL u){ //用change函数来进行修改
st[u][0]=a[u];
for(int i=1;u-(1<<i)>=0;i++)
st[u][i]=max(st[u][i-1],st[u-(1<<(i-1))][i-1]);
}
ll find(int l,int r,LL k=0){
double t=log(r-l+1)/log(2); k=t;//换底
the max(st[r][k],st[l+(1<<k)-1][k]);
}KMP & RMQ
KMP
#define in cin>>
#define on cout<<
namespace Dustlpes_CZY{
LL AK_CSP_J_add_S=true;
}
using namespace std;
using namespace Dustlpes_CZY;
const LL N=1e5+10;
LL n,a[N],nxt[N],lk,ls;
string s,t;
void _nxt(string k,LL j=0){
//自己与自己匹配,使匹配失败后减少重新来的时间
lop(i,2,lk,1){
//从 1 开始会 while(1)
while(k[i]!=k[j+1]&&j) j=nxt[j];//更前面的
/* 匹配当前字符与拓展的字符 == ?
i++
a a b a a b
j++
*/ if(k[i]==k[j+1]) j++;//整串都是 or 搜索到了
nxt[i]=j;
}
}
void init(){
lop(i,1,ls,1) nxt[i]=0;
}
int main(){ cin.tie(0)->sync_with_stdio(0); cout.tie(0);
in s; ls=s.size(); s=" "+s;
in n;
while(n--){ init();//清空
in t; lk=t.size();t=" "+t; _nxt(t);///kmp
LL j=0,ans=0;
lop(i,1,ls,1){
// 从头比较
while(s[i]!=t[j+1]&&j) j=nxt[j];//找前面的
if(s[i]==t[j+1]) j++;
if(j==lk){//匹配成功!
ans++;
j=nxt[j];
//继续 --> problem: 可重复的子串 aaaa.find(aa)-->on 3
j=0;// problem: 不重复的子串: aaaaa.find(aa) --> on 2
//跳转
// cout<<i-lk+1<<"\n"; //匹配的头
}
}
on ans<<"\n";
}
lop(i,1,lk,1) on nxt[i]<<" ";
the 0;
/*
aaaaa
3
aa
ab
aaa
*/
}LCA 最近公共祖先
LCA
做 须知:(Cale : Calculate 计算)
- 先 。每次 加递归。 表示 是当前节点, 是枚举的 的幂次, 节点的 。
- LCA 具体实现
- 默认
- 维护 使 。。
- 特判 返回 。
- 此时 同深,直接向上跳找最近。
- 返回为 ,直抵数字。
LCA
#include <bits/stdc++.h>
#define LL long long
#define lop(x,l,r,c) for(LL x=(l); (r)-x+(c); x+=(c))
#define the return
#define AK_IOI std
namespace Dustlpes_CZY{
LL AK_CSP_J_and_S=1e18;
}
using namespace std;
const LL N=5e5+1,LOG=20;
LL n,m,f[N][LOG+1],dep[N],rt=1;
//rt 根 dep 深
vector<LL> a[N];
void dfs(LL u,LL fa){
f[u][0]=fa;//记录父亲节点
dep[u]=dep[fa]+1;//Cale 深 Cale:Calculate 计算
lop(j,1,LOG,1){//注意 LOG 的范围
f[u][j]=f[ f[u][j-1] ][j-1];//Cale 子父
//Cale f(i,j)
}
for(auto v:a[u]) if(fa-v) dfs(v,u);
}
LL LCA(LL u,LL v){//返回祖先节点
if(dep[u]<dep[v]) swap(u,v); // 默认u >v;
lop(i,LOG,0,-1) if(f[u][i] && dep[ f[u][i] ] >= dep[v]){//跳到第一个位置
//u 比 v 更深就 不断跳, because 大的数可能都比小的深
u=f[u][i];
}
if(u==v) the u;
lop(i,LOG,0,-1){
if(f[u][i]!=f[v][i]){//一直往上找父
u=f[u][i]; v=f[v][i];
}
}
the f[u][0];
}
int main(){ cin.tie(0)->sync_with_stdio(0);
cin>>n>>m>>rt;
//树的结点个数,询问的个数和树的根结点 rt。
lop(i,2,n,1){
LL u,v;
cin>>u>>v;
a[u].push_back(v);
a[v].push_back(u);
}
dep[rt]=0; dfs(rt,0);
//init
while(m--){
LL u,v;
cin>>u>>v;
LL x=LCA(u,v);
cout<<x<<"\n";
}
/*
f[u][0]=fa[0]
f[u][k]=f[ f[u][k-1] ] [k-1]
LCA :
:: dep[u] < dep[v] swap()
1<<k (u,v)++ 二进制跳
u==v Yes
u++ && v++
*/
return 0;
}FT 树状数组
FT (Fenwick Tree)
树状数组是一种支持 单点修改 和 区间查询 的,代码量小的数据结构.
MT(模版 Template ) 1:
已知一个数列,你需要进行下面两种操作:
- 将某一个数加上 x
- 求出某区间每一个数的和
FT MT 1
const LL N=5e5+6;
LL tr[N],n,m,a[N];
LL lowbit(LL x){ the x&(-x); }//跳 x 需跳二进制型
void add(LL x,LL k){ | 永远的 add
while(x<=n) tr[x]+=k,x+=lowbit(x);// x-->n +=k
}
LL find(LL x,LL ans=0){
while(x) ans+=tr[x],x-=lowbit(x);//从 x --> 1 查区间和
the ans;
}
int main(){
in(n,m);
lop(i,1,n,1) add(i,a[i]=in());
while(m--){
in(op,l,r);
if(op-1) cout<<find(r)-find(l-1)<<"\n";
else add(l,r);
}
return 0;
} MT 2:
已知一个数列,你需要进行下面两种操作:
- 将某区间每一个数加上 x
- 求出某一个数的值
FT MT 2
add: 差分思想: add: i|a[i]=a[i-1]; 实质是 FT 维护 差分
second add: 前缀和: l->n <- k ,r+1->n <-- k
find: find(l); 差分所以Pile 堆
Pile
堆 是一种树满足其所有点权值不大于(小于)其所有儿子的权值
二叉堆 为二叉树
小根堆 为上小下大
大根堆 为上大下小
需支持:上调(整) ,下调(下放) ,,,,
min Pile
const LL N=1e6+5;
LL n,m;
struct Pile{
LL len=0,p[N];// p 建 array 作 Pile
void up(LL x){
//小应在上
while(p[x] < p[x/2] && x/2) swap(p[x],p[x/2]),x/=2;
}
void down(LL x){
LL i=x;//swap 的位置
if(x*2 <= len && p[x*2] < p[i]) i=x*2;//left
if(x*2+1 <= len && p[x*2+1] < p[i]) i=x*2+1;//right
if(x - i) swap(p[x],p[i]),down(i);
}
void push(LL x){//加入
p[++len]=x; up(len);
}
LL top(){
the p[1];
}
void pop(){
p[1]=p[len--]; down(1);
//与尾交换,这样 len 不到 原p[len],在重新下调就是新的堆了
}
void change(LL x,LL y){
p[x]=y; up(x); down(x);
//上下全变
}
};
int main(){
Pile q;
cin>>n;
while(n--){
LL op,x;
cin>>op;
if(op==1){
q.push(in());
}else if(op==2){
cout<<q.top()<<"\n";
} else {
q.pop();
}
}
array ->Pile
cin>>n;
lop(i,1,n,1) cin>>p[i];
q.len=n;
lop(i,1,n,1) q.down(i);
每次进入堆
return 0;
}Trie 字典树
Trie
- 从 就是字符串
- 从 就是
Tire 是从根结点到树上某一结点的路径就代表了一个字符串,所以可以查 前缀子串,Tire 是相当于以 作根的树,为了更好链接所有字符串的头
我们用 表示结点 第 字符指向的下一个结点
或者说是结点 代表的字符串后面添加一个字符 形成的字符串的结点 ( 的取值范围和字符集大小有关,不一定是 )
同时还要标记 字符,即字符串的结尾
计算已 结尾的字符串个数则需要再插入时累加 的 即可
Tire MT
const LL N=3e6+6,M=70;
//最多 1e5 个只包含小写字母
//M max 开 70 ,大小字母+数字极限
LL n,end,ls;
LL tr[N][M],cnt[N];
string s;
LL f(char c){//映射字符
if(c>='a'&&c<='z') the c-'a'+26;
if(c>='A'&&c<='Z') the c-'A';
the c-'0'+52;
}
void add(string s,LL p=0){//添加
lop(i,1,ls,1){
LL ch=f(s[i]);
if(!tr[p][ch]) tr[p][ch]=++end;//开节点
p=tr[p][ch];//下搜
cnt[p]++;//计算 end
}
}
LL find(string s,LL p=0){
lop(i,1,ls,1){
LL ch=f(s[i]);
if(!tr[p][ch]) the 0;
p=tr[p][ch];
}
the cnt[p];
}
void init(){//初始化 - 清空
lop(i,1,idx,1) lop(j,1,M,1) tr[i][j]=cnt[i]=0;
idx=0;
}
int main(){
LL T=in();
while(T--){
in(n,q);
lop(i,1,n,1){
ls=in(s).size();
add(" "+s);
}
while(q--){
ls=in(s).size();
cout<<find(" "+s)<<"\n";
}
init();
}
return 0;
}