浙江财经大学
信息管理与工程学院

HDU 3074 Multiply game(线段树)

本文由 Ocrosoft 于 2016-11-13 19:21:05 发表

Multiply game

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
 

Solution

线段树单点更新求区间积(MOD)。不封装了,封个毛线…更麻烦了。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
#define  rep(a,v,b) for(int a=v;a<b;a++)
#define  repe(a,v,b) for(int a=v;a<=b;a++)
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 300 + 10;
const int MOD = 1000000009;
int gcd(int a, int b)
{
	if (!b)return a;
	return gcd(b, a%b);
}
/*(◕‿‿◕)(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
/*(◕‿‿◕) 签订契约,成为马猴烧酒吧 (◕‿‿◕)*/
/*(◕‿‿◕)(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
using namespace std;
int x[MAXN * 4], y[MAXN * 4];
LL mul[MAXN * 4];
int a[MAXN];
void build(int pos, int l, int r)
{
	x[pos] = l;
	y[pos] = r;
	if (x[pos] == y[pos])mul[pos] = a[l];
	else
	{
		int mid = l + r >> 1;
		build(pos * 2, l, mid);
		build(pos * 2 + 1, mid + 1, r);
		mul[pos] = (mul[pos * 2] * mul[pos * 2 + 1]) % MOD;
	}

}
void update(int pos, int p, int v)
{
	if (x[pos] == p&&y[pos] == p)mul[pos] = v;
	else
	{
		int mid = x[pos] + y[pos] >> 1;
		if (p <= mid)update(pos * 2, p, v);
		else update(pos * 2 + 1, p, v);
		mul[pos] = (mul[pos * 2] * mul[pos * 2 + 1]) % MOD;
	}
}
LL query(int pos, int l, int r)
{
	if (x[pos] == l&&y[pos] == r)return mul[pos];
	else
	{
		int mid = x[pos] + y[pos] >> 1;
		if (r <= mid)return query(pos * 2, l, r);
		else if (l > mid)return query(pos * 2 + 1, l, r);
		return (query(pos * 2, l, mid)*query(pos * 2 + 1, mid + 1, r)) % MOD;
	}
}
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		repe(i, 1, n)scanf("%d", &a[i]);
		build(1, 1, n);
		int m;
		cin >> m;
		while (m--)
		{
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			if (!x)cout << query(1, y, z) << endl;
			else update(1, y, z);
		}
	}
	return 0;
}

 

欢迎分享与转载,请保留链接与出处。Ocrosoft » HDU 3074 Multiply game(线段树)

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址