An easy problem

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1553    Accepted Submission(s): 697

Problem Description
One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first, X = 1. This calculator only supports two types of operation.
1. multiply X with a number.
2. divide X with a number which was multiplied before.
After each operation, please output the number X modulo M.
 
Input
The first line is an integer T(1≤T≤10), indicating the number of test cases.
For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (1≤Q≤105,1≤M≤109)
The next Q lines, each line starts with an integer x indicating the type of operation.
if x is 1, an integer y is given, indicating the number to multiply. (0<y≤109)
if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)

It's guaranteed that in type 2 operation, there won't be two same n.

 
Output
For each test case, the first line, please output "Case #x:" and x is the id of the test cases starting from 1.
Then Q lines follow, each line please output an answer showed by the calculator.
 
Sample Input
1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7
 
Sample Output
Case #1:
2
1
2
20
10
1
6
42
504
84
 
思路:该题用long long 类型运算会溢出,结果WA。用高精度会TLE。线段树思路:第i个叶子结点维护的是第i个操作.若为乘运算则将结点的值修改为乘数,若为除运算则将结点的值修改为1.父结点维护左右子结点之积。每次输出结果为根节点的值。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=;
typedef long long ll;
struct Node{
ll val;
int l,r;
}a[MAXN*];
int n,mod;
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].val=;
if(l==r)
{
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
}
void update(int rt,int pos,int val)
{
if(a[rt].l==pos&&a[rt].r==pos)
{
a[rt].val=val%mod;
return ;
}
int mid=(a[rt].l+a[rt].r)>>;
if(pos<=mid)
{
update(rt<<,pos,val);
}
else
{
update((rt<<)|,pos,val);
}
a[rt].val=(a[rt<<].val*a[(rt<<)|].val)%mod;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d",&n,&mod);
build(,,n);
printf("Case #%d:\n",cas);
for(int i=;i<=n;i++)
{
int type,val;
scanf("%d%d",&type,&val);
if(type==)
{
update(,i,val);
}
else
{
update(,val,);
}
printf("%lld\n",a[].val);
}
}
return ;
}
 

HDU5475(线段树)的更多相关文章

  1. hdu5475(线段树单点修改,统计区间乘积)

    题目意思: 给定a*b*c*d*e*f*....,可以在某一步去掉前面的一个因子,每次回答乘积. #include <cstdio> #include <cstring> #i ...

  2. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  3. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  4. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  5. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  6. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  7. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  8. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  9. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

随机推荐

  1. linux下安装casperjs 开发运行环境

    casperjs是一个基于phantomjs的测试框架,使用python进行操作,所以一个完整的casperjs环境需要安装phantomjs和python. 1 phantomjs安装 到官网下载最 ...

  2. ANSI C和POSIX

    简单的说 ANSI C:标准C API(对应fopen) POSIX:方便在Linux下运行的C API(对应open)

  3. INSPIRED启示录 读书笔记 - 第12章 产品探索

    软件项目可以划分为两个阶段 探索产品阶段:弄清楚要开发什么产品(定义正确的产品) 在探索产品的阶段,产品经理负责分析各种创意,广泛收集用户需求,了解如何运用新技术,拿出产品原型并加以测试 从全局视角思 ...

  4. mac下安装py第三方库到python3下

    python3 -m pip install **** 中间可能碰到超时问题 python3 pip --default-timeout=100 install -U **** 设置默认超时时间即可 ...

  5. ggplot笔记001——ggplot2安装

         R3.2.2版安装ggplot2      今天安装ggplot2,开始用的是R3.2.1版本,但是一直报错.后面换了一个最新的R3.2.2,但安装时还是一样报错,原因是munsell这个包 ...

  6. 中文乱码之myEclipse项目导入时中文乱码(待)

    方法1:检查默认的编码是否设置成utf-8. 步骤如图: window——>preferences... 若Text file encoding 中的编码为 Other == UTF-8 ,则已 ...

  7. php环境之Wampserver端口修改

    WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐的配置环境过程,从而腾出更多精力去做开发.WampSer ...

  8. python之Django rest_framework总结

    一.rest api    a.api就是接口         如: - http://www.oldboyedu.com/get_user/                - http://www. ...

  9. phpPgAdmin (win)配置安装及远程访问

    phpPgAdmin (win)配置安装 [1]     通过PostgreSQL的Application Stack Builder配置安装phpPgAdmin 1.确保PostgreSQL安装并正 ...

  10. js抽象工厂模式

    抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 抽象工厂允许客户使用抽象的接口来创建一组相关产品,而不需要知道或关心实际生产出的具体产品是什么.这样客户就可以从具体 ...