模板

数据结构

单调队列

// BZOJ 2096
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 3000005

int k,n,l,r,lmax,rmax,lmin,rmin,ans;
int a[N],qmax[N],qmin[N];
struct hp{
  int Max,Min;};

void push(int id)
{
    while (lmax<rmax&&a[qmax[rmax]]<=a[id]) --rmax;
    qmax[++rmax]=id;
    while (lmin<rmin&&a[qmin[rmin]]>=a[id]) --rmin;
    qmin[++rmin]=id;
}
hp pop(int id)
{
    while (lmax<rmax&&qmax[lmax+1]<id) ++lmax;
    int Max=a[qmax[lmax+1]];
    while (lmin<rmin&&qmin[lmin+1]<id) ++lmin;
    int Min=a[qmin[lmin+1]];
    return (hp){Max,Min};
}
int main()
{
    scanf("%d%d",&k,&n);
    for (int i=1;i<=n;++i) scanf("%d",&a[i]);
    l=r=1;lmax=rmax=lmin=rmin=0;push(l);
    while (l<=n)
    {
        if (l>r)
        {
            r=l;lmax=rmax=lmin=rmin=0;push(l);
        }
        while (r<n)
        {
            hp now=pop(l);
            if (a[r+1]>now.Max)
                {
  if (a[r+1]-now.Min>k) break;}
            else if (a[r+1]<now.Min)
                {
  if (now.Max-a[r+1]>k) break;}
            ++r;push(r);
        }
        ans=max(ans,r-l+1);
        ++l;
    }
    printf("%d\n",ans);
}

单调栈

// BZOJ 1657
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 50005

int n,h[N],val[N],l[N],r[N],Max,stack[N],top,sum[N],ans;

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%d%d",&h[i],&val[i]),l[i]=1,r[i]=n;

    top=0;
    for (int i=1;i<=n;++i)
    {
        while (top&&h[stack[top]]<h[i])
        {
            r[stack[top]]=i-1;
            --top;
        }
        stack[++top]=i;
    }
    top=0;
    for (int i=n;i>=1;--i)
    {
        while (top&&h[stack[top]]<h[i])
        {
            l[stack[top]]=i+1;
            --top;
        }
        stack[++top]=i;
    }
    for (int i=1;i<=n;++i) sum[l[i]-1]+=val[i],sum[r[i]+1]+=val[i];
    for (int i=1;i<=n;++i) ans=max(ans,sum[i]);
    printf("%d\n",ans);
}

St表

// poj3264
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 50005
#define sz 16
using namespace std;  
int n,m,l,r,Max,Min;
int a[N],lg2[N],stmax[N][sz],stmin[N][sz];
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1,p=0;i<=n;++i)
    {
   
        while ((1<<p)<=i) ++p;
        lg2[i]=p-1;
    }
    for (int i=1;i<=n;++i)
      scanf("%d",&a[i]),stmax[i][0]=stmin[i][0]=a[i];
    for (int j=1;j<sz;++j)
        for (int i=1;i<=n;++i)
          if (i+(1<<j)-1<=n)
            {
   
                stmax[i][j]=max(stmax[i][j-1],stmax[i+(1<<(j-1))][j-1]);
                stmin[i][j]=min(stmin[i][j-1],stmin[i+(1<<(j-1))][j-1]);
            }
    for (int i=1;i<=m;++i)
    {
   
        scanf("%d%d",&l,&r);
        if (l>r) swap(l,r);
        int k=lg2[r-l+1];
        Max=max(stmax[l][k],stmax[r-(1<<k)+1][k]);
        Min=min(stmin[l][k],stmin[r-(1<<k)+1][k]);
        printf("%d\n",Max-Min);
    }
}

Lca

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100005
#define sz 17

int n,m,x,y;
int tot,point[N],nxt[N*2],v[N*2];
int f[N][sz+5],h[N],size[N];
struct hp{
  int pre,pt;};


void addedge(int x,int y)
{
    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;
    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void build(int x,int fa,int dep)
{
    h[x]=dep; size[x]=1;
    for (int i=1;i<sz;++i)
    {
        if ((h[x]-(1<<i))<1) break;
        f[x][i]=f[f[x][i-1]][i-1];
    }
    for (int i=point[x];i;i=nxt[i])
        if (v[i]!=fa)
        {
            f[v[i]][0]=x;
            build(v[i],x,dep+1);
            size[x]+=size[v[i]];
        }
}
int lca(int x,int y)
{
    if (h[x]<h[y]) swap(x,y);
    int k=h[x]-h[y];
    for (int i=0;i<sz;++i)
        if ((1<<i)&k) x=f[x][i];
    if (x==y) return x;
    for (int i=sz-1;i>=0;--i)
        if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
    return f[x][0];
}
hp find(int x,int high)
{
    int xx=x;
    for (int i=sz-1;i>=0;--i)
        while (h[f[x][i]]>high) xx=x,x=f[x][i];
    xx=x,x=f[x][0];
    hp ans;ans.pre=xx,ans.pt=x;
    return ans;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<n;++i)
    {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    build(1,0,1);
    scanf("%d",&m);
    for (int i=1;i<=m;++i)
    {
        scanf("%d%d",&x,&y);
        if (h[x]<h[y]) swap(x,y);
        if (x==y)
        {
            printf("%d\n",n);
            continue;
        }
        int r=lca(x,y);
        int len=h[x]-h[r]+h[y]-h[r];
        if (len%2)
        {
            puts("0");
            continue;
        }
        len/=2;
        int high=h[x]-len;
        if (high==h[r])
        {
            hp date1=find(x,high);
            hp date2=find(y,high);
            printf("%d\n",n-size[r]+size[r]-size[date1.pre]-size[date2.pre]);
        }
        else
        {
            hp date=find(x,high);
            printf("%d\n",size[date.pt]-size[date.pre]);
        }
    }
}

Splay

// BZOJ 3224
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 300005

int n,opt,x,root,sz;
int f[N],ch[N][2],key[N],size[N],cnt[N];

void clear(int x)
{
    f[x]=ch[x][0]=ch[x][1]=key[x]=size[x]=cnt[x]=0;
}
int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    size[x]=cnt[x]+size[ch[x][0]]+size[ch[x][1]];
}
void rotate(int x)
{
    int old=f[x],oldf=f[old],wh=get(x);
    ch[old][wh]=ch[x][wh^1];
    if (ch[old][wh]) f[ch[old][wh]]=old;
    ch[x][wh^1]=old;
    f[old]=x;
    if (oldf) ch[oldf][ch[oldf][1]==old]=x;
    f[x]=oldf;
    update(old);
    update(x);
}
void splay(int x)
{
    for (int fa;fa=f[x];rotate(x))
        if (f[fa])
            rotate( (get(x)==get(fa))?fa:x );
    root=x;
}
void insert(int x)
{
    if (!root)
    {
        root=++sz;
        cnt[sz]=size[sz]=1;key[sz]=x;
        return;
    }
    int now=root,fa=0;
    while (1)
    {
        if (x==key[now])
        {
            cnt[now]++;
            update(now);
            splay(now);
            break;
        }
        fa=now;
        now=ch[now][x>key[now]];
        if (!now)
        {
            ++sz;
            f[sz]=fa;ch[fa][x>key[fa]]=sz;
            size[sz]=cnt[sz]=1;
            key[sz]=x;
            update(fa);
            splay(sz);
            break;
        }
    }
}
int find(int x)
{
    int now=root,ans=0;
    while (1)
    {
        if (x<key[now]) now=ch[now][0];
        else
        {
            ans+=size[ch[now][0]];
            if (x==key[now])
            {
                splay(now);
                return ans+1;
            }
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}
int findx(int x)
{
    int now=root;
    while (1)
    {
        if (x<=size[ch[now][0]]) now=ch[now][0];
        else
        {
            x-=size[ch[now][0]];
            if (x<=cnt[now])
            {
                splay(now);
                return key[now];
            }
            x-=cnt[now];
            now=ch[now][1];
        }
    }
}
int pre()
{
    int now=ch[root][0];
    while (ch[now][1]) now=ch[now][1];
    return now;
}
int nxt()
{
    int now=ch[root][1];
    while (ch[now][0]) now=ch[now][0];
    return now;
}
void del(int x)
{
    int wh=find(x);
    if (cnt[root]>1)
    {
        --cnt[root];
        update(root);
        return;
    }
    if (!ch[root][0]&&!ch[root][1])
    {
        clear(root);
        root=0;
        return;
    }
    if (!ch[root][0])
    {
        int oldroot=root;
        root=ch[oldroot][1];
        f[root]=0;
        clear(oldroot);
        return;
    }
    if (!ch[root][1])
    {
        int oldroot=root;
        root=ch[oldroot][0];
        f[root]=0;
        clear(oldroot);
        return;
    }
    int oldroot=root;
    int leftbig=pre();splay(leftbig);
    ch[root][1]=ch[oldroot][1];
    f[ch[root][1]]=root;
    clear(oldroot);
    update(root);
    return;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
    {
        scanf("%d%d",&opt,&x);
        switch(opt)
        {
            case 1:
                {
                    insert(x);
                    break;
                }
            case 2:
                {
                    del(x);
                    break;
                }
            case 3:
                {
                    int ans=find(x);
                    printf("%d\n",ans);
                    break;
                }
            case 4:
                {
                    int ans=findx(x);
                    printf("%d\n",ans);
                    break;
                }
            case 5:
                {
                    insert(x);
                    printf("%d\n",key[pre()]);
                    del(x);
                    break;
                }
            case 6:
                {
                    insert(x);
                    printf("%d\n",key[nxt()]);
                    del(x);
                    break;
                }
        }
    }
    return 0;
}
// BZOJ 3223
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100005
#define inf 1000000000

int n,m,l,r,root;
int a[N],f[N],ch[N][2],size[N],key[N],delta[N];

int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
void pushdown(int x)
{
    if (x&&delta[x])
    {
        delta[ch[x][0]]^=1;
        delta[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
        delta[x]=0;
    }
}
int build(int l,int r,int fa)
{
    if (l>r) return 0;
    int mid=(l+r)>>1;
    f[mid]=fa;key[mid]=a[mid];size[mid]=1;
    int lch=build(l,mid-1,mid);
    int rch=build(mid+1,r,mid);
    ch[mid][0]=lch,ch[mid][1]=rch;
    update(mid);
    return mid;
}
void rotate(int x)
{
    pushdown(f[x]);
    pushdown(x);
    int old=f[x],oldf=f[old],wh=get(x);
    ch[old][wh]=ch[x][wh^1];
    if (ch[old][wh]) f[ch[old][wh]]=old;
    ch[x][wh^1]=old;
    f[old]=x;
    if (oldf) ch[oldf][ch[oldf][1]==old]=x;
    f[x]=oldf;
    update(old);
    update(x);
}
void splay(int x,int tar)
{
    for (int fa;(fa=f[x])!=tar;rotate(x))
        if (f[fa]!=tar)
            rotate( (get(x)==get(fa))?fa:x );
    if (!tar) root=x;
}
int find(int x)
{
    int now=root;
    while (1)
    {
        pushdown(now);
        if (x<=size[ch[now][0]]) now=ch[now][0];
        else
        {
            x-=size[ch[now][0]];
            if (x==1) return now;
            x-=1;
            now=ch[now][1];
        }
    }
}
void write(int x)
{
    pushdown(x);
    if (ch[x][0]) write(ch[x][0]);
    if (key[x]!=-inf&&key[x]!=inf) printf("%d ",key[x]);
    if (ch[x][1]) write(ch[x][1]);
}
int main()
{
    scanf("%d%d",&n,&m);
    a[1]=-inf;a[n+2]=inf;
    for (int i=1;i<=n;++i) a[i+1]=i;
    root=build(1,n+2,0);
    for (int i=1
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值