线段树 求区间连乘——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 ...
随机推荐
- yii2-widget-fileinput英文文档翻译
源地址:http://plugins.krajee.com/file-input 该插件是为bootstrap开发的增强版h5文件上传插件,具有多文件预览,多文件选择等功能.该插件提供了基于boots ...
- 64_p9
python2-termcolor-1.1.0-11.fc26.noarch.rpm 12-Feb-2017 14:05 13610 python2-terminado-0.6-2.fc26.noar ...
- Linux轻量级自动运维工具-Ansible浅析【转】
转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...
- 【bzoj5050】【bzoj九月月赛H】建造摩天楼
讲个笑话,这个题很休闲的. 大概是这样的,昨天看到这个题,第一眼星际把题目看反了然后感觉这是个傻逼题. 后来发现不对,这个修改一次的影响是很多的,可能导致一个数突然可以被改,也可能导致一个数不能被改. ...
- pip安装模块时:error: command 'gcc' failed with exit status 1
用安装python模块出现error: command 'gcc' failed with exit status 1 问题: gcc编译缺少模块 解决方法: yum install gcc libf ...
- AWS 使用总结
A.升配置的流程: 1.新开一台配置较高的机器; 2.将新机器和老机器的磁盘都取消关联,注意需要记录下老机器的磁盘分区设备名,如:/dev/sda1: 3.将老机器的磁盘挂载到新机器上,磁盘分区设备名 ...
- JavaWeb知识回顾-servlet生命周期。
Servlet生命周期 生命周期,很容易理解,拿人来说,就是你从出生到离开的这一过程.无论是什么技术,只要谈到生命周期都可以这样理解. Servlet的生命周期就是从它被创建到毁灭的过程,整个过程可以 ...
- 洛谷P2676 超级书架 题解
题目传送门 题目一看就是贪心.C++福利来了:sort. 基本思路就是:要使奶牛最少那么肯定高的奶牛先啦. 直接排序一遍(从高到矮)然后while,搞定! #include<bits/stdc+ ...
- 【Sql Server】Sql语句整理
use Person <--添加约束--> Alter table Student alter column Sno ) not null; Alter table Student Add ...
- OOD沉思录 --- 类和对象的关系 --- 使用关系
使用关系 对象A的方法MethodA使用了B的方法MethodB,则表示A对B存在使用关系 使用关系的最关键问题在于,A如何找到B,存在6种方案 方案一: A包含了B,B作为一个成员定义在A的类中,那 ...