ST表
[https://www.luogu.com.cn/problem/P3865]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
const int N=1e5+10;
int n,m,a[N],st[N][20];
int quary(int l,int r){
int len=log2(r-l+1);
int a=st[l][len];
int b=st[r-(1<<len)+1][len];
return max(a,b);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
st[i][0]=a[i];
}
for(int i=1;(1<<i)<=n;i++){
for(int j=1;j+(1<<i)-1<=n;j++){
int len=(1<<(i-1));
st[j][i]=max(st[j][i-1],st[j+len][i-1]);
}
}
while(m--){
int a,b;
cin>>a>>b;
cout<<quary(a,b)<<endl;
}
return 0;****
}SPFA
[https://www.luogu.com.cn/problem/P3385]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
int T,n,m,q,dis[2005],cnt[2005];
bool st[2005];
struct node{
int v,w;
};
vector<node>adj[2005];
bool spfa(int s){
memset(cnt,0,sizeof cnt);
queue<int>q;
q.push(s);
st[s]=1;
dis[s]=0;
while(!q.empty()){
int h=q.front();
q.pop();
st[h]=0;
if(cnt[h]>n) return 1;
for(node x:adj[h]){
if(dis[x.v]>dis[h]+x.w){
dis[x.v]=dis[h]+x.w;
cnt[x.v]=cnt[h]+1;
if(!st[x.v]){
st[x.v]=1;
q.push(x.v);
}
}
}
}
return 0;
}
signed main(){
cin >> T;
while(T--){
memset(adj,0,sizeof adj);
memset(st,0,sizeof st);
memset(dis,INF,sizeof dis);
cin >> n >> m;
while(m--){
int u,v,w;
cin>>u>>v>>w;
if(w>=0){
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
else adj[u].push_back({v,w});
}
int num=spfa(1);
if(num==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}最近公共祖先
(https://www.luogu.com.cn/problem/P3379)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
const int N=5e5+10;
int n,m,s,f[N][100],d[N*2];
vector<int>z[N*2];
void dfs(int x,int fa){
d[x]=d[fa]+1;
f[x][0]=fa;
for(auto i:z[x]){
if(i!=fa) dfs(i,x);
}
}
void build(){
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i<=n;i++){
f[i][j]=f[f[i][j-1]][j-1];
}
}
}
int lca(int x,int y){
if(d[x]>d[y]) swap(x,y);
for(int i=30;i>=0;i--){
if(d[f[y][i]]>=d[x]) y=f[y][i];
}
if(x==y) return x;
for(int i=30;i>=0;i--){
if(f[x][i]!=f[y][i]){
x=f[x][i];
y=f[y][i];
}
}
return f[x][0];
}
void solve(){
cin>>n>>m>>s;
for(int i=1;i<=n-1;i++){
int x,y; cin>>x>>y;
z[x].push_back(y);
z[y].push_back(x);
}
dfs(s,0); build();
while(m--){
int a,b; cin>>a>>b;
cout<<lca(a,b)<<endl;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;
}哈希表
(https://www.luogu.com.cn/problem/P3370)
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 200010;
const int P = 131;
int n;
string str[N];
int h[1510], p[N];
map<int,int>mp;
signed main()
{
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> str[i];
str[i]=" "+str[i];
}
p[0] = 1;
for(int i=1 ; i<=n ; i++)
{
p[i] = p[i-1] * P;
for(int j=1 ; j<str[i].size() ; j++)
{
h[j] = h[j-1] * P + str[i][j];
}
int k = str[i].size()-1;
mp[h[k]]++;
}
cout<<mp.size();
}线段树
(https://www.luogu.com.cn/problem/P3373)
#include <bits/stdc++.h>
#define MAXN 100010
#define ll long long
using namespace std;
int n, m, mod;
int a[MAXN];
struct Segment_Tree {
ll sum, add, mul;
int l, r;
}s[MAXN * 4];
void update(int pos) {
s[pos].sum=(s[pos<<1].sum+s[pos<<1|1].sum)%mod;
return;
}
void pushdown(int pos) {
s[pos<<1].sum=(s[pos<<1].sum*s[pos].mul+s[pos].add*(s[pos<<1].r-s[pos<<1].l+1))%mod;
s[pos << 1 | 1].sum = (s[pos << 1 | 1].sum * s[pos].mul + s[pos].add * (s[pos << 1 | 1].r - s[pos << 1 | 1].l + 1)) % mod;
s[pos << 1].mul = (s[pos << 1].mul * s[pos].mul) % mod;
s[pos << 1 | 1].mul = (s[pos << 1 | 1].mul * s[pos].mul) % mod;
s[pos << 1].add = (s[pos << 1].add * s[pos].mul + s[pos].add) % mod;
s[pos << 1 | 1].add = (s[pos << 1 | 1].add * s[pos].mul + s[pos].add) % mod;
s[pos].add = 0;
s[pos].mul = 1;
return;
}
void build_tree(int pos, int l, int r) {
s[pos].l = l;
s[pos].r = r;
s[pos].mul = 1;
if (l == r) {
s[pos].sum = a[l] % mod;
return;
}
int mid = (l + r) >> 1;
build_tree(pos << 1, l, mid);
build_tree(pos << 1 | 1, mid + 1, r);
update(pos);
return;
}
void ChangeMul(int pos, int x, int y, int k) {
if (x <= s[pos].l && s[pos].r <= y) {
s[pos].add = (s[pos].add * k) % mod;
s[pos].mul = (s[pos].mul * k) % mod;
s[pos].sum = (s[pos].sum * k) % mod;
return;
}
pushdown(pos);
int mid = (s[pos].l + s[pos].r) >> 1;
if (x <= mid) ChangeMul(pos << 1, x, y, k);
if (y > mid) ChangeMul(pos << 1 | 1, x, y, k);
update(pos);
return;
}
void ChangeAdd(int pos, int x, int y, int k) {
if (x <= s[pos].l && s[pos].r <= y) {
s[pos].add = (s[pos].add + k) % mod;
s[pos].sum = (s[pos].sum + k * (s[pos].r - s[pos].l + 1)) % mod;
return;
}
pushdown(pos);
int mid = (s[pos].l + s[pos].r) >> 1;
if (x <= mid) ChangeAdd(pos << 1, x, y, k);
if (y > mid) ChangeAdd(pos << 1 | 1, x, y, k);
update(pos);
return;
}
ll AskRange(int pos, int x, int y) {
if (x <= s[pos].l && s[pos].r <= y) {
return s[pos].sum;
}
pushdown(pos);
ll val = 0;
int mid = (s[pos].l + s[pos].r) >> 1;
if (x <= mid) val = (val + AskRange(pos << 1, x, y)) % mod;
if (y > mid) val = (val + AskRange(pos << 1 | 1, x, y)) % mod;
return val;
}
int main() {
scanf("%d%d%d", &n, &m, &mod);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build_tree(1, 1, n);
for (int i = 1; i <= m; i++) {
int opt, x, y;
scanf("%d%d%d", &opt, &x, &y);
if (opt == 1) {
int k;
scanf("%d", &k);
ChangeMul(1, x, y, k);
}
if (opt == 2) {
int k;
scanf("%d", &k);
ChangeAdd(1, x, y, k);
}
if (opt == 3) {
printf("%lld\n", AskRange(1, x, y));
}
}
return 0;
}
线性筛素数
(https://www.luogu.com.cn/problem/P3383)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl "\n"
int n,m;
vector<int>v;
bool st[100000010];
void shai(int n){
for(int i=2;i<=n;i++){
if(st[i]==1) continue;
v.push_back(i);
for(int j=i+i;j<=n;j+=i){
st[j]=1;
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
shai(n);
while(m--){
int x;
cin>>x;
cout<<v[x-1]<<endl;
}
return 0;
}dijistar
(https://www.luogu.com.cn/problem/P4779)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N=2e5+10;
using pii=pair<int,int>;
int n,m,s;
vector<pii>z[N];
int d[N],st[N];
void dij(int x)
{
memset(d,INF,sizeof d);
memset(st,0,sizeof st);
priority_queue<pii,vector<pii>,greater<pii>>q;
q.push({0,x});
d[x]=0;
while(q.size())
{
auto t=q.top();
q.pop();
if(st[t.second]==1) continue;
st[t.second]=1;
for(auto i:z[t.second])
{
if(d[i.first] > d[t.second] + i.second)
{
d[i.first] = d[t.second] + i.second;
q.push({d[i.first],i.first});
}
}
}
}
signed main()
{
cin>>n>>m>>s;
while(m--)
{
int u,v,w;
cin>>u>>v>>w;
z[u].push_back({v,w});
}
dij(s);
for(int i=1;i<=n;i++)
{
cout<<d[i]<<" ";
}
}拓扑排序
(https://www.luogu.com.cn/problem/B3644)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 110;
int n;
vector<int>ans,v[N];
int ru[N];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
while(1)
{
int t;
cin>>t;
if(t==0) break;
v[i].push_back(t);
ru[t]++;
}
}
queue<int>q;
for(int i=1;i<=n;i++)
{
if(ru[i]==0) q.push(i);
}
while(q.size())
{
int f=q.front();
q.pop();
ans.push_back(f);
for(auto i:v[f])
{
ru[i]--;
if(ru[i]==0) q.push(i);
}
}
for(auto i:ans)
{
cout<<i<<" ";
}
return 0;
} 最小生成树
(https://www.luogu.com.cn/problem/P3366)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 10000;
int n,m;
struct as{
int l,r,w;
};
int f[N];
vector<as>v;
int find(int x)
{
if(x!=f[x]) f[x]=find(f[x]);
return f[x];
}
int cmp(as x,as y)
{
return x.w < y.w;
}
int ans=0;
void kk()
{
sort(v.begin(),v.end(),cmp);
for(int i=1;i<=n;i++) f[i]=i;
int cnt=0;
for(auto i:v)
{
if(cnt==n-1) break;
int e1=find(i.l);
int e2=find(i.r);
if(e1==e2) continue;
ans+=i.w;
f[e1]=e2;
cnt++;
}
if(cnt==n-1) cout<<ans;
else cout<<"orz";
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
v.push_back({a,b,c});
}
kk();
}快速幂
(https://www.luogu.com.cn/problem/P1226)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
int a,b,p;
int pows(int a,int b){
int ans=1;
while(b){
if(b%2==1) ans=ans*a;
ans%=p;
a%=p;
a*=a;
a%=p;
b/=2;
}
return ans;
}
signed main(){
cin>>a>>b>>p;
int ans=pows(a,b);
cout<<a<<"^"<<b<<" mod "<<p<<"="<<ans;
return 0;
}二分查找
(https://www.luogu.com.cn/problem/U438352)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 2e5+10;
struct as{
int num,w;
}a[N];
int n,k;
int cmp(as x,as y)
{
return x.num<y.num;
}
int check(int mid)
{
if(a[mid].num >= k) return 1;
else return 0;
}
int ans;
signed main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i].num;
a[i].w=i;
}
sort(a+1,a+1+n,cmp);
a[0].num=-1e9;
a[n+1].num=1e9;
int l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid)==1) r=mid;
else l=mid;
}
if(a[r].num==k) cout<<r-1;
else cout<<"-1";
return 0;
}KMP
(https://www.luogu.com.cn/problem/P3375)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f
const int N=1000010;
const int P=131;
string s1, s2;
int h1[N], h2[N], p[N];
int s[N];
int f[N];
int get(int l,int r,int h[],int p[])
{
return h[r]-h[l-1]*p[r-l+1];
}
signed main()
{
h1[0]=0;
p[0]=1,s[0]=1;
cin >> s1 >> s2;
int len1=s1.size();
int len2=s2.size();
for(int i=0;i<len1;i++)
{
p[i+1]=p[i]*P;
h1[i+1]=h1[i]*P+s1[i];
}
for(int i=0;i<len2;i++)
{
h2[i+1]=h2[i]*P+s2[i];
}
int t=h2[len2];
for(int i=1;i<=len1-len2+1;i++)
{
if(get(i,i+len2-1,h1,p)==t)
{
cout<<i<<endl;
}
}
f[1]=0;
cout<<f[1]<<" ";
for(int i=2;i<=len2;i++)
{
bool flag=1;
for(int j=f[i-1];j>=0;j--)
{
if(get(1,j+1,h2,p)==get(i-j,i,h2,p))
{
if(j==f[i-1])
{
f[i]=f[i-1]+1;
}
else f[i]=j+1;
flag=0;
cout<<f[i]<<" ";
break;
}
}
if(flag)
{
f[i]=0;
cout<<f[i]<<" ";
}
}
} Manacher
https://www.luogu.com.cn/problem/P3805
#include<bits/stdc++.h>
using namespace std;
const int N=11000002;
int n,p[2*N],mi,r,an,le;
char s[N],t[2*N];
signed main(){
scanf("%s",s+1);
le=strlen(s+1);
t[++n]='$';
t[++n]='#';
for(int i=1;i<=le;i++){
t[++n]=s[i];
t[++n]='#';
}
t[++n]='!';
for(int i=2;i<n;i++){
if(i<=r){
p[i]=min(p[2*mi-i],r-i+1);
}
else p[i]=1;
while(t[i-p[i]]==t[i+p[i]]){
p[i]++;
}
if(i+p[i]>r){
mi=i;
r=i+p[i]-1;
}
an=max(an,p[i]);
}
cout<<an-1;
return 0;
}
AC 自动机
(https://www.luogu.com.cn/problem/P3808)
#include<bits/stdc++.h>
#define N 500010
using namespace std;
queue<int>q;
struct Aho_Corasick_Automaton{
int c[N][26],val[N],fail[N],cnt;
void ins(char *s){
int len=strlen(s);int now=0;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(!c[now][v])c[now][v]=++cnt;
now=c[now][v];
}
val[now]++;
}
void build(){
for(int i=0;i<26;i++)if(c[0][i])fail[c[0][i]]=0,q.push(c[0][i]);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<26;i++)
if(c[u][i])fail[c[u][i]]=c[fail[u]][i],q.push(c[u][i]);
else c[u][i]=c[fail[u]][i];
}
}
int query(char *s){
int len=strlen(s);int now=0,ans=0;
for(int i=0;i<len;i++){
now=c[now][s[i]-'a'];
for(int t=now;t&&~val[t];t=fail[t])ans+=val[t],val[t]=-1;
}
return ans;
}
}AC;
int n;char p[1000005];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%s",p),AC.ins(p);
AC.build();
scanf("%s",p);int ans=AC.query(p);
printf("%d\n",ans);
return 0;
}Floyd
(https://www.luogu.com.cn/problem/B3647)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
int n,m;
int d[5010][5010];
void f(){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) d[i][j]=0;
else d[i][j]=INF;
}
}
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
d[u][v]=min(d[u][v],w);
d[v][u]=min(d[v][u],w);
}
f();
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<d[i][j]<<" ";
}
cout<<endl;
}
return 0;
}并查集
(https://www.luogu.com.cn/problem/P3367)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 2e5+10;
int n,m;
int f[N];
int find(int x)
{
if(f[x]!=x)
{
f[x]=find(f[x]);
}
return f[x];
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) f[i]=i;
while(m--)
{
int z,x,y;
cin>>z>>x>>y;
if(z==1)
{
f[find(x)]=find(y);
}
if(z==2)
{
if(find(x)==find(y)) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}树状数组
(https://www.luogu.com.cn/problem/P3374)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 5e5+10;
int n,m,a[N],s[N];
int lowbit(int x)
{
return x & -x;
}
void add(int x,int k)
{
for(int i=x;i<=n;i+=lowbit(i))
{
s[i]+=k;
}
}
int get(int x)
{
int ans=0;
for(int i=x;i>0;i-=lowbit(i))
{
ans+=s[i];
}
return ans;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
add(i,a[i]);
}
while(m--)
{
int op;
cin>>op;
if(op==1)
{
int x,k;
cin>>x>>k;
add(x,k);
}
else
{
int x,y;
cin>>x>>y;
int ans=get(y)-get(x-1);
cout<<ans<<endl;
}
}
return 0;
}单调栈
(https://www.luogu.com.cn/problem/P5788)
#include<cstdio>
#include<stack>
#include<algorithm>
#define mp make_pair
using namespace std;
stack<long long> S;
long long a[5000005],ans;
int main(){
long long n;
scanf("%lld",&n);
for(long long i=1;i<=n;++i) scanf("%lld",&a[i]);
a[n+1]=10086001100860011ll;
for(long long i=1;i<=n+1;++i)
{
if(S.empty() || a[S.top()]>=a[i]) S.push(i);
else
{
while(S.size() && a[S.top()]<a[i])
{
long long Top=S.top();
ans+=(i-Top-1);
S.pop();
}
S.push(i);
}
}
printf("%lld",ans);
return 0;
}字典树
(https://www.luogu.com.cn/problem/P8306)
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f3f
#define endl '\n'
int n,q,t[3000005][80],tot=0,cnt[3000010];
char s[3000005];
int get(char x){
if(x>='A'&&x<='Z')
return x-'A';
else if(x>='a'&&x<='z')
return x-'a'+26;
else
return x-'0'+52;
}
void build(char s[]){
int len=strlen(s),p=0;
for(int i=0;i<len;i++){
int ch=get(s[i]);
if(!t[p][ch]) t[p][ch]=++tot;
p=t[p][ch];
cnt[p]++;
}
}
int find(char s[]){
int len=strlen(s),p=0;
for(int i=0;i<len;i++){
int ch=get(s[i]);
if(!t[p][ch]) return 0;
p=t[p][ch];
}
return cnt[p];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T; cin>>T;
while(T--){
for(int i=0;i<=tot;i++)
for(int j=0;j<=122;j++)
t[i][j]=0;
for(int i=0;i<=tot;i++)
cnt[i]=0;
tot=0;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>s;
build(s);
}
while(q--){
cin>>s;
cout<<find(s)<<endl;
}
}
return 0;
}