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. Docker数据卷容器

    用户需要在多个容器之间共享一些数据,就可以使用数据卷容器   从阿里云仓库下载镜像(也可以自己制作一个基础进项,比如只有几M的alpine) sudo docker pull registry.cn- ...

  2. charles抓包https/模拟弱网/设置断点重定向/压测

    charles几个常用功能   1,ios 抓包https网页:(如未配置,会显示unknown) 第一步是:给手机安装SSL证书 手机和电脑在同一wifi下,手机wifi配置http代理,ip是电脑 ...

  3. 利用for循环如何判定是水仙花数

    水仙花数业内的大家可能听说过,但是对于初学者来讲,对于水仙花数还是比较陌生的. 首先要知道的是水仙花数的计算公式:153=1**3+5**3+3**3: 如何去判定这个数是否为水仙花数,最好的办法就是 ...

  4. OpenGL(1)-环境搭建

    写在前面 工作几年,开始沉心做技术,对自己的知识进行梳理. OpenGL是由khronos组织制定并维护的规范,并不是API. OpenGL在3.2之前采用的是立即渲染模式(固定渲染管线),3.2之后 ...

  5. 曾经的华为C面试题,一点就通

     学习编程可以锻炼你的思维,帮助你更好地思考,创建一种我认为在各领域都非常有用的思维方式.   比尔盖茨      曾经的华为C面试题,一点就通 [问题区] 有两个变量x和y, x=10; y = 2 ...

  6. Django 前后端不分离 代码结构详解

    Demo:  hello_pycharm 根目录文件:hello_pycharm [__init__.py  __pycache__  settings.py  urls.py  wsgi.py] A ...

  7. Apache 性能配置优化

    前言 最近在进行apache性能优化设置.在修改apache配置)文件之前需要备份原有的配置文件夹conf,这是网站架设的好习惯.以下的apache配置调优均是在red had的环境下进行的. htt ...

  8. Navicat新建查询,系统找不到指定路径 独家解决办法

    Navicat新建查询系统找不到指定路径,很多人用了网上流行的那些解决办法,还是无法解决.比如: https://jingyan.baidu.com/article/86112f1387a713273 ...

  9. Tomcat之初识初体验

    1.what's this? Stable performance, free Java web application server! 相关: Java,Javac,JVM,JRE,JDK,Java ...

  10. lambda----jdk8重头戏

    简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能.本人建议不要乱用,因 ...