zoukankan      html  css  js  c++  java
  • POJ-3468 A Simple Problem with Integers(线段树、段变化+段查询、模板)

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

    Description

    You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+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.

    Source

     
    题目大意:给N个数,对应编号1~N。对应两种操作:①求编号a-b对应数字的和。②编号a-b对应数字都加c。
     
    解题思路:线段树模板,用lazy思想,关键是要弄懂pushdown函数。理解了的话这类题目都可以看成是模板题。。
    个人对pushdown函数的理解:先用lazy[num]存放的是这个区间上所有的数应该变换的值,即编号为num的结点上的数实际值应该是当前值加上lazy[num]。于是对应左右结点对应的区间的和应该加上lazy[num]*对应长度。然后将lazy传给左右结点。当要使用到时再调用pushdown。(这个看个人理解。。这种想法可能只适合我自己。。。做题的时候弄懂的)
     
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int N=100005;
    const int maxn = N*3;
    long long a[maxn],val[maxn],lazy[maxn];
    int n,m,ql,qr,A,B,value;
    
    void pushdown(int num,int l)
    {
        if(lazy[num])
        {
            lazy[num*2] += lazy[num];
            lazy[num*2+1] += lazy[num];
    
            val[num*2] += (long long)lazy[num]*(l-(l/2));
            val[num*2+1] += (long long)lazy[num]*(l/2);
    
            lazy[num] = 0;
        }
    }
    
    void build(int num,int l,int r)
    {
        if(l==r)
        {
            val[num] = a[l];
            return ;
        }
        int mid = (l+r)/2;
        build(num*2,l,mid);
        build(num*2+1,mid+1,r);
        val[num] = val[num*2]+val[num*2+1];
    }
    
    long long Findsum(int num,int l,int r)
    {
        int mid = (l+r)/2;
        long long ans = 0;
        if(ql<=l&&qr>=r)
        {
            return val[num];
        }
        else
        {
            pushdown(num,r-l+1);
            if(mid>=ql)
            {
                ans += Findsum(num*2,l,mid);
            }
            if(mid<qr)
            {
                ans += Findsum(num*2+1,mid+1,r);
            }
        }
        return ans;
    }
    
    void update(int num,int l,int r)
    {
        if(A<=l&&B>=r)
        {
            lazy[num] += (long long)value;
            val[num] += (long long)value*(r-l+1);
            return ;
        }
        pushdown(num,r-l+1);
        int mid = (l+r)/2;
        if(A<=mid)
            update(num*2,l,mid);
        if(B>mid)
            update(num*2+1,mid+1,r);
        val[num] = val[num*2]+val[num*2+1];
    }
    
    int main()
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        build(1,1,n);
        char c;
        for(int i=1;i<=m;i++)
        {
            getchar();
            scanf("%c",&c);
            if(c=='Q')
            {
                scanf("%d %d",&ql,&qr);
                printf("%lld
    ",Findsum(1,1,n));
            }
            else
            {
                scanf("%d %d %d",&A,&B,&value);
                update(1,1,n);
            }
        }
    }
  • 相关阅读:
    Ant: Class not found: javac1.8
    (转)ant 使用指南
    炼数成金(dataguru)IT技能修炼
    [原创]记我的6年测试经历
    CentOS6.5 安装并配置vsftpd
    炫龙炎魔T1笔记本 Win7 系统安装
    Oracl数据库+PL/SQL安装与配置
    C# 面试题
    Linux安装Python3后,如何使用pip命令
    CentOS7 Python2 和Python3 共存(Python3安装)【转】
  • 原文地址:https://www.cnblogs.com/WWkkk/p/7366754.html
Copyright © 2011-2022 走看看