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

题意:题意太裸了就不多说了,区间更新

直接上模板

链接:poj 3468

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <sstream>
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 100010;
const int inf = 0x3f3f3f3f;

struct node {
	int l, r;
	long long sum, add;
}tree[maxn << 2];

void pushup(int rt) {
	tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}

void pushdown(int rt, int len) {
	if(tree[rt].add) {
		tree[rt << 1].add += tree[rt].add;
		tree[rt << 1 | 1].add += tree[rt].add;
		tree[rt << 1].sum += tree[rt].add * (len - (len >> 1));
		tree[rt << 1 | 1].sum += tree[rt].add * (len >> 1);
		tree[rt].add = 0;
	}
}

void build(int rt, int l, int r) {
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].add = 0;
	if(l == r) {
		scanf("%lld", &tree[rt].sum);
		return;
	}
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
}

void update(int rt, int l, int r, int c)
{
	if(tree[rt].l == l && tree[rt].r == r) {
		tree[rt].add += c;
		tree[rt].sum += (long long)c * (r - l + 1);
		return;
	}
	if(tree[rt].l == tree[rt].r)
		return;
	pushdown(rt, tree[rt].r - tree[rt].l + 1);
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if(r <= mid)
		update(rt << 1, l, r, c);
	else if(l > mid)
		update(rt << 1 | 1, l, r, c);
	else {
		update(rt << 1, l, mid, c);
		update(rt << 1 | 1, mid + 1, r, c);
	}
	pushup(rt);
}

long long query(int rt, int l, int r) {
	if(tree[rt].l == l && tree[rt].r == r)
		return tree[rt].sum;
	pushdown(rt, tree[rt].r - tree[rt].l + 1);
	int mid = (tree[rt].r + tree[rt].l) >> 1;
	long long res = 0;
	if(r <= mid)
		res += query(rt << 1, l, r);
	else if(l > mid)
		res += query(rt << 1 | 1, l, r);
	else
		res += query(rt << 1, l, mid) + query(rt << 1 | 1, mid + 1, r);
	return res;

}

int main()
{
	int n, m;
	while(scanf("%d %d", &n, &m) != EOF) {
		build(1, 1, n);
		while(m--) {
			char ch[2];
			scanf("%s", ch);
			int a, b, c;
			if(ch[0] == 'Q') {
				scanf("%d %d", &a, &b);
				printf("%lld\n", query(1, a, b));
			}
			else {
				scanf("%d %d %d", &a, &b, &c);
				update(1, a, b, c);
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值