HDU 3074 Multiply game(線段樹 單點更新)
Multiply game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2687 Accepted Submission(s): 949
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…
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.
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
題意:
求線段樹區間乘積,有更改操作。
POINT:
LL,別的其實和求和一樣。本以為要用什麼費馬小定理啥的,寫著寫著發現線段樹一次一次更新就行了,區間除以的時候可能適用。水題。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define rt x<<1|1
#define lt x<<1
const int N = 50000*8;
#define LL long long
const LL p = 1000000007;
LL sum[N];
LL a[N/8];
void init()
{
}
void build(int x,int l,int r)
{
if(l==r) (sum[x]=a[l])%=p;
else
{
int mid=(l+r)>>1;
build(lt,l,mid);
build(rt,mid+1,r);
(sum[x]=sum[lt]*sum[rt])%=p;
}
}
void change(int x,int l,int r,int i,LL k)
{
if(l==r&&r==i) (sum[x]=k)%=p;
else
{
int mid=(l+r)>>1;
if(i<=mid) change(lt,l,mid,i,k);
if(mid<i) change(rt,mid+1,r,i,k);
(sum[x]=sum[lt]*sum[rt])%=p;
}
}
LL query(int x,int l,int r,int ll,int rr)
{
LL ans=1;
if(ll<=l&&rr>=r) (ans*=sum[x])%=p;
else
{
int mid=(l+r)>>1;
if(ll<=mid) (ans*=query(lt,l,mid,ll,rr))%=p;
if(mid<rr) (ans*=query(rt,mid+1,r,ll,rr))%=p;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
init();
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
build(1,1,n);
int q;
scanf("%d",&q);
while(q--)
{
int k,k1,k2;
scanf("%d %d %d",&k,&k1,&k2);
if(k)
{
change(1,1,n,k1,(LL)k2);
}
else
{
LL ans=0;
ans=query(1,1,n,k1,k2);
printf("%lld\n",ans);
}
}
}
return 0;
}
相關文章
- (hdu 1754) I Hate It(線段樹基礎,單點更新)
- (hdu 1166)敵兵佈陣(線段樹入門,單點更新)
- HDU 1698 Just a Hook (線段樹區間更新)Hook
- hdu 1754 I Hate It (線段樹)
- Transformation HDU - 4578線段樹綜合操作ORM
- hdu 1754 【線段樹/RMQ】I Hate ItMQ
- hdu 3973 字串hash+線段樹字串
- hdu4288 離線處理線段樹
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- HDU 1754 I Hate It 線段樹入門
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- HDU1754 I Hate It 【線段樹基礎:點修改+區間查詢】
- HDU 1754 I Hate It (線段樹 區間最值)
- hdu 1394 Minimum Inversion Number 【線段樹查詢】
- HDU 1556 Color the ball 線段樹入門題
- hdu 2665 可持久化線段樹求區間第K大值(函式式線段樹||主席樹)持久化函式
- 動態開點線段樹
- HDU 3333 Turing Tree(線段樹+離線操作)
- 線段樹學習筆記(更新中)筆記
- hihocoder 1078 線段樹的區間修改 (線段樹 區間更新 模板)
- 線段樹知識點總結
- 線段樹的一點總結
- 簡單的線段樹應用
- 線~段~樹
- HDU 5299 Circles Game(樹的刪邊遊戲)GAM遊戲
- 動態開點線段樹說明
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- Codeforces 52C (線段樹區間更新)
- HDU1166 敵兵佈陣【線段樹基礎:點修改+區間查詢】
- 線段樹 hate it
- 線段樹模板
- 01 線段樹
- 線段樹--RMQMQ
- HDU 1166 敵兵佈陣 線段樹入門題目
- [乾貨] 線段樹知識點總結
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- 1080 線段樹練習 單點修改及區間查詢