线段树 区间求和 poj 3468 A Simple Problem with Integers

题意:

给n个整数,求两种操作:1.给一个区间的数都加上一个数 2.查询一个区间的数的和  ,输出每次查询的结果

线段树区间求和,注意点:

1.使用lazy操作pushdown的时候,应该是子节点的lazy值加上父节点的lazy值,而不是直接赋值成父节点的lazy值,因为子节点可能之前也被操作过

2.节点的sum求和的时候应该加上区间的和(虽然直接加上修改值也能过样例TAT)

3.sum应该用long long

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

long long int sum[400000+10];
long long int lazy[400000+10];
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[(rt<<1)+1];
}
void pushdown(int rt,int length)
{if(lazy[rt]!=0)
{   if(length%2==0)
    {sum[rt<<1]+=lazy[rt]*(length/2);
    sum[(rt<<1)+1]+=lazy[rt]*(length/2);}
    else
    {
        sum[rt<<1]+=lazy[rt]*(length/2+1);
    sum[(rt<<1)+1]+=lazy[rt]*(length/2);
    }
    lazy[(rt<<1)+1]+=lazy[rt];
    lazy[rt<<1]+=lazy[rt];
    lazy[rt]=0;
}

}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%I64d",&sum[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,(rt<<1)+1);
    pushup(rt);
}
void update(int l,int r,int rt,int a,int b,int c)
{if(a<=l&&r<=b)
{
    sum[rt]+=(r-l+1)*c;
    lazy[rt]+=c;
    return;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(a<=m)
    update(l,m,rt<<1,a,b,c);
if(b>m)
    update(m+1,r,(rt<<1)+1,a,b,c);
pushup(rt);
}
long long int query(int l,int r,int rt,int a,int b)
{
    if(a<=l&&r<=b)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    long long int ans=0;
    int m=(l+r)>>1;
    if(a<=m)
    {
        ans+=query(l,m,rt<<1,a,b);
    }
    if(b>m)
    {
        ans+=query(m+1,r,(rt<<1)+1,a,b);
    }
    pushup(rt);
    return ans;

}

int main()
{
int n,q;
RII(n,q);
MS0(sum);
build(1,n,1);
MS0(lazy);
for(int i=0;i<q;i++)
{
    getchar();
    char o;
    scanf("%c",&o);
    if(o=='C')
    {int a,b,c;
    RIII(a,b,c);
    update(1,n,1,a,b,c);
    }
    else
        if(o=='Q')
    {
        int a,b;
        RII(a,b);
        printf("%I64d\n",query(1,n,1,a,b));
    }
}

        return 0;
}
时间: 2025-01-04 08:37:56

线段树 区间求和 poj 3468 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: 58269   Accepted: 17753 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 (线段树 成段更新 加值 求和)

题目链接 题意: 只有这两种操作 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. 分析:自己写的有点麻烦了,写的时候手残+脑残,改了好久. val+lazy*(r-l+1)表示和,如果lazy==0表示当前区间加的值不统一. 1 #include <iostream

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

poj 3468 A Simple Problem with Integers 【线段树-成段更新】

题目:poj 3468 A Simple Problem with Integers 题意:给出n个数,两种操作 1:l -- r 上的所有值加一个值val 2:求l---r 区间上的和 分析:线段树成段更新,成段求和 树中的每个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每个值要加的值 对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val .下面的区间标记num += val 对于求和操作,每次进行延迟更新,把num值

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

题目传送门 1 /* 2 线段树-成段更新:裸题,成段增减,区间求和 3 注意:开long long:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using namespace std; 11 12 #define lson l, mid, rt <<

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

POJ 3468 A Simple Problem with Integers(线段树)

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

POJ 3468 A Simple Problem with Integers 【树状数组】

题目链接:http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b,要求输出v[a]到v[b]之间的和,另一种给出三个数a,b,c,让v[a]到v[b]之间的数全都加上c. 完全是树状数组能够实现的功能,但是如果就这样单纯的套用模板,做第二种操作是更新每个值,这样的操作就有可能超时. 换一种思路,既然第二种操作是给某区间上的所有数加上相同的值,那么应该是能够简化的才对. 假设数组sum[i]为原数组从v[1]到v[i]的和,数