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. hadoop程序在本地模式调试作业

    1.首先下载cygwin,例如安装在该目录下,D:\Program Files\cygwin\ 2.copy linux上的jar包到D:\Program Files\cygwin\home\lib ...

  2. 20165101刘天野 2017-2018-2 《Java程序设计》第5周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 第七章:内部类与异常类 内部类(nested classes),面向对象程序设计中, ...

  3. Java 类及类的构造方法

    类 类是一个模子,确定对象将会拥有的特性(属性)和行为(方法). 类的特点 类时对象的类型 具有相同属性和方法的一组对象的集合 构造方法 作用就是对类进行初始化. 如果你没有定议任何构造方法的形式,J ...

  4. DB2日期及时间的使用

    1.DB2中日期.时间寄存器的使用 --取日期 SELECT CURRENT DATE FROM sysibm.sysdummy1; --返回数据类型:DATE --结果集(YYYY-MM-DD): ...

  5. HIVE HSQL 基本操作命令

    创建表: hive>create table tablename(id int,name string,password string); 创建一个名字为tablename的表,表的属性有int ...

  6. Spring初学之FactoryBean配置Bean

    实体bean: Car.java: package spring.beans.factorybean; public class Car { private String name; private ...

  7. andriod sdk 模拟器的安装以及root

    安卓模拟器常用的一共2种java sdk 和 bluestacks 蓝手指.前者比较卡,后者貌似不能模拟2g/3g上网.   本文主要是针对java sdk的安装使用,以及root.关于root的意思 ...

  8. IIS7 配置PHP服务器

    安装PHP Manager: 1)访问 http://phpmanager.codeplex.com/releases/view/69115 下载PHP Manager.其中,x86 为32位 Win ...

  9. servlet cannot be resolved to a type解决办法

    工程里的路径权限高,eclipse并到classpath里寻找jar位置. 项目名-->右键 Property-->选择 Build Path-->选择 Configure Buil ...

  10. Python基本语法(一)

    注释及注意 #代表注释:冒号:结尾时,接下来的代码会自动缩进,一般为4个空格.Python程序是大小写敏感的. 数据类型和变量 在Python中能够直接处理的数据类型有以下几种: 整数 浮点数 字符串 ...