A Simple Problem with Integers(区间更新)

A Simple Problem with Integers
Time Limit: 3000 MS Memory Limit: 32768 K
Total Submit: 178(48 users) Total Accepted: 51(36 users) Rating: Special Judge: No
Description
You have N integers, A1A2, ... , 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

Input contain multiple test cases,for each case:

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output
You have N integers, A1A2, ... , 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.
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
Source
POJ Monthly--2007.11.25, Yang Yi
Recommend
`Wind @Hrbust

线段树区间更新,如果真的全部更新每个节点,会很耗费时间,所以在可行的节点上标记一下,这样下次查询的时候如果查询到这个节点,就往下更新一次

#include<cstdio>
#include<cmath>
#include<queue>
#include<iostream>
using namespace std;
const int m=100000;
typedef long long LL;
struct node
{
    LL Inc;
    LL sum;
}a[m<<2];
LL b[m+1];
void build(int L,int R,int rt)
{
    a[rt].Inc=0;
    if(L==R)
    {
    //    scanf("%lld",&b[L]);
        a[rt].sum=b[L];
        return ;
    }
    int lr=rt<<1,rr=rt<<1|1;
    int mid=(L+R)>>1;
    build(L,mid,lr);
    build(mid+1,R,rr);
    a[rt].sum=a[lr].sum+a[rr].sum;
}
void update(int l,int r,int L,int R,int rt,LL x)
{
    if(L==l&&R==r)
    {
        a[rt].Inc+=x;
        return ;
    }
    a[rt].sum+=(r-l+1)*x;
    int m=(L+R)>>1;
    int lr=rt<<1,rr=rt<<1|1;
    if(r<=m)
    {
        update(l,r,L,m,lr,x);
    }
    else if(l>m)
    {
        update(l,r,m+1,R,rr,x);
    }
    else
    {
        update(l,m,L,m,lr,x);
        update(m+1,r,m+1,R,rr,x);
    }
}
LL  query(int l,int r,int L,int R,int rt)
{
    if(l<=L&&r>=R)
    {
        return a[rt].sum+a[rt].Inc*(r-l+1);
    }
    a[rt].sum+=(R-L+1)*a[rt].Inc;
    int m=(L+R)>>1;
    int lr=rt<<1,rr=rt<<1|1;
    if(a[rt].Inc)
    {
        //update(L,m,L,m,lr,a[rt].Inc);
    //    update(m+1,R,m+1,R,rr,a[rt].Inc);
        a[lr].Inc+=a[rt].Inc;
        a[rr].Inc+=a[rt].Inc;
    }
    a[rt].Inc=0;
    if(r<=m)
    {
        return query(l,r,L,m,lr);
    }
    else if(l>m)
    {
        return query(l,r,m+1,R,rr);
    }
    LL left=query(l,m,L,m,lr);
    LL right=query(m+1,r,m+1,R,rr);
    return left+right;
}

int main()
{
    int N,Q;
    while(~scanf("%d%d",&N,&Q))
    {
        int i;
        for(i=1;i<=N;i++)
        {
            scanf("%lld",&b[i]);
        }
        build(1,N,1);
        char c;
        int l,r;
        LL x;
        while(Q--)
        {
            cin>>c;
            if(c==‘Q‘)
            {
                scanf("%d%d",&l,&r);
                printf("%lld\n",query(l,r,1,N,1));
            }
            else if(c==‘C‘)
            {
            //    cout<<"dsfdsf"<<endl;
                scanf("%d%d%lld",&l,&r,&x);
                update(l,r,1,N,1,x);
            }
        }
    }
    return 0;
}
时间: 2024-08-10 23:30:30

A Simple Problem with Integers(区间更新)的相关文章

poj------(3468)A Simple Problem with Integers(区间更新)

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

POJ3468-A Simple Problem with Integers(区间更新 + SegmentTree || SplayTree || BinaryIndexTree)

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. In

poj3468A Simple Problem with Integers区间和线段树

同上... #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include

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

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 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(线段树区间更新)

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

HDU 4267 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5402    Accepted Submission(s): 1710 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

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

题意:典型的线段树C,Q问题,有n个数a[i] (1~n),C, a, b,c在[a,b]区间增加c Q a b 求[a,b]的和. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #i

Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers

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