zoukankan      html  css  js  c++  java
  • poj3468 线段树+lazy标记

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 92921   Accepted: 28910
    Case Time Limit: 2000MS

    Description

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input

    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample Input

    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4
    

    Sample Output

    4
    55
    9
    15

    Hint

    The sums may exceed the range of 32-bit integers.
    题目大意:
    给你n个数,然后会进行q次操作,Q a b为查询区间[a,b]所有数的和sum,
    C a b c ,为将区间[a,b]所有数整体加c。
    思路分析:如果只是改变某一个数的值,我们直接用一个裸的线段树来维护就行,但是这个题是
    区间的值整体发生变化,如果再进行和以前一样的操作,则update的复杂度变成了LlogL(L为
    区间长度)很不高效,势必超时,因此我们可以采用lazy标记的方法,用空间换时间,开一个lazy
    数组,每次不必访问到根节点,只需要到更新的区间所在的节点就可以,大大提高了程序效率,只需要
    明确lazy标记什么时候往儿子传就可以,一个是要往子节点更新,另一个是即将要查询子节点,都要将
    lazy标记向下传一层,传给自己的儿子,增值会爆int,需要用long long
    代码:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int maxn=100000+100;
    typedef long long ll;
    ll tree[3*maxn];
    ll lazy[3*maxn];
    ll num[maxn];
    void build (int p,int l,int r)
    {
        if(l==r) {tree[p]=num[l];return;}
        int mid=(l+r)>>1;
        build(p<<1,l,mid);
        build((p<<1)|1,mid+1,r);
        tree[p]=tree[p<<1]+tree[(p<<1)|1];
    }
    void pushdown(int p,int m)//把lazy标记下放到儿子节点
    {
        if(lazy[p])
        {
            lazy[p<<1]+=lazy[p];
            lazy[(p<<1)|1]+=lazy[p];
            tree[p<<1]+=(m-(m>>1))*lazy[p];
            tree[(p<<1)|1]+=(m>>1)*lazy[p];
            lazy[p]=0;
        }
    }
    void update(int p,int l,int r,int x,int y,int v)
    {
        if(x<=l&&y>=r)
        {
            lazy[p]+=v;
            tree[p]+=(ll)v*(r-l+1);//区间长度
            return;
        }
        pushdown(p,r-l+1);
        int mid=(l+r)>>1;
        if(y<=mid) update(p<<1,l,mid,x,y,v);
        else if(x>mid) update((p<<1)|1,mid+1,r,x,y,v);
        else {update(p<<1,l,mid,x,mid,v),update((p<<1)|1,mid+1,r,mid+1,y,v);}
        tree[p]=tree[p<<1]+tree[(p<<1)|1];
    }
    ll find(int p,int l,int r,int x,int y)
    {
        if(x<=l&&y>=r){return tree[p];}
        pushdown(p,r-l+1);
        int mid=(l+r)>>1;
        ll ans=0;
        if(y<=mid) ans=find(p<<1,l,mid,x,y);
        else if(x>mid) ans=find((p<<1)|1,mid+1,r,x,y);
        else ans=find(p<<1,l,mid,x,mid)+find((p<<1)|1,mid+1,r,mid+1,y);
        return ans;
    }
    int main()
    {
         int n,q;
         char s[5];
         int a,b,c;
        while(~scanf("%d%d",&n,&q))
        {
            memset(tree,0,sizeof(tree));
            memset(lazy,0,sizeof(lazy));
            for(int i=1;i<=n;i++)
                scanf("%lld",&num[i]);
            build(1,1,n);
            while(q--)
            {
                scanf("%s",s);
                if(s[0]=='Q')
                {
                    scanf("%d%d",&a,&b);
                    printf("%lld
    ",find(1,1,n,a,b));
                }
                else
                {
                    scanf("%d%d%d",&a,&b,&c);
                    update(1,1,n,a,b,c);
                }
            }
        }
    }
  • 相关阅读:
    Docker端口映射实现
    Docker容器访问控制
    Docker配置 DNS
    Docker快速配置指南
    Docker外部访问容器
    Docker容器如何互联
    Docker 备份、恢复、迁移数据卷
    19.30内置登录处理
    18.29SSM基础整合开发
    19.30内置登录处理
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5692325.html
Copyright © 2011-2022 走看看