poj 3468 A Simple Problem with Integers(线段树成段更新,懒惰标记的使用)经典题目

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

这道题目就是错在懒惰标记没有处理好,而且没有注意long long

懒惰标记实际上就是让子节点暂时处于不更新状态,用到的时候再更新,如本题中的visit[]就是懒惰标记,例如总长度是1-10,我们现在要想更新1-6,(将1-6的值都加3)那么update()会先找1-10,发现不合适,再找他的左右孩子,发现1<5,说明1-6的区间在1-10的左孩子中,同时6>5,1-6也在1-10的右孩子中,这样依次去找1-6在的区间。但是找到1-5的时候,我们发现整个1-5都在1-6中间,也就是说这一段都要更新,那么我们将1-5的sum值更新了,同时用visit[rt]+=3记录下来1-5中的数字现在每个都 要加的数字,但是1-5下边还有1-3,4-5,3-3,4-4,5-5,这些我们就可以不用更新,因为这些我们暂时还用不到,假如现在又要将1-5区间的值都加5,那么visit[rt]+=5,此时就是8了,但是还是不用更新他的子节点,假如我们现在要用到1-3区间了,我们就可以一次性给1-3区间加上8,而不用先加3,再加5,这样懒惰标记就使得每次的递归都少了好多,

2、题目:

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 52972 Accepted: 15828
Case Time Limit: 2000MS

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.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1,A2, ... , AN. -1000000000 ≤Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa,Aa+1, ... ,Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa,Aa+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

Hint

The sums may exceed the range of 32-bit integers.

Source

 
3、AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
ll sum[N*4];
int visit[N*4];
void pushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    visit[rt]=0;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(rt);
}
void pushDown(int rt,int d)
{
    if(visit[rt]!=0)
    {
       visit[rt<<1]+=visit[rt];
       visit[rt<<1|1]+=visit[rt];
        sum[rt<<1|1]+=(ll)(d>>1)*visit[rt];//注意后边乘可能超整形,强制类型转换(ll),即AC
        sum[rt<<1]+=(ll)(d-(d>>1))*visit[rt];
        visit[rt]=0;
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l && R>=r)
    {
        visit[rt]+=c;
        sum[rt]+=(r-l+1)*c;
        return ;
    }
    pushDown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m)
        update(L,R,c,lson);
    if(R>m)
        update(L,R,c,rson);
    pushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l && R>=r)
    {
        return sum[rt];
    }
    pushDown(rt,r-l+1);
    int m=(l+r)>>1;
    ll ret=0;
    if(L<=m)
        ret+=query(L,R,lson);
    if(R>m)
        ret+=query(L,R,rson);
    return ret;
}
int main()
{
    int n,q,a,b,c;
    char s[3];
    scanf("%d%d",&n,&q);
    build(1,n,1);
    while(q--)
    {
        scanf("%s",s);
        if(s[0]=='C')
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,n,1);
        }
        else if(s[0]=='Q')
        {
            scanf("%d%d",&a,&b);
            printf("%lld\n",query(a,b,1,n,1));
        }
    }
    return 0;
}
/*
10 100
1 2 3 4 5 6 7 8 9 10
C 1 6 3
Q 1 1
Q 1 2
Q 4 4
Q 1 10
Q 1 5
C 3 6 3
Q 1 5
*/

4、wrong answer 代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
ll sum[N*4];
int visit[N*4];
void pushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    visit[rt]=0;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(rt);
}
void pushDown(int L,int R,int c,int l,int r,int rt)
{
    int m=r-l+1;
    if(visit[rt]!=0)
    {
        //printf("%d %d %d %d %d %d\n",l,r,sum[rt<<1|1],sum[rt<<1],m,c);
        sum[rt<<1|1]+=(m>>1)*c;
        sum[rt<<1]+=(m-(m>>1))*c;
       // printf("&&&&&&%d %d\n",sum[rt<<1|1],sum[rt<<1]);
        visit[rt]=0;
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    //printf("*%d %d %d \n",l,r,rt);
    if(L<=l && R>=r)
    {
        sum[rt]+=(r-l+1)*c;
        visit[rt]=1;
        pushDown(L,R,c,l,r,rt);
        return ;
    }
    pushDown(L,R,c,l,r,rt);
    int m=(l+r)>>1;
    if(L<=m)
    update(L,R,c,lson);
    if(R>m)
    update(L,R,c,rson);
    pushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l && R>=r)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;
    ll ret=0;
    if(L<=m)
    ret+=query(L,R,lson);
    if(R>m)
    ret+=query(L,R,rson);
    return ret;
}
int main()
{
    int n,q,a,b,c;
    char s[3];
    scanf("%d%d",&n,&q);
    build(1,n,1);
    while(q--)
    {
        scanf("%s",s);
        if(s[0]=='C')
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,n,1);
        }
        else if(s[0]=='Q')
        {
            scanf("%d%d",&a,&b);
            printf("%lld\n",query(a,b,1,n,1));
        }
    }
    return 0;
}
/*
10 100
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 1 5
C 3 6 3
Q 1 5
*/


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值