UESTC-1057 秋实大哥与花

学习一下线段树:
UESTC-1057 秋实大哥与花

题意:

秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前。

所以秋实大哥精心照料了很多花朵。现在所有的花朵排成了一行,每朵花有一个愉悦值。

秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉悦值都会增加一个相同的值v(v可能为负)。同时他想知道每次他唱完歌后这一段连续的花朵的愉悦值总和是多少。

Input

第一行有一个整数n,表示花朵的总数目。

第二行包含n个整数ai,表示第i朵花初始的愉悦值。

第三行包含一个整数m,表示秋实大哥唱了m天的歌。接下来m行,每行包含三个整数l r v,表示秋实大哥对着[l,r]这个区间内的花朵歌唱,每朵花的愉悦值增加了v。
1≤n,m,ai,|v|≤100000,1≤l≤r≤n。

Output

输出共m行,第i行表示秋实大哥完成第i天的歌唱后,那一段花朵的愉悦值总和。

Sample Input

3
0 0 0
3
1 2 1
1 2 -1
1 3 1

Sample Output

2
0
3
题目链接

#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <string>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

const int maxn=1e5+100;
int a[maxn];
int n,q;
struct node{
    int l,r;
    long long sum,lazy;
    void update(long long x){// x be the update-val or lazy.
        sum+=1LL*(r-l+1)*x;// update sum of interval
        lazy+=x;//update lazy in push_down and update operation.
    }
}tree[maxn*4];


void push_up(int root){
    tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;//update from son to father
}
void push_down(int root){
    int lazyval=tree[root].lazy;
    if(lazyval){//if lazy alreadly exist
        tree[root<<1].update(lazyval);//update lazy and sum, if lazy exist the sum must be update.
        tree[root<<1|1].update(lazyval);
        tree[root].lazy=0;//used.
    }
}

void build(int root,int l,int r){
    tree[root].l=l;//build the interval of node.
    tree[root].r=r;
    tree[root].sum=tree[root].lazy=0;//initialization the lazy and sum
    if(r==l){
        tree[root].sum=a[l];//build the leaf node
    }else{
        int mid=(l+r)/2;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
        push_up(root);//and update the father
    }

}

void update(int root,int l,int r,long long val){
    int L=tree[root].l,R=tree[root].r;
    if(l<=L && R<=r){// judge if belongs the interval.
        tree[root].update(val);
    }else{
        push_down(root);
        int mid=(L+R)/2;
        if(l<=mid){// intersect 
            update(root<<1,l,r,val);
        }
        if(r>mid){
            update(root<<1|1,l,r,val);
        }
        push_up(root);
    }
}

long long query(int root ,int l,int r){//similary with update operation
    int L=tree[root].l,R=tree[root].r;
    if(l<=L && R<=r){
        return tree[root].sum;
    }else{
        push_down(root);
        int mid=(L+R)/2;
        long long ans=0;
        if(l<=mid){
            ans+=query(root<<1,l,r);
        }
        if(r>mid){
            ans+=query(root<<1|1,l,r);
        }
        push_up(root);
        return ans;
    }
}

int main (){
    scanf ("%d",&n);
    for(int  i=1;i<=n;i++){
        scanf ("%d",&a[i]);
    }
    build(1,1,n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        int l,r,val;
        scanf("%d%d%d",&l,&r,&val);
        update(1,l,r,val);
        printf("%lld\n",query(1,l,r));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值