hdu 5475 (线段树)
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
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.
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.
Then Q lines follow, each line please output an answer showed by the calculator.
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7
思路:
之前以为可以直接逆元,后面想起来逆元是要两个互质的数,后面想到了题目就是提示你用线段树,只要用建个线段树就好了。
假如第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 (线段树)的更多相关文章
- hdu 5475 线段树
An easy problem Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 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 ...
- hdu 3974 线段树 将树弄到区间上
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 3397 线段树双标记
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 4578 线段树(标记处理)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) ...
- hdu 4533 线段树(问题转化+)
威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4052 线段树扫描线、奇特处理
Adding New Machine Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 1542 线段树扫描(面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- 解决网速慢时maven仓库访问慢
构建maven项目时会下载很多依赖,会从官网地址下载是个外国网站,访问速度会很慢,但可以通过修改maven的settings.xml文件换成国内的镜像地址就可以加快访问速度: 一.找到settings ...
- AI 启蒙-无人售货机智能找零算法
人的理想志向往往和他的能力成正比. --约翰逊-- AI 启蒙-无人售货机智能找零算法 [问题区] 你现在是一家无人售货机生产公司的高级程序员,技术经理叫你实现无人售货机智能找零钱的算法,具体需求 ...
- 【坚持】Selenium+Python学习记录 DAY10
2018/05/31-2018/06/1 [官方文档](https://www.jetbrains.com/help/pycharm/set-up-a-git-repository.html) 通过p ...
- 高可用OpenStack(Queen版)集群-9.Cinder控制节点集群
参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...
- 为centos虚拟机配置固定ip
在virtual上安装centos虚拟机以后,发现虚拟机没有ip,无法联网 将虚拟机的网络适配器改为桥接模式,桥接到物理机的无线网卡 为虚拟机配置固定IP(vi /etc/sysconfig/netw ...
- Python序列之元组 (tuple)
作者博文地址:http://www.cnblogs.com/spiritman/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号, ...
- [linux] ssh远程执行本地脚本
1.ssh密钥登录 略 2.免确认机器指纹,ssh -o StrictHostKeyChecking=no [root@XM-v125 ~]# ssh wykai@192.168.0.110 The ...
- 随手记录-linux-添加epel源
下载各种yum源 https://opsx.alibaba.com/mirror https://blog.csdn.net/harbor1981/article/details/51135623
- 为什么找程序员一定要看他的 GitHub
听说 最开始听到这句话是某知名互联网公司大牛告诉我的,我很不以为然,不过迫于他是"leader"我也注册了一个 highsea (广告 0.0):当然我可懒得 push 更别提 c ...
- WebGL学习笔记四点一
上一章是对图形的变换,这一章的第一节主要介绍了光栅化的过程,在创建多个颜色的三角形的过程中顶点着点器的过程如下 ,1.首先通过attribute的变量从javascript中获取数据,根据drawAr ...