VmatrixOJ--[array] Star's Problem

Description

Star thinks Joe's problem "Sum" is too young too simple, so he added two operations.

Give a sequence of N numbers and M operations, there are three types of operations:
Operation 1: Change all the number to its opposite number;
Operation 2: Add an integer X to all the numbers;
Operation 3: Print the sum of the subsequence in [L, R].

Now it's your job, good luck.

Input

The first line is two integers N and M (0 < N < 20000, 0 < M < 200000), means the length of sequence and the number of operations.
The second line has N integers, indicate the sequence.
The follow M lines shows the operations:
"1"         Operation 1
"2 X"       Operation 2, X is an integer
"3 L R"     Operation 3, L and R is integer
Numbers in sequence and X in Operation 2 has a absolute value less or equal to 100.

Output

The result of Operation 3, one per line.

Sample Input

5 5
1 2 3 4 5
3 1 1
1
3 1 3
2 5
3 4 5

Sample Output

1
-6
1

Hint

You should use scanfprintf, and long long. opposite number: 相反数

Code: 

#include <stdio.h>
#define forto(i,a,b) for(i=(a);i<=(b);i++)
typedef long long LL;
LL arr[100100];
int main()
    {
        int n,m,i;
        scanf("%d%d",&n,&m);
        forto(i,1,n) scanf("%lld",&arr[i]);
        forto(i,2,n) arr[i]+=arr[i-1];
        LL mu=1,sum=0;
        while(m--){
        	int t;scanf("%d",&t);
        	if(t==1){mu=-mu;sum=-sum;
			}
			if(t==2){
				LL x; scanf("%lld",&x); sum+=x;}
			if(t==3){
				int l,r; scanf("%d%d",&l,&r);
				printf("%lld\n",(arr[r]-arr[l-1])*mu+sum*(r-l+1));
			}
		}
		return 0;
        
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值