POJ3468 A Simple Problem with Integers(线段树成段增减,区间求和)

题意:一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和。(这里区间指的是数列中连续的若干个数)对每次询问给出结果。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define M 100005
#define ls node<<1,l,m
#define rs node<<1|1,m+1,r
int n,m;
long long tree1[M<<2],tree2[M<<2];
void pushdown(int node,int m)
{
    if(tree1[node])
    {
        tree1[node<<1]+=tree1[node];
        tree1[node<<1|1]+=tree1[node];
        tree2[node<<1]+=(m-(m>>1))*tree1[node];
        tree2[node<<1|1]+=(m>>1)*tree1[node];
        tree1[node]=0;
    }
}
void buildtree(int node,int l,int r)
{
    tree1[node]=0;
    if(l==r)
    {
        scanf("%lld",&tree2[node]);
        return ;
    }
    int m=(l+r)>>1;
    buildtree(ls);
    buildtree(rs);
    tree2[node]=tree2[node<<1]+tree2[node<<1|1];
}
void update(int L,int R,int c,int node,int l,int r)
{
    if(L<=l&&r<=R)
    {
        tree1[node]+=c;
        tree2[node]+=c*(r-l+1);
        return ;
    }
    pushdown(node,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,ls);
    if(R>m) update(L,R,c,rs);
    tree2[node]=tree2[node<<1]+tree2[node<<1|1];
}
long long query(int node,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return tree2[node];
    }
    pushdown(node,r-l+1);
    int m=(l+r)>>1;
    long long ans=0;
    if(L<=m) ans+=query(ls,L,R);
    if(R>m) ans+=query(rs,L,R);
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        buildtree(1,1,n);
        char s[2];
        int a,b,c;
        while(m--)
        {
            scanf("%s",s);
            if(s[0]==‘Q‘)
            {
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(1,1,n,a,b));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,1,n);
            }
        }
    }
    return 0;
}
时间: 2024-10-13 15:27:59

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

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

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

poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 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 th

poj 3468 线段树 成段增减 区间求和

题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample Output 455915 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6

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

线段树功能 update:成段更新  query:区间求和 #include <iostream> #include <string> #include <cstdio> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; typedef long long ll; const int MAXN = 111111; ll sum[MAXN<&l

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(线段树)

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

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

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

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