线段树 求区间连乘——hdu 3074 Multiply game
Multiply game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2189 Accepted Submission(s):
783
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…
(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.
answer of multiplication in each line, because the answer can be very large, so
can only output the answer after mod 1000000007.
3
#define mod 1000000007
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 50010
struct Tree{
long long sum;
}tree[N*];/*数组要开到四倍,因为会有许多空节点使用不到*/
int n,t,q,k1,k2,p,x;
int a[N];
void update(int k)
{
int lch=k<<,rch=(k<<)+;/*不知道为甚,宏定义会出错误*/
tree[k].sum=(tree[lch].sum*tree[rch].sum)%mod;
}
void build_tree(int k,int l,int r)
{
int lch=k<<,rch=(k<<)+,mid=(l+r)>>;
if(l==r)
{
tree[k].sum=a[l]%mod;
return ;
}
build_tree(lch,l,mid);
build_tree(rch,mid+,r);
update(k);
}
long long query(int k,int l,int r,int k1,int k2)
{
int lch=k<<,rch=(k<<)+,mid=(l+r)>>;
if(k1<=l&&r<=k2)
{
return tree[k].sum%mod;
}
long long ans=;
if(k1<=mid)
ans=(ans*query(lch,l,mid,k1,k2))%mod;
if(k2>mid)
ans=(ans*query(rch,mid+,r,k1,k2))%mod;
return ans;
}
void change(int k,int l,int r,int pos,int pl)
{
int lch=k<<,rch=(k<<)+,mid=(l+r)>>;
if(l==r)/*一开始加了懒惰标记,结果下传的时候处理的和区间下传一样了,结果错了,改为直接找到单点,再往回更新*/
{
tree[k].sum=pl;
return;
}
if(pos<=mid)
change(lch,l,mid,pos,pl);
else change(rch,mid+,r,pos,pl);
update(k);
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(tree,,sizeof(tree));
memset(a,,sizeof(a));
scanf("%d",&n);
for(int i=;i<=n;++i)
{
scanf("%d",&a[i]);
}
build_tree(,,n);
scanf("%d",&q);
for(int i=;i<=q;++i)
{
scanf("%d",&x);
if(x==)
{
scanf("%d%d",&k1,&k2);
cout<<query(,,n,k1,k2)<<endl;
}
else
{
scanf("%d%d",&k1,&p);
change(,,n,k1,p);
a[k1]=p;
}
} }
return ;
}
线段树 求区间连乘——hdu 3074 Multiply game的更多相关文章
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps
https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...
- poj 3264 线段树 求区间最大最小值
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- BZOJ 4127: Abs (树链剖分 线段树求区间绝对值之和 带区间加法)
题意 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d(d>=0) 2 u v 表示询问路径 (u,v) 上点权绝对值的和 分析 绝对值之和不好处理,那么我们开 ...
- HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 1e5个点,问 ...
- 线段树(区间合并)HDU - 1540
题意:输入n,m,给定n个相互连通的村庄,有m个操作,D x,表示破坏x村庄使其与相邻的两个村庄不相通,R 表示修复上一个被破坏的村庄,与相邻的两个村庄联通.Q x表示与x相连的村庄有多少个. 思路: ...
- hdu1166 敌兵布阵(线段树 求区间和 更新点)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
随机推荐
- escapeRegExp捕捉通配符的代码解析
费了好几个小时,把一小段正则代码搞通了,回顾并记下,加深记忆. 该段代码来自yii.js,它也是引自stackoverflow,地址是:http://stackoverflow.com/questio ...
- Hbuilder连接第3方模拟器(夜神)
http://www.bcty365.com/content-146-5148-1.html
- javascript经典小游戏代码集合
http://www.jb51.net/Special/349.htm
- pycharm显示行号
在PyCharm 里,显示行号有两种办法: 1,临时设置.右键单击行号处,选择 Show Line Numbers. 但是这种方法,只对一个文件有效,并且,重启PyCharm 后消失. 2,永久设置. ...
- 【Python学习】字符编码
先说两个基础知识. (1)计算机内部,数据是由0,1组成的: (2)计算机最小的数据单位,就是一个二进制单位即bit,接下来就是8个二进制单位表示一个字节(Byte). 1 ASCII码 ASCII码 ...
- 72.xilinx vivado zynq vdma仿真及应用详解(一)
很多人用zynq平台做视频图像开发,但是对vdma了解比较少,上手起来稍微有些困难,我针对这一现象,做了一个基于vivado和modelsim的仿真和应用测试工程,并写篇文章做些介绍,希望能对大家有帮 ...
- Windows上安装Jekyll
Jekyll是什么 jekyll是一个简单的免费的Blog生成工具,是一个静态站点生成器, 它会根据网页源码生成静态文件.它提供了模板.变量.插件等功能,所以实际上可以用来编写整个网站.也可使用基于j ...
- yum和head一起用,报错“由于管道被破坏而退出”
当要打印 [yum list ]时, 加上了管道符 以及 head 会出现报错 “由于管道被破坏而退出” 是因为 yum 与 head 连用 存在bug ,如果使用tail 则没有出现 具体什么bug ...
- ssh -o 常用选项
ssh -o ConnectTimeout=3 -o ConnectionAttempts=5 -o PasswordAuthentication=no -o StrictHostKeyCheckin ...
- java之正则表达式、日期操作
正则表达式和日期操作 正则表达式简介 正则表达式就是使用一系列预定义的特殊字符来描述一个字符串的格式规则,然后使用该格式规则匹配某个字符串是否符合格式要求. 作用:比如注册邮箱,邮箱有用户名和密码,一 ...