#POJ 3468 A Simple Problem with Integers 【区间加减法】【水】

题目:

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 88582 Accepted: 27535
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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



思路:线段树板题
两个操作,Q  求区间和
    C  改变区间值
注意lazy数组和pushdown操作在区间赋值和区间加减法操作时的区别。


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define MAXN 200000
long long lazy[2 * MAXN];
long long t[2 * MAXN], a[2 * MAXN];

//int be[MAXN], ed[MAXN];
//int hips[2 * MAXN];
//double endhips[4 * MAXN];
//int maxn;

using namespace std;

void BuildTree(int l, int r, int x){
	if (l == r)
	{
		t[x] = a[l];
		return;
	}
	int m = (l + r) >> 1;
	BuildTree(l, m, x << 1);
	BuildTree(m + 1, r, x << 1 | 1);
	t[x] = t[x << 1] + t[x << 1 | 1];
	return;
}

void PushDown(int l, int r, int x){
	int m = (l + r) >> 1;
	if (lazy[x]){
		t[x << 1] += lazy[x] * (m - l + 1);
		t[x << 1 | 1] += lazy[x] * (r - m);
		lazy[x << 1 | 1] += lazy[x];
		lazy[x << 1] += lazy[x];
		lazy[x] = 0;
	}
	return;
}

void Modify(int pos, int val, int l, int r, int x){
	if (pos == l && r == l)
	{
		t[x] = val;
		return;
	}
	int m = (l + r) >> 1;
	if (pos <= m)
	{
		Modify(pos, val, l, m, x << 1);
	}
	else Modify(pos, val, m + 1, r, x << 1 | 1);
	t[x] = t[x << 1] + t[x << 1 | 1];
	return;
}

void SegModify(int L, int R, int val, int l, int r, int x){
	if (l == L&&r == R){
		t[x] += (R - L + 1)*val;
		lazy[x] += val;
		return;
	}
	PushDown(l, r, x);
	int m = (l + r) >> 1;
	if (R <= m)SegModify(L, R, val, l, m, x << 1);
	else if (L>m)SegModify(L, R, val, m + 1, r, x << 1 | 1);
	else {
		SegModify(L, m, val, l, m, x << 1);
		SegModify(m + 1, R, val, m + 1, r, x << 1 | 1);
	}
	t[x] = t[x << 1] + t[x << 1 | 1];
	return;
}

long long Query(int L, int R, int l, int r, int x){
	if (L == l && R == r)
		return t[x];
	PushDown(l, r, x);
	int m = (l + r) >> 1;
	if (R <= m)return Query(L, R, l, m, x << 1);
	else if (L>m)return Query(L, R, m + 1, r, x << 1 | 1);
	else return Query(L, m, l, m, x << 1) + Query(m + 1, R, m + 1, r, x << 1 | 1);
}

int main()
{


	int nn, mm;
	while (cin >> nn >> mm)
	{
		memset(t, 0, sizeof(t));
		//memset(hips, 0, sizeof(hips));
		//memset(endhips, 0, sizeof(endhips));
		memset(lazy, 0, sizeof(lazy));
		//maxn = 0;

		int n = 1;
		while (n < MAXN / 2)
		{
			n *= 2;
		}
		for (size_t i = 0; i < nn; i++)
		{
			scanf("%I64d", &a[i + 1]);
		}
		BuildTree(1, n, 1);


		long long be, ed, tran;
		char op;
		while (mm--)
		{
			cin >> op;
			if (op == 'C')
			{
				scanf("%I64d%I64d%I64d", &be, &ed, &tran);
				SegModify(be, ed, tran, 1, n, 1);
			}
			else
			{
				scanf("%I64d%I64d", &be, &ed);
				cout << Query(be, ed, 1, n, 1) << "\n";
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值