Edison UVALive3488

本文介绍了一道关于序列操作的问题,并详细解析了如何利用FHQ Treap数据结构进行高效处理。通过实例演示了平衡树的应用场景及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题目大意

有一个0~n-1的序列,有m次操作,操作包含三个元素:pl,len,ti,表示这个操作进行ti次,每次将从pl+1开始的len个元素移到序列头部。
分析

看到题不难想到使用平衡树将需移动部分切出来挂到顶部。本题我使用fhqtreap来实现,但需要注意的是对于挪动部分使用的split不再以节点值为关键字切割而改用它在树中的排名作为关键字。具体实现详见代码。注意输出格式!!!!!!!!

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int root,son[1100000][2],a[1100000],rd[1100000],siz[1100000],cnt,n;
inline void up(int x){siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;}
inline int bnode(int x){
      siz[++cnt]=1;
      a[cnt]=x;
      rd[cnt]=rand();
      return cnt;
}
inline int gk(int rt,int k){
      while(1){
        if(k<=siz[son[rt][0]])rt=son[rt][0];
          else if(k==siz[son[rt][0]]+1)return rt;
          else k-=siz[son[rt][0]]+1,rt=son[rt][1];    
      }
}
inline void spt1(int rt,int k,int &x,int &y){
      if(!rt)x=y=0;
      else {
          if(a[rt]<=k)x=rt,spt1(son[rt][1],k,son[rt][1],y);
            else y=rt,spt1(son[rt][0],k,x,son[rt][0]);
          up(rt);
      }
}
inline int mer(int x,int y){
      if(!x||!y)return x+y;
      if(rd[x]<rd[y]){
          son[x][1]=mer(son[x][1],y);
          up(x);
          return x;
      }else {
          son[y][0]=mer(x,son[y][0]);
          up(y);
          return y;
      }
}
inline void spt(int sum,int rt,int k,int &x,int &y){
      if(!rt)x=y=0;
      else {
          if(sum+siz[son[rt][0]]+1<=k){
          x=rt,spt(sum+siz[son[rt][0]]+1,
          son[rt][1],k,son[rt][1],y);
        }else y=rt,spt(sum,son[rt][0],k,x,son[rt][0]);
          up(rt);
      }
}
inline void init(){
      root=cnt=0;
      memset(rd,0,sizeof(rd));
      memset(a,0,sizeof(a));
      memset(son,0,sizeof(son));
      memset(siz,0,sizeof(siz));
}
inline long long getans(int n){
      long long ans=0;
      int i;
      for(i=1;i<=n;i+=2)
        ans+=(long long)a[gk(root,i)];
      return ans;
}
inline void work(int _){
      int n,m,i,j,x,y,z,w,pl,len,ti;
      scanf("%d%d",&n,&m);
      for(i=0;i<n;i++){
          spt1(root,i,x,y);
          root=mer(mer(x,bnode(i)),y);
      }
      for(i=1;i<=m;i++){
          scanf("%d%d%d",&pl,&len,&ti);
          for(j=1;j<=ti;j++){
            spt(0,root,pl,x,y),spt(0,y,len,z,w);
            root=mer(z,mer(x,w));
        }
      }
      printf("%lld\n",getans(n));      
}
int main(){
      srand(time(0)+20021023);
      int t;
      scanf("%d",&t);
      for(int _=1;_<=t;_++){
          printf("Case %d:\n",_);
          init();
          work(_);
          if(_!=t)puts("");
      }
      return 0;
}

转载于:https://www.cnblogs.com/yzxverygood/p/9390741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值