过秋漫遍来时路,昂首今朝仍光明 | I/ENFP |「神眷」·【无情】极路·【禁忌无效化】 | 宝, 我们相爱不是巧然, 而是你我缘分的必然。
终极 define lop
#define lop(x,l,r,c)\
for(LL x=l; ((r)-(x))*(c)>=0&&(c); x+=(c))正负翻转得到大小帧,从而用 的偏移使得范围内的 以及超到负级时跳出。
比 要好的原因是负级不会回去,进而使 :
#df lp(xlrc) for x=l in as(r-x+c)>=as(c) to x+=c:
lp(i=l=120,r=115,c=2) pf(i<<2);而加上
&&(c)更可以使得二次条件以及对于 FT 中的条件:
n=10,m=5;
lop(i,1,n,2 && i<=m) cout<<i<<" ";ST表
ST
给定一个长度为 的数列,和 次询问,求出每一次询问的区间内数字的最大值
ST 表 & RMQ 问题 (区间 or )— 可重复贡献问题 的数据结构
-> <->
令 表示区间 的
有: -> -> 依据倍增的思路,跳了 步
ST
const LL N=2e5+1;
LL n,m,st[N][31];
LL opt(LL x,LL y){ the max(x,y);}
int main(){ cin.tie(0)->sync_with_stdio(0); cout.tie(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)]||- 优先大于 << :: 1<<j-1 --> 1<<(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;1<<i<=u;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
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(){
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;
}LCA 最近公共祖先
LCA
做 须知:( : Calculate 计算)
- 先 。每次 加递归。 表示 是当前节点, 是枚举的 的幂次, 节点的 。
- 具体实现
- 默认
- 维护 使 。。
- 特判 返回 。
- 此时 同深,直接向上跳找最近。
- 返回为 ,直抵数字。
LCA
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";
}
return 0;
}FT 树状数组
FT (Fenwick Tree)
树状数组是一种支持 单点修改 和 区间查询 的,代码量小的数据结构.
MT(模版 Template) ——单休区查:
已知一个数列,你需要进行下面两种操作:
- 将某一个数加上 x
- 求出某区间内每一个数的和
FT MT 1
const LL N=5e5+6;
LL tr[N],n,m;
LL lowbit(LL x){ the x&(-x); }//跳 x 需跳二进制型
void add(LL x,LL d){ while(x<=n) tr[x]+=d,x+=lowbit(x);}// x-->n +=d
LL find(LL x,LL ans=0){ while(x) ans+=tr[x],x-=lowbit(x);the ans;}
//从 x --> 1 查区间和
LL ask(LL l,LL r){ the find(r)-find(--l);}
int main(){
in(n,m);
lop(i,1,n,1) add(i,in());
while(m--){
in(op,l,r);
if(op-1) cout<<ask(l,r)<<"\n";
else add(l,r);
}
the 0;
}取消淡茶:
已知一个数列,你需要进行下面两种操作:
- 将某区间每一个数加上 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); 差分所以取消插曲
区修区查。
(O.O) 这是最常用的部分,也是用线段树写着最麻烦的部分——但是现在我们有了树状数组!
怎么求呢?我们基于取消淡茶的“差分”思路,考虑一下如何在取消淡茶构建的 FT 中求前缀和:
位置p的前缀和 =
右式子 被使用了 次, 被使用了 次…写出位置 的前缀和为:
当然: 很好求,简化使得 即可。
那 ,接着对应于取消淡茶里的前缀和就行了。
码中用 代替
FT QXQC
tr[N],sun[N];
add(x,d) : lop(i,x,n,lb(i)) tr[i]+=d,sun[i]+=d*x
push(l,r,d) add(l,d); add(r+1,-d);
find(x,ans=0) lop(i,x,1,-lb(i)) ans+=(x+1)*tr[i]-sun[i]; the ans;
ask(l,r) the find(r)-find(--l);Pile 堆 可= prity
Pile
堆 是一种树满足其所有点权值不大于(小于)其所有儿子的权值
二叉堆 为二叉树
小根堆 为上小下大
大根堆 为上大下小
需支持:上调(整) ,下调(下放) ,,,,
min Pile
const LL N=1e6+5;
LL n,m;
struct Pile{
LL len,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);
//上下全变
}
} q;
int main(){
cin>>n;
while(n--){
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) p[i]=in();
q.len=n;
lop(i,1,n,1) q.down(i);
每次进入堆
the 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();
}
the 0;
}SegT 线段树
SegT (Segment Tree)
线段树是算法竞赛中常用的用来维护 区间信息 的数据结构。
在 的时间里实现:
- 单点修改,区间修改(
+ and *)。 - 区间查询:区间求和
sum,求区间最大值mx,求区间最小值mi。
一些东西看 忠诚 里面。
基本实现:
记 inf=-1e18,特别的 SegT 看 扶苏的问题, 为一种运算。写的顺序:
up()->eval()->down()->build()->...//后面随便0.struct
适用全部的结构体:
LL l,r;//区间
LL ans;//答案
LL ls,rs;//动态开点的 left_son + right_son
LL add,mul;//懒惰标记 + and *1.up
本质是通过子节点求出父节点的值 。
void up(LL u){
t[u]=opt(t[u<<1].ans,t[u<<1|1].ans);
}2.eval
优化:接收懒标记
void eval(CCTV &v,LL add,LL mul){
// 对于一个 u 的子节点 v 接收 add 的 lazy 标记,更改保存: &v
if(mul==inf){//只用加的 lazy
v.ans+=(v.r-v.l+1)*add;//更改区间所有的数 +=add, 所以总和 += (r-l+1) 个 add
v.add+=add;//传承
}
}1.build
表示使 节点维护 的区间,那如何做左儿子和右儿子呢?
mid = l+r>>1,跟归并一样,实际是二叉树的性质分为 和 。
特别的,每次建树后需要进行一次 up 操作,这样使得 有初值;
对于叶子来说,我们赋值 {l=l, r=r, ans=0, add=0, mul=inf} 为子节点值,从而是父节点可以 。
void build(u,l,r){
if(l==r){
t[u]={l,r,a[l],u<<1,u<<1|1,0,inf};the ;// all members
// 以 L 为头
} t[u]={l,r,0,u<<1,u<<1|1,0,inf};
mid=l+r>>1;
build(l,mid); build(mid+1,r);
up(u);//计算 ans 或其他
}SDLL 链表
全链表 (S/D)Linked List
SLL 单向链表
1 x y:将元素 插入到 后面(默认有 打头);2 x:询问 后面的元素是什么。如果 是最后一个元素,则输出 ;3 x:从表中删除元素 后面的那个元素,不改变其他元素的先后顺序。
SLL Code
N=1e6+6;
LL T,n,m,q,k;
struct SLL{
LL _[N]={0},len,nxt[N]={},l;//last
map<LL,LL> mp;//位置
void push(LL x,LL y){// x后插y
l=mp[x]; nxt[++len]=nxt[l];
nxt[l]=len; _[len]=y; mp[y]=len;
}
LL ans(LL x){ the _[nxt[mp[x]]];}
void pop(LL x){ l=mp[x],nxt[l]=nxt[nxt[l]]; }
} sl;
int main(){
cin>>q;
sl.push(0,1);
while(q--){
cin>>op>>x;
if(op<2) cin>>y;
if(op==1) sl.push(x,y);
elif(op==2) cout<<sl.ans(x)<<"\n";
else sl.pop(x);
}
the 0;
}DLL 双向链表 约等于 list
赋值 ,再如同题目操作。
DLL Code
N=1e6+6;
struct DLL{
LL a[N],l[N],r[N],len,_;
map<LL,bool> cnt;
map<LL,LL> mp;
void push(LL x){
a[++len]=x;//默认加入到最后
l[len]=len-1; r[len-1]=len;
mp[x]=len;
}
void init(LL x){
x=mp[x];//删 x
r[l[x]]=r[x],l[r[x]]=l[x];
}
void ans(){ while(_=r[_]) cout<<a[_]<<" "; }
void change(LL x,LL y,LL c){//默认到右插
if(x==y) the ;
x=mp[x];y=mp[y];
if(c) y=l[y];//转换是左插要映射 y
r[x]=r[y]; l[x]=y;
l[r[y]]=x; r[y]=x;
}
void pop(LL x){ cnt[x]=1; }
bool count(LL x){ the cnt[x]; }
} a;
int main(){
cin>>n>>t;
lop(i,1,n,1){
a.push(in());//赋值
}
while(t--){
cin>>op>>x;
if(op!=3) cin>>y;
if(a.count(x)) continue;
a.init(x);
if(op==3) a.pop(x),continue;
a.change(x,y,op&1);
}
a.ans();
the 0;
}IP 逆序对
IP (Inversion pair)
P1908 为 FT,主讲为队列版。
void ip(LL l,LL r){ if(r<=l) the ;
LL X=l+r>>1,i=l,j=X; ip(l,X++); ip(X,r); queue<LL> q;//X++ 为后面减少字面量
while(i<X&&j<=r) if(a[i]<=a[j]) q.p(a[i++]); else q.p(a[j++]),ans+=X-i;//ans
while(i<X) q.p(a[i++]); while(j<=r) q.p(a[j++]);/ 外 push
while(l<=r) a[l++]=q.front(),q.pop();
}deque 手写+AI 测评改进
dq
手写 deque。
constexpr int MAXN=1<<((1<<(1<<(1<<1)))+(1<<(1<<1))+1);
//1 <<21
struct dq{
LL q[MAXN],num=0;
int l=0,r=-1;
static constexpr int CAP = MAXN;
bool full() const{ return num==CAP; }
bool empty() const{ return !num; }
void push_back(LL x){
if(full()) return ;
r++; if(r==CAP) r=0;
q[r]=x; num++;
}
void push_front(LL x){
if(full()) return ;
l--; if(l<0) l=CAP-1;
q[l]=x; num++;
}
void pop_back(){
if(empty()) return ;
num--; r--;
if(r < 0) r=CAP-1;
}
void pop_front(){
if(empty()) return ;
num--; l++;
if(l ==CAP) l=0;
}
LL size() const{ return num;}
LL front() const{ return q[l];}
LL back() const{ return q[r];}
};祭奠 PASS TJ 区
P14920 [GESP202512 六级] 道具商店
P1147 连续自然数和
P14970『GTOI - 2A』睡眠质量
P14987 全等
