Multiply game HDU - 3074 线段树

题解

给n个数字 两种操作一种将某个点修改为某个值 另一种询问一个区间的所有元素乘积

线段树模板提 单点修改区间查询

AC代码

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 5e4 + 10;
ll a[MAXN];

struct node 
{
	int l, r;
	ll v;
}tre[MAXN * 4];

inline void PushUp(int x)
{
	tre[x].v = tre[x << 1].v * tre[x << 1 | 1].v % MOD;
}
void Build(int x, int l, int r)
{
	tre[x].l = l, tre[x].r = r, tre[x].v = 0;
	if (l == r)
		tre[x].v = a[l];
	else
	{
		int m = l + r >> 1;
		Build(x << 1, l, m);
		Build(x << 1 | 1, m + 1, r);
		PushUp(x);
	}
}
void Update(int x, int p, ll v)
{
	int l = tre[x].l, r = tre[x].r;
	if (l == r)
		tre[x].v = v;
	else
	{
		int m = l + r >> 1;
		if (m >= p)
			Update(x << 1, p, v);
		else
			Update(x << 1 | 1, p, v);
		PushUp(x);
	}
}
ll Query(int x, int pl, int pr)
{
	int l = tre[x].l, r = tre[x].r;
	if (pl <= l && r <= pr)
		return tre[x].v;
	else
	{
		int m = l + r >> 1;
		ll v = 1;
		if (m >= pl)
			v = v * Query(x << 1, pl, pr) % MOD;
		if (m < pr)
			v = v * Query(x << 1 | 1, pl, pr) % MOD;
		return v;
	}
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	int T;
	cin >> T;
	while (T--)
	{
		int n, q;
		cin >> n;
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]);
		Build(1, 1, n);
		cin >> q;
		for (int i = 0; i < q; i++)
		{
			int op, x, y;
			scanf("%d%d%d", &op, &x, &y);
			if (op)
				Update(1, x, y);
			else
				printf("%lld\n", Query(1, x, y));
		}
	}

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值