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>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define ll __int64
#define maxn 100005
#define mod  1000000009

struct node
{
	ll l,r,sum;
	ll lazy;
}tree[maxn*10];
ll a[maxn];
void Pushup(ll rt)
{
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void Pushdown(ll rt)
{
	if(tree[rt].lazy!=0)
	{
		tree[rt<<1].lazy+=tree[rt].lazy;//注意是+=,不是=;
		tree[rt<<1|1].lazy+=tree[rt].lazy;
		tree[rt<<1].sum+=(tree[rt<<1].r-tree[rt<<1].l+1)*tree[rt].lazy;
		tree[rt<<1|1].sum+=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*tree[rt].lazy;
		tree[rt].lazy=0;
	}
}
void build(ll l,ll r,ll rt)
{
	tree[rt].l=l;tree[rt].r=r;
	tree[rt].sum=0;tree[rt].lazy=0;
	if(l==r)
	{
		tree[rt].sum=a[l];
		return;
	}
	ll mid=(l+r)/2;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	Pushup(rt);
}

void update(ll rt,ll l,ll r,ll v)
{
	Pushdown(rt);
	if(tree[rt].l==l&&tree[rt].r==r)
	{
		tree[rt].lazy=v;
		tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*v;
		return ;
	}

	ll mid=(tree[rt].l+tree[rt].r)>>1;
	if(mid<l)
		update(rt<<1|1,l,r,v);
	else if(mid>=r)
		update(rt<<1,l,r,v);
	else
	{
		update(rt<<1,l,mid,v);
		update(rt<<1|1,mid+1,r,v);
	}
	Pushup(rt);
}
ll query(ll rt,ll l,ll r)
{
	if(tree[rt].l==l&&tree[rt].r==r)
		return tree[rt].sum;
	Pushdown(rt);
	ll mid=(tree[rt].l+tree[rt].r)>>1,ret=0;
	if(mid<l)
		ret+=query(rt<<1|1,l,r);
	else if(mid>=r)
		ret+=query(rt<<1,l,r);
	else
	{
		ret+=query(rt<<1,l,mid);
		ret+=query(rt<<1|1,mid+1,r);
	}
	Pushup(rt);
	return ret;
}
int main()
{
	ll n,m;
	while(~scanf("%I64d%I64d",&n,&m))
	{
		for(int i=1;i<=n;i++)
			scanf("%I64d",&a[i]);
		build(1,n,1);
		char s[2];
		ll u,v,c;
		while(m--)
		{
			scanf("%s",s);
			if(s[0]=='Q')
			{
				scanf("%I64d%I64d",&u,&v);
				printf("%I64d\n",query(1,u,v));
			}
			else
			{
				scanf("%I64d%I64d%I64d",&u,&v,&c);
				update(1,u,v,c);
			}
		}
	}
	return 0;
}
/*
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

10 22
1 2 3 4 5 6 7 8 9 10
Q 4 4
C 1 10 3
C 6 10 3
C 6 9 3
C 8 9 -100
C 7 9 3
C 7 10 3
C 1 10 3
Q 6 10
Q 6 9
Q 8 9
Q 7 9
Q 7 10
Q 1 10
Q 2 4
C 3 6 3
Q 9 9
Q 1 1
Q 5 5
Q 6 6
Q 7 7
Q 6 8
*/

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-08-26 09:29:25

PKU 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

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

【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

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

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. T

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]的和,数

POJ3468__A Simple Problem with Integers (线段树)

本文出自blog.csdn.net/svitter --我大C++的指针岂是尔等能够简单领悟! 题意 给N个节点,标号A1~An,然后有Q个操作,操作分为Q i j,查询i,j间的区间和.C i j k,i到j个数字,每个数字增加k,并且输出. 输入输出分析 给N,Q,然后跟操作.注意判断Q,C使用scanf("%s"). 测试数据: 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 Samp

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

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   Accepted: 17974 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 [线段树区间更新求和]

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