poj 3468 : A Simple Problem with Integers 【线段树 区间修改】

题目链接

  题目是对一个数组,支持两种操作

    操作C:对下标从a到b的每个元素,值增加c;

    操作Q:对求下标从a到b的元素值之和。

具体题解参见 http://blog.csdn.net/acceptedxukai/article/details/6933446

代码来自 http://blog.csdn.net/jinglinxiao/article/details/54561770

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
typedef long long LL;

const int maxn=1e5+7;
LL lazy[maxn<<2],sum[maxn<<2];

void push_up(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void push_down(int rt,int len)
{
    if(lazy[rt]==0) return ;
    lazy[rt<<1]+=lazy[rt];
    lazy[rt<<1|1]+=lazy[rt];
    sum[rt<<1]+=lazy[rt]*(len-(len>>1));
    sum[rt<<1|1]+=lazy[rt]*(len>>1);
    lazy[rt]=0;
}

void build(int rt,int l,int r)
{
    if(l==r)
    {
        lazy[rt]=0;
        scanf("%lld",&sum[rt]);
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    push_up(rt);
}

void update(int rt,int l,int r,int ul,int ur,int v)
{
    if(ul<=l&&r<=ur)
    {
        lazy[rt]+=v;
        sum[rt]+=(r-l+1)*v;
        return ;
    }
    if(l==r) return ;
    push_down(rt,r-l+1);
    int mid=(l+r)>>1;
    if(ul<=mid) update(rt<<1,l,mid,ul,ur,v);
    if(ur>mid) update(rt<<1|1,mid+1,r,ul,ur,v);
    push_up(rt);
}

LL query(int rt,int l,int r,int ql,int qr)
{
    if(ql<=l&&r<=qr) return sum[rt];
    int mid=(l+r)>>1;
    push_down(rt,r-l+1);
    LL ret=0;
    if(ql<=mid) ret+=query(rt<<1,l,mid,ql,qr);
    if(qr>mid) ret+=query(rt<<1|1,mid+1,r,ql,qr);
    return ret;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        while(m--)
        {
            char op[3];
            scanf("%s",op);
            if(op[0]==‘Q‘)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%lld\n",query(1,1,n,l,r));
            }
            else
            {
                int l,r,c;
                scanf("%d%d%d",&l,&r,&c);
                update(1,1,n,l,r,c);
            }
        }
    }
}
时间: 2024-08-07 17:01:20

poj 3468 : A Simple Problem with Integers 【线段树 区间修改】的相关文章

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

A Simple Problem with Integers 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

POJ 3468 A Simple Problem with Integers(线段树区间更新区间求和)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   Accepted: 20818 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 3468 A Simple Problem with Integers 线段树区间更新

点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 63565   Accepted: 19546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations.

(简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

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. 题意

POJ 3468 A Simple Problem with Integers 线段树 区间更新 区间查询

题目链接: http://poj.org/problem?id=3468 题目描述: 一组数列, 可进行一段区间加上某一个数, 和区间查询 解题思路: 线段树, 之前的那道题是求总区间直接输出sum[1] 就可以了, 这次有了区间查询, 同理, 查询的时候Pushdown 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <map

POJ 3468 A Simple Problem with Integers(线段树 区间更新)

http://poj.org/problem?id=3468 题意 :对于一个序列有两种操作 1 查询 l到r 的和 2 对于l 到r上的每个数 加上 up 思路: 用单点更新必然超时 所以需要区间更新 (位运算时 注意 m-m>>1 与 m-(m>>1) 的区别) #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using names

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

A Simple Problem with Integers 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

POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 62431   Accepted: 19141 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers //线段树的成段更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   Accepted: 17974 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of