HDU 3074 Multiply game 线段树区间乘法

http://acm.hdu.edu.cn/showproblem.php?pid=3074

Tired of playing computer games, alpc23 is planning to play a game on numbers. Because plus and subtraction is too easy for this gay, he wants to do some multiplication in a number sequence. After playing it a few times, he has found it is also too boring. So he plan to do a more challenge job: he wants to change several numbers in this sequence and also work out the multiplication of all the number in a subsequence of the whole sequence.
  To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…
 

Input

The first line is the number of case T (T<=10).
  For each test case, the first line is the length of sequence n (n<=50000), the second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an,
Then the third line is the number of operation q (q<=50000), from the fourth line to the q+3 line are the description of the q operations. They are the one of the two forms:
0 k1 k2; you need to work out the multiplication of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
1 k p; the kth number of the sequence has been change to p. (1<=k<=n)
You can assume that all the numbers before and after the replacement are no larger than 1 million.

Output

For each of the first operation, you need to output the answer of multiplication in each line, because the answer can be very large, so can only output the answer after mod 1000000007.

Sample Input

1
6
1 2 4 5 6 3
3
0 2 5
1 3 7
0 2 5

Sample Output

240
420

题目大意:给n个数,两种操作:求区间乘积,单点修改。

思路:RMQ模板改一下就好了。单点修改也不需要标记。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const ll MOD=1e9+7;
const int maxl=5e4;

struct node
{
	int l,r;
	ll mul;
};

node tree[maxl*4+5];
int a[maxl];
int n,m;

void build(int i,int l,int r)//i为当前节点编号 区间为[l,r]
{
	tree[i].l=l,tree[i].r=r;
	if(l==r)//叶子节点
	{
		tree[i].mul=a[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(i<<1,l,mid);//左子树
	build(i<<1|1,mid+1,r);//右子树
	tree[i].mul=tree[i<<1].mul*tree[i<<1|1].mul;
	if(tree[i].mul>=MOD)
        tree[i].mul%=MOD;
}

void update(int i,int p,int c)//i为当前节点编号 把[p,p]节点的值改为c
{
	if(tree[i].l==tree[i].r&&tree[i].r==p)//叶子节点
	{
		tree[i].mul=c;
		return ;
	}
	int mid=(tree[i].l+tree[i].r)>>1;
	if(p<=mid)//左半区间
		update(i<<1,p,c);
	else	//右半区间
		update(i<<1|1,p,c);
	tree[i].mul=tree[i<<1].mul*tree[i<<1|1].mul;
	if(tree[i].mul>=MOD)
        tree[i].mul%=MOD;
}

ll query(int i,int l,int r)
{
	if(tree[i].l==l&&tree[i].r==r)//当前区间刚好是要找的区间
		return tree[i].mul;
	int mid=(tree[i].l+tree[i].r)>>1;
	ll ans;
	if(r<=mid)//要查询的区间在左半部分
		ans=query(i<<1,l,r);
	else if(l>=mid+1)//要查询的区间在右半部分
		ans=query(i<<1|1,l,r);
	else //左右两边均有
		ans=query(i<<1,l,mid)*query(i<<1|1,mid+1,r);
    if(ans>=MOD)
        ans%=MOD;
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        scanf("%d",&m);
        int op,l,r;
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d",&op,&l,&r);
            if(!op)
                printf("%lld\n",query(1,l,r));
            else
                update(1,l,r);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值