poj 3468 线段树

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

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

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

POJ Monthly--2007.11.25, Yang Yi

题目很好理解,就是跟新区间求和。初学线段树,一开始不会懒操作,结果每次求和都搜到树的最底层,TLE了几发。后来经KDQ大神指点,终于在知道懒操作,然后自己改改终于过了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct node
{
    int left,right;
    long long int value;
    long long int add ;
}tree[400005];
int m,n;
char ch[10];
int s,e;
long long int a,b[100005];
void push_down(int u)
{
    if(tree[u].add)
    {
        tree[u << 1].value += (tree[u << 1].right-tree[u<<1].left+1)*tree[u].add;
        tree[u<<1|1].value+=(tree[u<<1|1].right-tree[u<<1|1].left+1)*tree[u].add;
        tree[u<<1].add+=tree[u].add;
        tree[u<<1|1].add+=tree[u].add;
        tree[u].add=0;
    }
}
void create(int l,int r,int root)
{
    tree[root].left=l;
    tree[root].right=r;
    tree[root].value=0;
    tree[root].add=0;
    if(l==r)
    {
        tree[root].value=b[l];
        return;
    }
    int m=(tree[root].left+tree[root].right)/2;
    create(l,m,root<<1);
    create(m+1,r,root<<1|1);
    tree[root].value=tree[root<<1].value+tree[root<<1|1].value;
}
long long int query(int l,int r,int root)
{
    if(l > tree[root].right || r < tree[root].left)return 0 ;
    if(tree[root].left==l&&tree[root].right==r)return tree[root].value;
    else
    {
        push_down(root);
        int m=(tree[root].left+tree[root].right)/2;
        if(r<=m)return query(l,r,root<<1);
        else if(l>m)return query(l,r,root<<1|1);
        else return query(l,m,root<<1)+query(m+1,r,root<<1|1);
    }
}
void update(int l,int r,int root,long long int v)
{
    if(l>tree[root].right||r<tree[root].left)return ;
    if(tree[root].left==l&&tree[root].right==r)
    {
        tree[root].value+=v*(tree[root].right-tree[root].left+1);
        tree[root].add+=v;
        return;
    }
    push_down(root);
    int m=(tree[root].left+tree[root].right)/2;
    if(r<=m)update(l,r,root<<1,v);
    else if(l>m)update(l,r,root<<1|1,v);
    else
    {
        update(l,m,root<<1,v);
        update(m+1,r,root<<1|1,v);
    }
    tree[root].value=tree[root<<1].value+tree[root<<1|1].value;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&b[i]);
    }
    create(1,n,1);
    while(m--)
    {
        scanf("%s",&ch);
        if(ch[0]=='Q')
        {
            scanf("%d%d",&s,&e);
            cout<<query(s,e,1)<<endl;
        }
        else
        {
            scanf("%d%d%lld",&s,&e,&a);
            update(s,e,1,a);
        }
    }
    return 0;
}


你可能感兴趣的:(poj,Baoge)