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 1698 線段樹 一段更新染色
- 線段樹 transformation——hdu 4578ORM
- HDU 1166 敵兵佈陣 (線段樹 插點問線)
- HDU 3333 Turing Tree(線段樹+離線操作)
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- HDU 1394 Minimum Inversion Number (暴力+線段樹)
- HDU 2795 Billboard(線段樹 區間最大)
- Transformation HDU - 4578線段樹綜合操作ORM
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- HDU 1542 Atlantis (線段樹+離散化+掃描線)
- HDU1754 I Hate It 【線段樹基礎:點修改+區間查詢】
- HDU 1754 I Hate It (線段樹 區間最值)
- 線段樹簡單思路
- 線段樹(1)建樹、單點修改、單點查詢、區間查詢和例題
- HDU1166 敵兵佈陣【線段樹基礎:點修改+區間查詢】
- 動態開點線段樹
- 線段樹學習筆記(更新中)筆記
- HDU 4027 Can you answer these queries? (線段樹 區間開方)
- 洛谷題單指南-線段樹-P3373 【模板】線段樹 2
- HDU 1255 覆蓋的面積(線段樹+掃描線+離散化)
- HDU 1729 Stone GameGAM
- Switch Game HDU - 2053GAM
- 線段樹
- 線~段~樹
- HDU 3397 Sequence operation(線段樹區間染色加區間合併)
- 動態開點線段樹說明
- 01 線段樹
- 線段樹--RMQMQ
- 【模版】線段樹
- 線段樹模板
- 線段樹 hate it
- HDU1698 Just a Hook【線段樹基礎:區間修改+區間查詢】Hook
- 【博弈論】HDU - 7216 Triangle GameGAM
- ut.cpp 最大線段並減線段交 [線段樹]
- 李超線段樹
- 線段樹進階
- 權值線段樹
- 線段樹筆記筆記