poj3468A Simple Problem with Integers(线段树的区域更新)

http://poj.org/problem?id=3468

真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊。

线段树的区域更新。

线段树功能:update:成段增减 query:区间求和

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define N 100002
struct node
{
    int l,r;
    long long lz,w;
}q[4*N];
void pushup(int rt)
{
    q[rt].w=q[rt*2].w+q[rt*2+1].w;
}
void pushdown(int rt,int m)
{
    if(q[rt].lz)
    {
        q[rt*2].lz+=q[rt].lz;
        q[rt*2+1].lz+=q[rt].lz;
        q[rt*2].w+=(m-m/2)*q[rt].lz;
        q[rt*2+1].w+=(m/2)*q[rt].lz;
        q[rt].lz=0;
    }
}
void build(int l,int r,int rt)
{
    q[rt].l=l;
    q[rt].r=r;
    q[rt].lz=0;
    q[rt].w=0;
    if(l==r)
    {
        scanf("%lld",&q[rt].w);
        return ;
    }
    build(l,(r+l)/2,rt*2);
    build((r+l)/2+1,r,rt*2+1);
    pushup(rt);
}
void update(int key,int ll,int rr,int l,int r,int rt)
{
    if(ll<=l&&rr>=r)
    {
        q[rt].w+=(long long)key*(r-l+1);
        q[rt].lz+=key;
        return ;
    }
    pushdown(rt,r-l+1);
    int m=(r+l)/2;
    if(ll<=m)
    update(key,ll,rr,l,m,rt*2);
    if(rr>m)
    {
        update(key,ll,rr,m+1,r,rt*2+1);
    }
    pushup(rt);
}
long long query(int ll,int rr,int l,int r,int rt)
{
    if(ll<=l&&rr>=r)
    {
        return q[rt].w;
    }
    pushdown(rt,r-l+1);
    int m=(r+l)/2;
    long long L=0;
    if(ll<=m) L+=query(ll,rr,l,m,rt*2);
    if(rr>m) L+=query(ll,rr,m+1,r,rt*2+1);
    return L;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            char op[10];
            int a , b , c;
            scanf("%s",op);
            if (op[0] == ‘Q‘)
            {
                scanf("%d%d",&a,&b);
                long long tt=query(a,b,1,n,1);
                printf("%lld\n",tt);
            }
            else if(op[0]==‘C‘)
            {
                scanf("%d%d%d",&a,&b,&c);
                update(c,a,b,1,n,1);
            }
        }
    }
    return 0;

}

感觉这个博客不错http://www.douban.com/note/273509745/

poj3468A Simple Problem with Integers(线段树的区域更新),布布扣,bubuko.com

时间: 08-09

poj3468A Simple Problem with Integers(线段树的区域更新)的相关文章

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

poj3468A Simple Problem with Integers 线段树

//两个操作,第一个区间[a , b]内的所有数加c //第二个询问区间[a , b]的值 //很标准的线段数 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 1e5+10 ; #define left v<<1 #define right v<<1|1 typedef __int64 ll ; ll h[ma

A Simple Problem with Integers 线段树 成段更新

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3468 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

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

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. T

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 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 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

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

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 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 , 线段树+区间更新。

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

hdu4267---A Simple Problem with Integers(线段树)

Problem Description Let A1, A2, - , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. The other is to query the value of some element. Input There are