G - A Simple Problem with Integers POJ - 3468

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 andQ. 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.
题意:给你n个整数,有两种操作,第一种,当字母为Q时,输出区间【a,b】的和,为C时,将区间【a,b】整体加c;

思路:线段树模板,注意数据范围诶,另外延迟标记用的可以^__^,哈哈哈;

下面附上代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
const int M=100005;
struct Tree
{
	LL sum;
	LL lazy;
	LL l,r,len; 
};
Tree tree[M<<2];
void pushup(LL o)
{
	tree[o].sum=tree[ll].sum+tree[rr].sum;
}
void pushdown(LL o)
{
	if(tree[o].lazy)
	{
		tree[ll].lazy+=tree[o].lazy;
		tree[rr].lazy+=tree[o].lazy;
		tree[ll].sum+=tree[o].lazy*tree[ll].len;
		tree[rr].sum+=tree[o].lazy*tree[rr].len;
		tree[o].lazy=0;
	}
}
void build(LL o, LL L,LL R)
{
	tree[o].l=L,tree[o].r=R;
	tree[o].len=R-L+1;
	tree[o].lazy=0;
	if(L==R)
	{
		LL p;
		scanf("%lld",&p);
		tree[o].sum=p;
		return ;
	}
	LL mid=(L+R)>>1;
	build(o<<1,L,mid);
	build(o<<1|1,mid+1,R);
	pushup(o);
}
void update(LL o,LL L,LL R,LL v)
{
	if(tree[o].l>=L&&tree[o].r<=R)
	{
		tree[o].lazy+=v;
		tree[o].sum+=v*tree[o].len;   
		return ;   
	}
	pushdown(o);
	LL mid=(tree[o].l+tree[o].r)>>1;
	if(R<=mid)
		update(ll,L,R,v);
	else if(L>mid)
		update(rr,L,R,v);
	else
	{
		update(ll,L,mid,v);
		update(rr,mid+1,R,v);
	}
	pushup(o);
}
LL Query(LL o,LL L,LL R)
{
	if(tree[o].l>=L&&tree[o].r<=R)
		return tree[o].sum;
	LL mid=(tree[o].l+tree[o].r)>>1;
	pushdown(o);
	if(R<=mid)
		return Query(ll,L,R);
	else if(L>mid)
		return Query(rr,L,R);
	else
		return 	Query(ll,L,mid)+Query(rr,mid+1,R);
}
int main()
{
	LL N,Q1;
	char s[3]={0};
	LL a,b,c;
	scanf("%lld %lld",&N,&Q1);
	build(1,1,N);
	while(Q1--)
	{
		scanf("%s",s);
		if(s[0]=='Q')
		{
			scanf("%lld %lld",&a,&b);
			LL k=Query(1,a,b);
			printf("%lld\n",k);
		}
		else
		{
			scanf("%lld %lld %lld",&a,&b,&c);
			update(1,a,b,c);
		}
	}
	return 0;
}






读取PNG图片作为无符号16位整数通常涉及图像处理库,因为PNG格式本身是一个复杂的压缩格式,包含多种数据类型和压缩算法。在编程语言中,如Python,你可以使用Pillow(PIL的后继者)这样的库来读取PNG图片并将其像素值转换为16位无符号整数。以下是一个基本的步骤和代码示例: 1. 安装Pillow库(如果尚未安装): ``` pip install Pillow ``` 2. 使用Pillow打开PNG图片文件。 3. 遍历图片的像素,并将每个像素的值转换为16位无符号整数。 示例代码(以Python为例): ```python from PIL import Image import numpy as np # 打开PNG文件 png_image = Image.open("image.png") # 将图片转换为16位无符号整数数组 # 注意:Pillow默认读取为8位无符号整数,对于16位数据需要指定模式 if png_image.mode == 'I': # 假设图片是16位无符号整数格式 png_array = np.array(png_image, dtype=np.uint16) else: # 转换模式为16位整数格式,注意这可能会改变原始数据的表示 # 对于16位深度的PNG,需要确保图像在保存时是以16位模式保存的 png_image = png_image.convert('I') png_array = np.array(png_image, dtype=np.uint16) # 现在png_array包含了图片数据,作为16位无符号整数 ``` 注意,上述代码假设PNG图片是以16位模式保存的。如果PNG图片不是以16位模式保存,上述代码将会将8位数据错误地当作16位来处理,这可能会导致数据不准确。确保图片的保存格式与你读取它的格式相匹配是很重要的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值