An easy problem

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

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

思路:

之前以为可以直接逆元,后面想起来逆元是要两个互质的数,后面想到了题目就是提示你用线段树,只要用建个线段树就好了。

假如第i次操作  ,x = 1;是第一种操作,那么只要根据修改第x个叶子结点将他修改为y。

x=2,只要将第y个叶子节点修改为1,

每一次操作都求出根节点的值就好了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e5+;
ll sum[M<<];
ll m;
void pushup(int rt){
sum[rt] = (sum[rt<<]*sum[rt<<|])%m;
} void update(int p,int c,int l,int r,int rt){
if(l == r){
sum[rt] = c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
if(p > m) update(p,c,rson);
pushup(rt);
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = ;
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} ll query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
ll ret = ;
if(L <= m) ret= (ret*query(L,R,lson))%m;
if(R > m) ret= (ret*query(L,R,rson))%m;
return ret;
}
int main()
{
ll t,n,x;
ll y;
ios::sync_with_stdio();
cin.tie();
cout.tie();
while(cin>>t){
int t1 = t;
while(t--){
ll ans = ;
cin>>n>>m;
build(,n,);
//cout<<2<<endl;
cout<<"Case #"<<t1-t<<":"<<endl;
for(int i = ;i <= n;i ++){
cin>>x>>y;
if(x==){
update(i,y,,n,);
cout<<query(,n,,n,)<<endl;
}
else{
update(y,,,n,);
cout<<query(,n,,n,)<<endl;
}
}
memset(sum,,sizeof(sum));
}
}
return ;
}

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

  1. hdu 5475 线段树

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. hdu 3397 线段树双标记

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  7. hdu 4533 线段树(问题转化+)

    威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  8. hdu 2871 线段树(各种操作)

    Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  10. hdu 1542 线段树扫描(面积)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. 大神教你零基础学PS,30堂课从入门到精通

    ps视频教程,ps自学视频教程.ps免费视频教程下载,大神教你零基础学PS教程视频内容较大,分为俩部分: 大神教你零基础学PS--30堂课从入门到精通第一部分:百度网盘,https://pan.bai ...

  2. VRRP+tunnel+IP SLA+Track实现冗余切换

    IP SLA(Internet Protocol Service-Level Agreement)互联网服务等级协议,本实验里通过发送测试报文,测试下一跳是否可达,结合Track实现冗余静态路由的切换 ...

  3. python游戏编程——乌龟和鱼类场景编程

    综合举例: 游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏. O    假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10 ·       游戏生成1只 ...

  4. Web APi 入门例子

    http://www.cnblogs.com/guyun/p/4589115.html#what

  5. 文字排版 - bootStrap4常用CSS笔记

    [文字常用标签]   <h1>.<h2>.<h3>.<h4>.<h5>.<h6> 标题类标签,h1字体最大以次类推 <sm ...

  6. CDN的基本原理和基础架构

    CDN基本原理 最简单的CDN网络由一个DNS服务器和几台缓存服务器组成: ①当用户点击网站页面上的内容URL,经过本地DNS系统解析,DNS系统会最终将域名的解析权交给CNAME指向的CDN专用DN ...

  7. ETSI公布的多接入移动边缘计算概念验证

    ETSI多接入移动边缘计算 公布的概念验证如下: 来源 MEC PoC Projects PoC#1: "Video User Experience Optimization via MEC ...

  8. texlive2018和texstudio的安装及汉化教程

    latex是编写论文的利器,尤其是公式的编辑是word等不可比的,且公式可以支持转换为Matgtype,十分方便且学习周期短. 下文是texlive2018和texstudio的安装教程: 本文转自: ...

  9. Tomcat ngxin 反向代理

    tomcat nginx 反向代理 安装nginx yum直接安装 yum install nginx –y 也可以编译安装 这是用编译安装,新手可以用yum安装 配置文件在 /etc/nginx/c ...

  10. 第33次Scrum会议(11/21)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/21 11:35~11:57,总计22min.地点:东北 ...