CSP-S模板(无注释)
线段树
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
struct node{
int l,r;
int sum;
int add;
}t[N*4];
int a[N];
int x,y,k,n,m;
void pushup(int p){
t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void pushdown(int p){
if(t[p].add==0)
return;
t[p*2].add+=t[p].add;
t[p*2+1].add+=t[p].add;
t[p*2].sum+=(t[p*2].r-t[p*2].l+1)*t[p].add;
t[p*2+1].sum+=(t[p*2+1].r-t[p*2+1].l+1)*t[p].add;
t[p].add=0;
}
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
if(l==r){
t[p].sum=a[l];
return;
}
int mid=(l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
pushup(p);
}
void update(int p,int l,int r,int c){
if(t[p].l>=l&&t[p].r<=r){
t[p].add+=c;
t[p].sum+=(t[p].r-t[p].l+1)*c;
return;
}
int mid=(t[p].l+t[p].r)/2;
pushdown(p);
if(l<=mid) update(p*2,l,r,c);
if(r>mid) update(p*2+1,l,r,c);
pushup(p);
}
int query(int p,int l,int r){
if(t[p].l>=l&&t[p].r<=r)return t[p].sum;
int mid=(t[p].l+t[p].r)/2;
pushdown(p);
int ret=0;
if(l<=mid) ret+=query(p*2,l,r);
if(r>mid) ret+=query(p*2+1,l,r);
return ret;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
while(m--){
int op;
cin>>op;
if(op==1){
cin>>x>>y>>k;
update(1,x,y,k);
}
if(op==2){
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
}
return 0;
}
树状数组
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int a[N],n,m,c[N];
int lowbit(int x){
return x&(-x);
}
void add(int x,int v){
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
}
int sum(int x){
int ret=0;
while(x>0){
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
add(i,a[i]);
}
for(int i=1;i<=m;i++){
int f,x,y;
cin>>f>>x>>y;
if(f==1){
add(x,y);
}
if(f==2){
cout<<sum(y)-sum(x-1)<<"\n";
}
}
return 0;
}
Kruskal
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
struct node{
int u,v,w;
}e[N];
int fa[N],ans,cnt,n,m;
bool cmp(node a,node b){
return a.w<b.w;
}
int find(int x){
if(fa[x]==x){
return x;
}
return fa[x]=find(fa[x]);
}
bool kruskal(){
sort(e+1,e+1+m,cmp);
for(int i=1;i<=n;i++){
fa[i]=i;
}
for(int i=1;i<=m;i++){
int x=find(e[i].u);
int y=find(e[i].v);
if(x!=y){
fa[x]=y;
ans+=e[i].w;
cnt++;
}
}
return cnt==n-1;
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
e[i].u=u;
e[i].v=v;
e[i].w=w;
}
if(kruskal()){
cout<<ans<<endl;
}
else
{
cout<<"orz"<<endl;
}
return 0;
}
ST表
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],f[N][31],n,m,lg[N];
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read();
m=read();
for(int i=1;i<=n;++i){
f[i][0]=read();
}
for(int j=1;j<=30;++j){
for(int i=1;i+(1<<j)-1<=n;++i){
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
for(int i=1;i<=m;++i){
int x,y;
x=read();
y=read();
int k=log2(y-x+1);
cout<<max(f[x][k],f[y-(1<<k)+1][k])<<"\n";
}
return 0;
}
最近公共祖先(LCA)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,m,s,dep[N],f[N][25];
vector <int> g[N];
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
f[u][0]=fa;
for(int i=1;i<=19;i++){
f[u][i]=f[f[u][i-1]][i-1];
}
for(int v:g[u]){
if(v!=fa){
dfs(v,u);
}
}
}
int lca(int u,int v){
if(dep[u]<dep[v]){
swap(u,v);
}
for(int i=19;i>=0;i--){
if(dep[f[u][i]]>=dep[v]){
u=f[u][i];
}
}
if(u==v){
return v;
}
for(int i=19;i>=0;i--){
if(f[u][i]!=f[v][i]){
u=f[u][i],v=f[v][i];
}
}
return f[u][0];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>s;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(s,0);
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
cout<<lca(a,b)<<endl;
}
return 0;
}
KMP
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s,p;
int xt[1000005],n,m;
signed main(){
cin>>s>>p;
xt[0]=xt[1]=0;
n=p.size(),m=s.size();
s=' '+s,p=' '+p;
for(int i=1,j=0;i<n;i++){
while(j && p[i+1]!=p[j+1])j=xt[j];
if(p[i+1]==p[j+1])j++;
xt[i+1]=j;
}
for(int i=0,j=0;i<m;i++){
while(j&&s[i+1]!=p[j+1])j=xt[j];
if(s[i+1]==p[j+1])j++;
if(j==n){
cout<<i+1-n+1<<endl;
}
}
for(int i=1;i<=n;i++){
cout<<xt[i]<<' ';
}
return 0;
}