线段树最基础板子题
然后这道题5次MLE一次TLE让我知道了一点东西
先看题目和限制
B - Multiply game
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
题目出处:
HDU - 3074
就是一个线段树的裸题只是把加法换成了乘法,看起来就是这样哇塞好简单 ,其实不是,看下限制条件
Time l
imit1000 ms
Memory
limit32768 kB
OS
Windows
Source
2009 Multi-University Training Contest 17 - Host by NUDT
HintsHide
好了那么这个内存就是坑然而我不知道,然后在MLE五次之后我找到了题解,发现我就比题解多了给memset,所以说memset会吃内存,至于为什么,我也不知道,总结一下,MLE了以后要是实在优化不了,要是有memset先试试把memset删掉,然后我交完通过了以后想到可以先不建树,可以动态开点,但是我也懒得写了,那就这样吧。
标注做的少点,细节可以看另一个线段树模板题
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define top 1000000007
typedef long long ll;
ll a[500005];
struct node
{
int l,r;
ll milt;
}tree[500005*4];
int m,n;
void build(int k,int l,int r)//建树
{
tree[k].l=l;
tree[k].r=r;
int mid=(l+r)/2;
if(l==r)
{
tree[k].milt=a[l];
return;
}
build(k*2,l,mid);
build(k*2+1,mid+1,r);
tree[k].milt=((tree[2*k].milt)*(tree[2*k+1].milt)%top);
return;
}
void renew(int k,ll num,ll newn)//单点修改
{
if(tree[k].l==tree[k].r&&tree[k].l==num)
{
tree[k].milt=newn;
return;
}
ll mid=(tree[k].l+tree[k].r)/2;
if(num<=mid)
{
renew(2*k,num,newn);
}
else if(num>=mid+1)
{
renew(2*k+1,num,newn);
}
tree[k].milt=((tree[2*k].milt)*(tree[2*k+1].milt)%top);
return;
}
ll putout(int k,int l,int r)//查询
{
if(tree[k].l==l&&tree[k].r==r)
{
return tree[k].milt%top;
}
ll mid=(tree[k].l+tree[k].r)/2;
if(r<=mid)
{
return putout(2*k,l,r)%top;
}
else if(l>=mid+1)
{
return putout(2*k+1,l,r)%top;
}
else
{
return ((putout(2*k,l,mid))*(putout(2*k+1,mid+1,r))%top);
}
}
void shuchu()//查询函数的一个接口
{
int l,r;
scanf("%d %d",&l,&r);
printf("%lld\n",putout(1,l,r)%top);
}
void huanshu()//单点修改的接口
{
ll num;ll nu;
scanf("%lld %lld",&num,&nu);
renew(1,num,nu);
}
int main()
{
int q;
scanf("%d",&q);
while(q--)
{
scanf("%d",&n);
for(ll i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
scanf("%d",&m);
// memset(tree,0,sizeof(tree));//5次MLE的痛
build(1,1,n);
int p;
for(ll i=1;i<=m;i++)
{
scanf("%d",&p);
switch(p)
{
case 0 :shuchu(); break;
case 1 :huanshu(); break;
}
}
}
return 0;
}
2021.7.16