zoukankan      html  css  js  c++  java
  • Codevs 4927 线段树练习5(分块)

    4927 线段树练习5
    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 黄金 Gold
    题目描述 Description
    有n个数和5种操作
    add a b c:把区间[a,b]内的所有数都增加c
    set a b c:把区间[a,b]内的所有数都设为c
    sum a b:查询区间[a,b]的区间和
    max a b:查询区间[a,b]的最大值
    min a b:查询区间[a,b]的最小值
    输入描述 Input Description
    第一行两个整数n,m,第二行n个整数表示这n个数的初始值
    接下来m行操作,同题目描述
    输出描述 Output Description
    对于所有的sum、max、min询问,一行输出一个答案
    样例输入 Sample Input
    10 6
    3 9 2 8 1 7 5 0 4 6
    add 4 9 4
    set 2 6 2
    add 3 8 2
    sum 2 10
    max 1 7
    min 3 6
    样例输出 Sample Output
    49
    11
    4
    数据范围及提示 Data Size & Hint
    10%:1

    /*
    分块. 
    这题改了一个下午.
    直接呵呵了.
    思路是挺简单的,但是实现起来有点鬼畜.
    哎就不说啥了 码力太弱.
    注意几个细节.
    重构不完整的块时要注意区间和贡献的计算还有每个位置的值应该是啥.
    ★x,y在同一个块中不要忘了重构y到块末的贡献.
    然后区分该区间是否被"set",重构的话把标记清掉.
    感谢前人的分块solution code 给了我debug一个下午的可能hhh.
    */
    #include<cstdio>
    #include<cmath>
    #include<iostream>
    #define MAXN 200001
    #define MAXM 200001
    #define LL long long
    #define INF 1e18
    using namespace std;
    int n,m,q,belong[MAXN];
    LL ans,s[MAXN],sum[MAXM],tot,tag[MAXM],bj[MAXM],max1[MAXM],min1[MAXM];
    bool b[MAXM];
    LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
        return x*f;
    }
    void sloveadd(int x,int y,LL z)
    {
        for(int i=x;i<=min(belong[x]*m,y);i++) 
        {
            if(b[belong[x]]) s[i]=bj[belong[x]]+tag[belong[x]]+z;
            else s[i]+=z+tag[belong[x]];
            sum[belong[x]]+=z;
        }
        if(belong[x]==belong[y])
        {
            for(int i=y+1;i<=belong[y]*m;i++)
            {
                if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
                else s[i]+=tag[belong[y]];
            }
        }
        for(int i=(belong[x]-1)*m+1;i<=x-1;i++)
        {
            if(b[belong[x]]) s[i]=bj[belong[x]]+tag[belong[x]];
            else s[i]+=tag[belong[x]];
        }
        b[belong[x]]=false;tag[belong[x]]=0;
        max1[belong[x]]=-INF,min1[belong[x]]=INF;
        for(int i=(belong[x]-1)*m+1;i<=min(n,belong[x]*m);i++)
        {
            max1[belong[i]]=max(max1[belong[i]],s[i]+tag[belong[i]]);
            min1[belong[i]]=min(min1[belong[i]],s[i]+tag[belong[i]]);
        }
        for(int i=belong[x]+1;i<=belong[y]-1;i++) 
        sum[i]+=z*m,tag[i]+=z,max1[i]+=z,min1[i]+=z;
        if(belong[x]!=belong[y])
        {
          for(int i=(belong[y]-1)*m+1;i<=y;i++) 
          {
            if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]]+z;
            else s[i]+=z+tag[belong[y]];
            sum[belong[y]]+=z;
          }
          for(int i=y+1;i<=min(n,belong[y]*m);i++)
          {
              if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
              else s[i]+=tag[belong[y]];
          }
          max1[belong[y]]=-INF,min1[belong[y]]=INF;
          b[belong[y]]=false;tag[belong[y]]=0;
          max1[belong[y]]=-INF,min1[belong[y]]=INF;
          for(int i=(belong[y]-1)*m+1;i<=min(n,belong[y]*m);i++)
          {
              max1[belong[y]]=max(max1[belong[y]],s[i]+tag[belong[y]]);
              min1[belong[y]]=min(min1[belong[y]],s[i]+tag[belong[y]]);
          }
        }
        return ;
    }
    void slovechange(int x,int y,LL z)
    {
        for(int i=x;i<=min(belong[x]*m,y);i++) 
        {
            if(!b[belong[x]]) sum[belong[x]]+=z-s[i]-tag[belong[x]];
            else sum[belong[x]]+=z-bj[belong[x]]-tag[belong[x]];
            s[i]=z;
        }
        if(belong[x]==belong[y])
        {
            for(int i=y+1;i<=belong[y]*m;i++)
            {
                if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
                else s[i]+=tag[belong[y]];
            }
        }
        for(int i=(belong[x]-1)*m+1;i<=x-1;i++)
        {
            if(b[belong[x]]) s[i]=bj[belong[x]]+tag[belong[x]];
            else s[i]+=tag[belong[x]];
        }
        b[belong[x]]=false;tag[belong[x]]=0;
        max1[belong[x]]=-INF,min1[belong[x]]=INF;
        for(int i=(belong[x]-1)*m+1;i<=min(n,belong[x]*m);i++)
        {
            max1[belong[i]]=max(max1[belong[i]],s[i]+tag[belong[i]]);
            min1[belong[i]]=min(min1[belong[i]],s[i]+tag[belong[i]]);
        }
        for(int i=belong[x]+1;i<=belong[y]-1;i++)
          sum[i]=z*m,tag[i]=0,b[i]=true,max1[i]=z,min1[i]=z,bj[i]=z;
        if(belong[x]!=belong[y])
        {
          for(int i=(belong[y]-1)*m+1;i<=y;i++) 
          {
              if(!b[belong[y]]) sum[belong[y]]+=z-s[i]-tag[belong[y]];
              else sum[belong[y]]+=z-bj[belong[y]]-tag[belong[y]];
              s[i]=z;
          }
          for(int i=y+1;i<=min(n,belong[y]*m);i++)
          {
              if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
              else s[i]+=tag[belong[y]];
          }
          b[belong[y]]=false;tag[belong[y]]=0;
          max1[belong[y]]=-INF,min1[belong[y]]=INF;
          for(int i=(belong[y]-1)*m+1;i<=min(n,belong[y]*m);i++)
          {
              max1[belong[y]]=max(max1[belong[y]],s[i]+tag[belong[y]]);
              min1[belong[y]]=min(min1[belong[y]],s[i]+tag[belong[y]]);
          }
        }
        return ;
    }
    LL slovequerysum(int x,int y)
    {
        ans=0;
        for(int i=x;i<=min(belong[x]*m,y);i++)
        {
            if(!b[belong[x]]) ans+=s[i]+tag[belong[i]];
            else ans+=bj[belong[i]]+tag[belong[i]];
        }
        for(int i=belong[x]+1;i<=belong[y]-1;i++) ans+=sum[i];
        if(belong[x]!=belong[y])
        {
            for(int i=(belong[y]-1)*m+1;i<=y;i++)
            {
                if(!b[belong[y]]) ans+=s[i]+tag[belong[y]];
                else ans+=bj[belong[y]]+tag[belong[y]];
            }
        }
        return ans;
    }
    LL slovequerymax(int x,int y)
    {
        ans=-INF;
        for(int i=x;i<=min(belong[x]*m,y);i++)
        {
            if(!b[belong[x]]) ans=max(ans,s[i]+tag[belong[i]]);
            else ans=max(ans,bj[belong[i]]+tag[belong[i]]);
        }
        for(int i=belong[x]+1;i<=belong[y]-1;i++) ans=max(ans,max1[i]);
        if(belong[x]!=belong[y])
        {
            for(int i=(belong[y]-1)*m+1;i<=y;i++)
            {
                if(!b[belong[y]]) ans=max(ans,s[i]+tag[belong[y]]);
                else ans=max(ans,bj[belong[y]]+tag[belong[y]]);
            }
        }
        return ans;
    }
    LL slovequerymin(int x,int y)
    {
        ans=INF;
        for(int i=x;i<=min(belong[x]*m,y);i++)
        {
            if(!b[belong[x]]) ans=min(ans,s[i]+tag[belong[i]]);
            else ans=min(ans,bj[belong[i]]+tag[belong[i]]);
        }
        for(int i=belong[x]+1;i<=belong[y]-1;i++) ans=min(ans,min1[i]);
        if(belong[x]!=belong[y])
        {
            for(int i=(belong[y]-1)*m+1;i<=y;i++)
            {
                if(!b[belong[y]]) ans=min(ans,s[i]+tag[belong[y]]);
                else ans=min(ans,bj[belong[y]]+tag[belong[y]]);
            }
        }
        return ans;
    }
    int main()
    {
        int x,y,z;LL k;char ch[6];
        n=read();q=read();
        m=sqrt(n);
        for(int i=1;i<=n;i++) belong[i]=(i-1)/m+1;
        for(int i=1;i<=belong[n];i++) max1[i]=-INF,min1[i]=INF;
        for(int i=1;i<=n;i++)
        {
          s[i]=read(),sum[belong[i]]+=s[i];
          max1[belong[i]]=max(max1[belong[i]],s[i]);
          min1[belong[i]]=min(min1[belong[i]],s[i]);
        }
        while(q--)
        {
            scanf("%s",ch);x=read(),y=read();
            if(ch[0]=='a') k=read(),sloveadd(x,y,k);
            else if(ch[1]=='e') k=read(),slovechange(x,y,k);
            else if(ch[1]=='u') printf("%lld
    ",slovequerysum(x,y));
            else if(ch[1]=='a') printf("%lld
    ",slovequerymax(x,y));
            else if(ch[1]=='i') printf("%lld
    ",slovequerymin(x,y));
        }
        return 0;
    }
  • 相关阅读:
    聚类算法学习-kmeans,kmedoids,GMM
    hdu
    高仿精仿微信应用ios源码下载
    UVA 116 Unidirectional TSP 经典dp题
    [置顶] 动态规划之切割钢条
    poj
    求解printf函数?
    实现多文件上传在iOS开发中
    开源DirectShow分析器和解码器: LAV Filter
    <Win32_20>纯c语言版的打飞机游戏出炉了^_^
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068058.html
Copyright © 2011-2022 走看看