zoukankan      html  css  js  c++  java
  • bzoj 1500 维修数列

        splay乱搞。

        调了两个多小时。。。这辈子再也不想写splay了。。。

        维护左边最大连续和右边最大连续,维护两个标记,无脑push_down、push_up就行了。

        注意最大连续和至少要包含一个数。

        

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #define N 500010
      6 #define M 5000005
      7 #define inf 0x3f3f3f3f
      8 #define lc(x) ch[x][0]
      9 #define rc(x) ch[x][1]
     10 using namespace std;
     11 int n,m;
     12 int cnt;
     13 int a[N],root,ch[N][2],tmproot;
     14 int sum[N],ls[N],rs[N],mx[N],ji[N];
     15 int lazy[N],size[N],zhi[N],fa[N];
     16 int s[M],tot2;
     17 inline int mxx(int x,int y)
     18 {
     19     if(x>y)return x;return y;
     20 }
     21 // ls 左边最大 rs 右边最大
     22 void push_up(int x)
     23 {
     24     size[x]=size[lc(x)]+size[rc(x)]+1;
     25     sum[x]=sum[lc(x)]+sum[rc(x)]+zhi[x];
     26     mx[x]=mxx(mx[lc(x)],mxx(mx[rc(x)],zhi[x]+rs[lc(x)]+ls[rc(x)]));
     27     ls[x]=mxx(0,mxx(ls[lc(x)],sum[lc(x)]+zhi[x]+ls[rc(x)]));
     28     rs[x]=mxx(0,mxx(rs[rc(x)],sum[rc(x)]+zhi[x]+rs[lc(x)]));
     29     return ;
     30 }
     31 void push_down(int x)
     32 {
     33     int l=ch[x][0];int r=ch[x][1];
     34     if(ji[x]!=-1001)
     35     {
     36         ji[l]=ji[r]=ji[x];
     37         sum[l]=size[l]*ji[x];sum[r]=size[r]*ji[x];
     38         zhi[l]=zhi[r]=ji[x];
     39         if(ji[x]>0)ls[l]=rs[l]=mx[l]=sum[l],ls[r]=rs[r]=mx[r]=sum[r];
     40         else ls[l]=rs[l]=0,ls[r]=rs[r]=0,mx[l]=mx[r]=ji[x];
     41         ji[x]=-1001;
     42     }
     43     if(lazy[x]==1)
     44     {
     45         lazy[l]^=1;lazy[r]^=1;
     46         swap(ch[l][0],ch[l][1]);
     47         swap(ch[r][0],ch[r][1]);
     48         swap(ls[l],rs[l]);swap(ls[r],rs[r]);
     49         lazy[x]=0;
     50     }
     51     sum[0]=ls[0]=rs[0]=0;mx[0]=-inf;
     52     return ;
     53 }
     54 void build(int x,int l,int r)
     55 {
     56     if(l==r)
     57     {
     58         size[x]=1;
     59         sum[x]=zhi[x]=mx[x]=a[l];
     60         rs[x]=ls[x]=max(0,a[l]);
     61         return ;
     62     }
     63     int mid=(l+r)>>1;
     64     if(l!=mid)
     65     {
     66         if(tot2)ch[x][0]=s[tot2--];
     67         else ch[x][0]=++cnt;
     68         fa[ch[x][0]]=x,build(ch[x][0],l,mid-1);
     69     }
     70     if(tot2)ch[x][1]=s[tot2--];
     71     else ch[x][1]=++cnt;
     72     fa[ch[x][1]]=x,build(ch[x][1],mid+1,r);
     73     zhi[x]=a[mid];
     74     push_up(x);
     75 }
     76 void rotate(int p)
     77 {
     78     int q=fa[p],y=fa[q],x=(ch[q][1]==p);
     79     ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
     80     ch[p][x^1]=q;fa[q]=p;
     81     fa[p]=y;
     82     if(y)
     83     {
     84         if(ch[y][1]==q)ch[y][1]=p;
     85         else ch[y][0]=p;
     86     }
     87     push_up(q);
     88     return ;
     89 }
     90 void splay(int x,int yy)
     91 {
     92     for(int y;y=fa[x];rotate(x))
     93     {
     94         if(y==yy)break;
     95         if(fa[y]!=yy)
     96         {
     97             if((lc(fa[y])==y&&lc(y)==x)||(rc(fa[y])==y&&rc(y)==x))rotate(y);
     98             else rotate(x);
     99         }
    100     }
    101     push_up(x);
    102     if(!yy)root=x;
    103 }
    104 int find(int k,int x)
    105 {
    106     push_down(k);
    107     int l=lc(k),r=rc(k);
    108     if(size[l]+1==x)return k;
    109     if(size[l]+1>x)return find(l,x);
    110     else return find(r,x-size[l]-1);
    111 }
    112 char c[20];
    113 void dfs(int x)
    114 {
    115     if(!x)return ;
    116     cout<<zhi[x]<<' '<<sum[x]<<' '<<size[x]<<endl;
    117     dfs(ch[x][0]);dfs(ch[x][1]);
    118 }
    119 void del(int x)
    120 {
    121     if(!x)return ;
    122     s[++tot2]=x;ji[x]=-1001;lazy[x]=0;sum[x]=0;
    123     del(ch[x][0]);del(ch[x][1]);
    124     ch[x][0]=ch[x][1]=0;fa[x]=0;size[x]=0;
    125 }
    126 int main()
    127 {
    128     scanf("%d%d",&n,&m);
    129     mx[0]=-inf;
    130     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    131    for(int i=1;i<N;i++)ji[i]=-1001;
    132     root=++cnt;
    133     a[0]=a[n+1]=0;
    134     build(cnt,0,n+1);
    135     int t1,t2,t3,t4,t5,t6;
    136     for(int i=1;i<=m;i++)
    137     {
    138         scanf("%s",c);
    139         if(c[0]=='I')
    140         {
    141             scanf("%d%d",&t1,&t2);
    142             for(int j=1;j<=t2;j++)scanf("%d",&a[j]);
    143             if(tot2)tmproot=s[tot2--];
    144             else tmproot=++cnt;
    145             build(tmproot,1,t2);
    146             t1++;t3=find(root,t1);
    147             splay(t3,0);
    148             t4=ch[t3][1];push_down(t4);
    149             while(ch[t4][0])t4=ch[t4][0],push_down(t4);
    150             splay(t4,t3);ch[t4][0]=tmproot;fa[tmproot]=t4;push_up(t4);push_up(t3);
    151         }
    152         else if(c[0]=='D')
    153         {
    154             scanf("%d%d",&t1,&t2);
    155             t3=find(root,t1);
    156             splay(t3,0);
    157             t2=t2+t1+1;
    158             t4=find(root,t2);
    159             splay(t4,t3);
    160             del(ch[t4][0]);
    161             ch[t4][0]=0;
    162             push_up(t4);push_up(t3);
    163         }
    164         else if(c[0]=='R')
    165         {
    166             scanf("%d%d",&t1,&t2);
    167             t3=find(root,t1);splay(t3,0);t2=t2+t1+1;t4=find(root,t2);
    168             splay(t4,t3);
    169             t5=ch[t4][0];
    170             lazy[t5]^=1;swap(ch[t5][0],ch[t5][1]);swap(ls[t5],rs[t5]);
    171             push_up(t4);push_up(t3);
    172         }
    173         else if(c[0]=='G')
    174         {
    175             scanf("%d%d",&t1,&t2);
    176             t3=find(root,t1);
    177             splay(t3,0);t2=t2+t1+1;
    178             t4=find(root,t2);
    179             splay(t4,t3);
    180             printf("%d
    ",sum[ch[t4][0]]);
    181         }
    182         else if(c[0]=='M'&&c[4]=='S')
    183         {
    184             t1=find(root,1);
    185             splay(t1,0);
    186             t2=find(root,size[root]);
    187             splay(t2,t1);
    188             printf("%d
    ",mx[ch[t2][0]]);
    189         }
    190         else
    191         {
    192             scanf("%d%d%d",&t1,&t2,&t6);
    193             t3=find(root,t1);splay(t3,0);t2=t2+t1+1;
    194             t4=find(root,t2);
    195             splay(t4,t3);
    196             t5=ch[t4][0];
    197             ji[t5]=t6;zhi[t5]=t6;sum[t5]=size[t5]*t6;
    198             if(t6>0)ls[t5]=rs[t5]=mx[t5]=sum[t5];
    199             else ls[t5]=rs[t5]=0,mx[t5]=t6;
    200             push_up(t4);push_up(t3);
    201         }
    202     }
    203     return 0;
    204 }
  • 相关阅读:
    Android签名详解(debug和release)
    Java反射机制的学习
    Android应用开发中如何使用隐藏API(转)
    asp.net购物车,订单以及模拟支付宝支付(二)---订单表
    asp.net购物车,订单以及模拟支付宝支付(一)---购物车表及添加购物车流程
    asp.net权限控制的方式
    .Net使用程序发送邮件时的问题
    Word2016“此功能看似已中断 并需要修复”问题解决办法
    C#字符串来袭——因为爱,所以爱
    C#时间的味道——任时光匆匆我只在乎你
  • 原文地址:https://www.cnblogs.com/ezyzy/p/6202481.html
Copyright © 2011-2022 走看看