• POJ3468:A Simple Problem with Integers(线段树模板)


    A Simple Problem with Integers

    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 149972   Accepted: 46526

    题目链接:http://poj.org/problem?id=3468

    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

    题解:

    线段树模板题,注意一下lazy标记的下传操作,标记也是long long 型的。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;
    int n,m;
    ll a[N];
    ll ans;
    struct Tree{
        int l,r;
        ll f,w;
    }tre[(N<<2)+1];
    void build(int o,int l,int r){
        tre[o].l=l;tre[o].r=r;tre[o].f=0;
        if(l==r){
            tre[o].w=a[l];
            return ;
        }
        int mid=l+r>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        tre[o].w=tre[o<<1].w+tre[o<<1|1].w;
    }
    void down(int o){
        tre[o<<1].f+=tre[o].f;
        tre[o<<1|1].f+=tre[o].f;
        tre[o<<1].w+=tre[o].f*(tre[o<<1].r-tre[o<<1].l+1);
        tre[o<<1|1].w+=tre[o].f*(tre[o<<1|1].r-tre[o<<1|1].l+1);
        tre[o].f=0;
    }
    void update(int o,int l,int r,int val){
        int L=tre[o].l,R=tre[o].r;
        if(L>=l && R<=r){
            tre[o].w+=(ll)val*(R-L+1);
            tre[o].f+=val;
            return ;
        }
        down(o);
        int mid=L+R>>1;
        if(l<=mid) update(o<<1,l,r,val);
        if(r>mid) update(o<<1|1,l,r,val);
        tre[o].w=tre[o<<1].w+tre[o<<1|1].w;
    }
    void query(int o,int l,int r){
        int L=tre[o].l,R=tre[o].r;
        if(L>=l && R<=r){
            ans+=tre[o].w;
            return ;
        }
        down(o);
        int mid=L+R>>1;
        if(l<=mid) query(o<<1,l,r);
        if(r>mid) query(o<<1|1,l,r);
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        build(1,1,n);
        char s[5];
        for(int i=1;i<=m;i++){
            scanf("%s",s);
            if(s[0]=='Q'){
                int l,r;ans=0;
                scanf("%d%d",&l,&r);
                query(1,l,r);
                printf("%I64d
    ",ans);
            }else{
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                update(1,a,b,c);
            }
        }
        return 0;
    }
  • 相关阅读:
    实现图片加载时显示百分比思路——serverpush
    我对大项目的看法(定义)
    lucene
    Access常用内置SQL函数
    闰年算法
    DotFuscator使用步骤
    软件加密狗破解思路和方法
    Lucene(.net)学习
    .Net 代码安全保护产品DNGuard HVM使用
    为什么动态创建的控件没有显示出来
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10398471.html
Copyright © 2020-2023  润新知