码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3074 Multiply game(线段树)

时间:2015-07-17 22:49:07      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

Multiply game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1820    Accepted Submission(s): 611


Problem Description
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
 

Source
2009 Multi-University Training Contest 17 - Host by NUDT
题目大意:给一些数,根据不同要求来进行点更新或者是区间计算。所以是简单的线段树模板题。
#include<stdio.h>
#include<string.h>
#define N 500005
#define mod 1000000007
struct Tree{
	int l,r;
	__int64 sum,add;
}tree[N<<2];
void pushup(int root){
	if(tree[root].l==tree[root].r)return ;
	tree[root].sum=tree[root<<1].sum*tree[root<<1|1].sum %mod;
	return ;
}
void pushdown(int root){
	if(tree[root].l==tree[root].r)return ;
	if(tree[root].add==-1)return ;
	tree[root<<1].add=tree[root<<1|1].add=tree[root].add;
	tree[root<<1].sum=(tree[root<<1].r-tree[root<<1].l+1)*tree[root].add;
	tree[root<<1|1].sum=(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].add;
	tree[root].add=-1;
	return;
}
void update(int l,int r,int z,int root){
	if(l==tree[root].l&&r==tree[root].r){
		tree[root].sum=(tree[root].r-tree[root].l+1)*z;
		tree[root].add=z;
		return;
	}
	pushdown(root);
	int mid=tree[root].l+tree[root].r>>1;
	if(r<=mid)update(l,r,z,root<<1);
	else if(l>mid)update(l,r,z,root<<1|1);
	else {
		update(l,mid,z,root<<1);
		update(mid+1,r,z,root<<1|1);
	}
	pushup(root);
	return;
}
void build(int l,int r,int root){
	tree[root].l=l;
	tree[root].r=r;
	tree[root].sum=0;
	tree[root].add=-1;
	if(l==r){
		tree[root].sum=1;
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,root<<1);
	build(mid+1,r,root<<1|1);
	pushup(root);
	return ;
}
__int64 query(int l,int r,int root){
	if(l==tree[root].l&&r==tree[root].r)
	return tree[root].sum;
	pushdown(root);
	int mid=tree[root].l+tree[root].r>>1;
	if(r<=mid)return query(l,r,root<<1);
	else if(l>mid)return query(l,r,root<<1|1);
	else return query(l,mid,root<<1)*query(mid+1,r,root<<1|1)%mod;
}
int main()
{
	int T,i,j,k,n,q,k1,k2,a[50005],caozuo;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		build(1,n,1);
		for(i=1;i<=n;i++)
		{scanf("%d",&a[i]);
		update(i,i,a[i],1);
		}
		scanf("%d",&q);
		while(q--)
		{
			scanf("%d%d%d",&caozuo,&k1,&k2);
			if(caozuo==0){
				printf("%I64d\n",query(k1,k2,1));
			}
			if(caozuo==1)update(k1,k1,k2,1);
		}
	}
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 3074 Multiply game(线段树)

标签:

原文地址:http://blog.csdn.net/aaaaacmer/article/details/46932389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!