POJ 题目3468 A Simple Problem with Integers(线段树成段更新,区间求和)

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 72251   Accepted: 22295
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 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 A1,
A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

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

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

ac代码

#include<stdio.h>
#include<string.h>
struct s
{
	__int64 sum,val;
}node[410000];
void pushup(int tr)
{
	node[tr].sum=node[tr<<1].sum+node[tr<<1|1].sum;
}
void pushdown(int tr,int m)
{
	if(node[tr].val)
	{
		node[tr<<1].val+=node[tr].val;
		node[tr<<1|1].val+=node[tr].val;
		node[tr<<1].sum+=node[tr].val*(m-(m>>1));
		node[tr<<1|1].sum+=node[tr].val*(m>>1);
		node[tr].val=0;
	}
}
void build(int l,int r,int tr)
{
	node[tr].val=0;
	if(l==r)
	{
		scanf("%I64d",&node[tr].sum);
		return;
	}
	int m=(l+r)>>1;
	build(l,m,tr<<1);
	build(m+1,r,tr<<1|1);
	pushup(tr);
}
__int64 query(int L,int R,int l,int r,int tr)
{
	if(L<=l&&r<=R)
	{
		return node[tr].sum;
	}
	int m=(l+r)>>1;
	pushdown(tr,r-l+1);
	__int64 ans=0;
	if(L<=m)
		ans+=query(L,R,l,m,tr<<1);
	if(m<R)
		ans+=query(L,R,m+1,r,tr<<1|1);
	pushup(tr);
	return ans;
}
void update(int L,int R,__int64 add,int l,int r,int tr)
{
	if(L<=l&&r<=R)
	{
		node[tr].sum+=add*(r-l+1);
		node[tr].val+=add;
		return;
	}
	pushdown(tr,r-l+1);
	int m=(l+r)>>1;
	if(L<=m)
		update(L,R,add,l,m,tr<<1);
	if(m<R)
		update(L,R,add,m+1,r,tr<<1|1);
	pushup(tr);
}
int main()
{
	int n,q;
	while(scanf("%d%d",&n,&q)!=EOF)
	{
		build(1,n,1);
		char s[3];
		while(q--)
		{
			scanf("%s",s);
			if(s[0]=='Q')
			{
				int a,b;
				scanf("%d%d",&a,&b);
				printf("%I64d\n",query(a,b,1,n,1));
			}
			else
				if(s[0]=='C')
				{
					int a,b;
					__int64 c;
					scanf("%d%d%I64d",&a,&b,&c);
					update(a,b,c,1,n,1);
				}
		}
	}
}
时间: 2024-10-18 18:53:21

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 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 线段树成段更新

线段树功能 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

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

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

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

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训练计划2777_Count Color(线段树/成段更新/区间染色)

解题报告 题意: 对线段染色,询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话访问线段树,求出颜色种数.结果超时了,最坏的情况下,染色可以染到叶子节点. 换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了,用按位或来处理颜色种数. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为d[u]+d[v]-2*d[LCA(u,v)] 现在d数组是变化的 对应每一条边的变化 他修改的是一个区间 用时间戳处理每个点管辖的区域 然后用线段树修改 线段树的叶子节点村的是根到每一个点的距离 求最近公共祖先没差别 只是堕落用线段树维护d数组 各种错误 4个小时 伤不起 #include <cs

poj 3468 A Simple Problem with Integers 线段树区间更新

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