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 namespace std;
__int64 sum[1000000];
__int64 add[1000000];
void push(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
    //if(rt==5) printf("%I64d %d\n",add[rt],m);
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        sum[rt<<1]+=add[rt]*(m-(m>>1));
        sum[rt<<1|1]+=add[rt]*(m>>1);
        //if(rt<<1==10) printf("%I64d %d %d\n",sum[rt<<1],m,m-m/2);
        add[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%I64d",&sum[rt]);
        //if(rt==18) printf("%d...\n",sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(l  ,m,rt<<1);
    build(m+1,r,rt<<1|1);
    push(rt);
}
__int64 query(int L,int R,int l,int r,int rt) //query(l,r,1,n,1)
{
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    __int64 ret=0;
    if(L<=m) ret+= query(L,R,l,m,rt<<1);
    if(m<R)  ret+= query(L,R,m+1,r,rt<<1|1);
    return ret;
}
void update(int L,int R,int up,int l,int r,int rt)//update(l,r,up,1,n,1);
{
    if(L<=l&&r<=R)
    {
        add[rt]+=up;
        sum[rt]+=(__int64)up*(r-l+1);
        return ;
    }
    int m=(l+r)>>1;
    pushdown(rt,r-l+1);
    if(L<=m) update(L,R,up,l,m,rt<<1);
    if(m<R)  update(L,R,up,m+1,r,rt<<1|1);
    push(rt);
}
int main()
{
    int n,m;
    int i,j,k;
    int l,r,up;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        memset(add,0,sizeof(add));
        char que[10];
        while(m--)
        {
            scanf("%s",que);
            if(que[0]==‘Q‘)
            {
                scanf("%d%d",&l,&r);
                printf("%I64d\n",query(l,r,1,n,1));
            }
            else if(que[0]==‘C‘)
            {
                scanf("%d%d%d",&l,&r,&up);
                update(l,r,up,1,n,1);
            }
            else if(que[0]==‘a‘)
            {
                int temp;
                scanf("%d",&temp);
                //printf("%I64d I64d\n",sum[temp],add[temp]);
            }
        }
    }
    return 0;
}#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
__int64 sum[1000000];
__int64 add[1000000];
void push(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
    //if(rt==5) printf("%I64d %d\n",add[rt],m);
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        sum[rt<<1]+=add[rt]*(m-(m>>1));    //   m-m>>1 与 m-(m>>1) 的结果完全不同
        sum[rt<<1|1]+=add[rt]*(m>>1);
        //if(rt<<1==10) printf("%I64d %d %d\n",sum[rt<<1],m,m-m/2);
        add[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%I64d",&sum[rt]);
        //if(rt==18) printf("%d...\n",sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(l  ,m,rt<<1);
    build(m+1,r,rt<<1|1);
    push(rt);
}
__int64 query(int L,int R,int l,int r,int rt) //query(l,r,1,n,1)
{
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    __int64 ret=0;
    if(L<=m) ret+= query(L,R,l,m,rt<<1);
    if(m<R)  ret+= query(L,R,m+1,r,rt<<1|1);
    return ret;
}
void update(int L,int R,int up,int l,int r,int rt)//update(l,r,up,1,n,1);
{
    if(L<=l&&r<=R)
    {
        add[rt]+=up;
        sum[rt]+=(__int64)up*(r-l+1);
        return ;
    }
    int m=(l+r)>>1;
    pushdown(rt,r-l+1);
    if(L<=m) update(L,R,up,l,m,rt<<1);
    if(m<R)  update(L,R,up,m+1,r,rt<<1|1);
    push(rt);
}
int main()
{
    int n,m;
    int i,j,k;
    int l,r,up;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        memset(add,0,sizeof(add));
        char que[10];
        while(m--)
        {
            scanf("%s",que);
            if(que[0]==‘Q‘)
            {
                scanf("%d%d",&l,&r);
                printf("%I64d\n",query(l,r,1,n,1));
            }
            else if(que[0]==‘C‘)
            {
                scanf("%d%d%d",&l,&r,&up);
                update(l,r,up,1,n,1);
            }            /*
            else if(que[0]==‘a‘)
            {
                int temp;
                scanf("%d",&temp);
            }*/
        }
    }
    return 0;
}
时间: 2024-10-25 18:49:39

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 , 线段树+区间更新。

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 [线段树区间更新求和]

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 线段树 区间更新 区间查询

题目链接: 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 (线段树区域更新)

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 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: 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