栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

A Simple Problem with Integers

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

A Simple Problem with Integers

A Simple Problem with Integers


题目翻译
你有 N 个整数,A1, A2, … , AN。 您需要处理两种操作。
一种类型的操作是将某个给定的数字添加到给定间隔中的每个数字。
另一种是要求给定区间内的数字总和。

题目分析
线段树模板题(同洛谷P3372)
https://www.luogu.com.cn/problem/P3372

代码

#include
using namespace std;
const int MAXN=100005;
long long a[MAXN];
long long lazy[MAXN<<2];
struct tree
{
	int l;
	int r;
	long long sum;
	int maxx;
	int mid()
	{
		return (l + r) >> 1;
	}
};
tree node[MAXN<<2];
void pushup(int i)
{
	node[i].sum = node[i << 1].sum + node[i << 1| 1].sum;
}
void build(int l, int r,int i)
{
	node[i].l = l;
	node[i].r = r;
	lazy[i] = 0;
	if (l == r)
	{
		node[i].sum = a[l];;
		return;
	}
	int m = node[i].mid();
	build(l, m,i << 1);
	build( m + 1, r, (i << 1 | 1));
	pushup(i);
}
void pushdown(int i, int m)
{
	if (lazy[i])
	{
		lazy[i << 1] += lazy[i];
		lazy[i << 1 | 1] += lazy[i];
		node[i << 1].sum += lazy[i] * (m - (m >> 1));
		node[i << 1 | 1].sum += lazy[i] * (m >> 1);
		lazy[i] = 0;
	}
}
void update(long long c,int l,int r,int i)
{
	if (node[i].l ==l&& node[i].r==r)
	{
		lazy[i] += c;
		node[i].sum += c * (r - l + 1);
		return;
	}
	if (node[i].l == node[i].r)return;
	int m = node[i].mid();
	pushdown(i, node[i].r - node[i].l + 1);
	if (r <= m)update(c, l, r, i << 1);
	else if (l > m)update(c, l, r, i << 1|1);
	else
	{
		update(c, l, m, i << 1);
		update(c, m+1, r, i << 1 | 1);
	}
	pushup(i);
}
long long query(int l, int r, int i)
{
	if (node[i].l == l && node[i].r == r)
	{
		return node[i].sum;
	}
	int m = node[i].mid();
	pushdown(i, node[i].r - node[i].l + 1);
	long long res = 0;
	if (r <= m)res+=query(l, r, i << 1);
	else if (l > m)res+=query(l, r, i << 1|1);
	else
	{
		res+=query(l, m, i << 1);
		res+=query(m+1, r, i << 1 | 1);
	}
	return res;
}
int main()
{
    int p,q,m,n;
	char t;
	long long c;
	cin >> p >> q;
	for (int k = 1; k <= p; k++)
		scanf("%lld", &a[k]);
	build(1, p, 1);
	while (q--)
	{
		cin >> t;
		if (t == 'Q')
		{
			scanf("%d %d", &m, &n);
			printf("%lldn",query(m, n, 1));
		}
		if (t == 'C')
		{
			scanf("%d %d %lld", &m, &n, &c);
			update(c, m, n, 1);
		}
	}
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/723277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号