zoukankan      html  css  js  c++  java
  • 【BZOJ1251】序列终结者

    Description

    网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。

    Input

    第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。

    Output

    对于每个第3种操作,给出正确的回答。

    Sample Input

    4 4
    1 1 3 2
    1 2 4 -1
    2 1 3
    3 2 4

    Sample Output

    2
    【数据范围】
    N<=50000,M<=100000。
     
    splay 。终于把这个splay的题写出来了。明天去挑战维护序列。
      1 #include<cstdio>
      2 #include<algorithm>
      3 #define inf 0x7fffffff
      4 using namespace std; 
      5 const int N=50010;
      6 int c[N][2],tag[N],w[N],mx[N],rev[N],size[N],fa[N],id[N];
      7 int m,n,rt;
      8 void pushdown(int x){
      9     int l=c[x][0],r=c[x][1];
     10     if (rev[x]){
     11         rev[l]^=1;rev[r]^=1;
     12         rev[x]=0;
     13         swap(c[x][0],c[x][1]);
     14     }
     15     if (tag[x]){
     16         if(l)tag[l]+=tag[x],mx[l]+=tag[x],w[l]+=tag[x];
     17         if (r)tag[r]+=tag[x],mx[r]+=tag[x],w[r]+=tag[x];
     18         tag[x]=0;
     19     }
     20 }
     21  
     22 void updata(int x){
     23     int l=c[x][0],r=c[x][1];
     24     size[x]=size[l]+size[r]+1;
     25     mx[x]=max(mx[l],mx[r]);
     26     mx[x]=max(mx[x],w[x]);
     27 }
     28  
     29 void rotate(int x,int &k){
     30     int y=fa[x],z=fa[y],l,r;
     31     if (c[y][0]==x)l=0;else l=1;r=l^1;
     32     if (y==k) k=x;
     33     else if (c[z][1]==y) c[z][1]=x;else c[z][0]=x;
     34     fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
     35     c[y][l]=c[x][r];c[x][r]=y;
     36     updata(y);updata(x);
     37 }
     38  
     39 void splay(int x,int &k){
     40     while (x!=k){
     41         int y=fa[x],z=fa[y];
     42         if (y!=k){
     43             if (c[y][0]==x^c[z][0]==y) rotate(x,k);
     44             else rotate(y,k);
     45         }
     46         rotate(x,k);
     47     }
     48 }
     49  
     50 int find(int &k,int rk){
     51     pushdown(k);
     52     int l=c[k][0],r=c[k][1];
     53     if (size[l]+1==rk) return k;
     54     if (size[l]>=rk) return find(l,rk);
     55     return find(r,rk-size[l]-1);
     56 }
     57  
     58 void build(int l,int r,int f){
     59     if (l>r) return;
     60     int mid=(l+r)>>1,now=id[mid],last=id[f];
     61     if (l==r){
     62         size[l]=1;tag[l]=rev[l]=0;
     63         fa[l]=last;
     64         if(l<f)c[last][0]=now;
     65         else c[last][1]=now;
     66         return;
     67     }
     68     build(l,mid-1,mid);build(mid+1,r,mid);
     69     fa[now]=last;updata(now);
     70     if (mid>=f)c[last][1]=now;else c[last][0]=now; 
     71 }
     72  
     73 void add(int l,int r,int v){
     74     int x=find(rt,l),y=find(rt,r+2);
     75     splay(x,rt); splay(y,c[x][1]);
     76     int z=c[y][0];
     77     tag[z]+=v;mx[z]+=v;w[z]+=v;
     78 }
     79  
     80 void rever(int l,int r){
     81     int x=find(rt,l),y=find(rt,r+2);
     82     splay(x,rt);splay(y,c[x][1]);
     83     int z=c[y][0];
     84     rev[z]^=1;
     85 }
     86  
     87 int solvemx(int l,int r){
     88     int x=find(rt,l),y=find(rt,r+2);
     89     splay(x,rt);splay(y,c[x][1]);
     90     int z=c[y][0];
     91     return mx[z];
     92 }
     93  
     94 int main(){
     95     scanf("%d%d",&n,&m);
     96     mx[0]=-inf;//注意这里有负数,所以mx[0]要定义最小值
     97     for (int i=1;i<=n+2;i++) id[i]=i;
     98     build(1,n+2,0);rt=(n+3)>>1;
     99     int opt,l,r,v;
    100     for (int i=0;i<m;i++){
    101         scanf("%d%d%d",&opt,&l,&r);
    102         switch (opt){
    103             case 1:scanf("%d",&v);add(l,r,v);break;
    104             case 2:rever(l,r);break; 
    105             case 3:printf("%d
    ",solvemx(l,r));break;
    106         }
    107         //for (int j=1;j<=n;j++)printf("%d ",solvemx(j,j));
    108     }
    109 }
  • 相关阅读:
    delphi TMemoryStream读取html文件中文乱码
    tfrxbarcode2dview 和 tfrxbarcode not found 解决办法
    delphi ForceDirectories 用法
    Java Scanner next() 与 nextLine() 区别
    delphi 用superobject解析json
    delphi 方法可以提前声明
    delphi 判断文件是否被占用
    mysql explain用法和结果的含义
    C# SpinWait
    js异步编程
  • 原文地址:https://www.cnblogs.com/wuminyan/p/5143526.html
Copyright © 2011-2022 走看看