给出有多少次操作 和MOD 初始值为1
操作1 y 表示乘上y
操作2 y 表示除以第 y次操作乘的那个数

线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y
遇到操作2 就把第i个结点的值 替换成1
利用线段树的性质,对整个1~n的区间进行维护,每次输出sum[1]的值即可

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

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <string>
# include <cmath>
# include <queue>
# include <list>
# define LL long long
using namespace std ; const int maxn = ;
int MOD ; LL sum[maxn<<] ; //结点开4倍 void PushUP(int rt) //更新到父节点
{
sum[rt] = (sum[rt * ] * sum[rt * + ]) % MOD ; //rt 为当前结点 } void build(int l , int r , int rt) //构建线段树
{
if (l == r)
{
sum[rt] = ;
return ;
}
int m = (l + r) / ;
build(l , m , rt * ) ;
build(m + , r , rt * +) ;
PushUP(rt) ;
} void updata(int p , LL v , int l , int r , int rt)
{
if (l == r)
{
sum[rt] = v % MOD ;
return ;
}
int m = (l + r) / ;
if (p <= m)
updata(p , v , l , m , rt * ) ;
else
updata(p , v , m + , r , rt * + ) ;
PushUP(rt) ;
} int main()
{
//freopen("in.txt","r",stdin) ;
int T ;
scanf("%d" , &T) ;
int Case = ;
while(T--)
{
Case++ ;
int n ;
scanf("%d %d" , &n , &MOD) ;
build(,n,) ;
int i , op ;
LL y ;
printf("Case #%d:\n", Case);
for (i = ; i <= n ; i++)
{
scanf("%d %I64d" , &op , &y) ;
if (op == )
updata(i , y , , n , ) ;
if (op == )
updata(y , , , n , ) ;
printf("%I64d\n", sum[]);
}
}
return ;
}

hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)的更多相关文章

  1. ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)

    Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...

  2. ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)

    Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...

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

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

  4. UVAlive7141 BombX 14年上海区域赛D题 线段树+离散化

    题意:一个无限大的棋盘, 有n个小兵, 给出了n个兵的坐标, 现在有一个长为width 高为height的炸弹放在棋盘上, 炸弹只能上下左右平移, 不能旋转. 且放炸弹的区域不能含有士兵, 炸弹可以一 ...

  5. HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)

    突然想到的节约时间的方法,感觉6翻了  给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...

  6. hdu 5475(2015上海网赛) An easy problem

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5475 题意就是给X赋初值1,然后给Q个操作,每个操作对应一个整数M:如果操作是1则将X乘以对应的M,如果是 ...

  7. hdu 5455 (2015沈阳网赛 简单题) Fang Fang

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...

  8. hdu 5443 (2015长春网赛G题 求区间最值)

    求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...

  9. hdu 5053 (2014上海网赛L题 求立方和)

    题目大意:给你L到N的范围,要求你求这个范围内的所有整数的立方和. Sample Input2 //T1 32 5 Sample OutputCase #1: 36Case #2: 224 # inc ...

随机推荐

  1. nc命令的常用参数介绍

    nc命令的常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必做运维的应该在网络安全上都对一些开源软件都应该是相当的了解吧,比如tcpdump,namp等神奇,今天要给 ...

  2. mongo查询日期格式数据

    /ali/mongodb/bin/mongo -u user -p '123456' 127.0.0.1:27017/KYElog ISODate方式 db.col_02.find({"Lo ...

  3. React.js基础入门

    本文主要是针对React的一些demo教程.参考了菜鸟教程中的react教程,做了一些总结.Demo的下载链接是 https://github.com/RealAndMe/react-demo 下面要 ...

  4. C++的一些不错开源框架,可以学习和借鉴

    from https://www.cnblogs.com/charlesblc/p/5703557.html [本文系外部转贴,原文地址:http://coolshell.info/c/c++/201 ...

  5. ssh 批量执行命令

    # python3.5 + paramiko # pip 是python的包管理工具,在shell里执行如下命令安装paramoko模块 # pip install paramiko # import ...

  6. Rolling in the Deep (Learning)

    Rolling in the Deep (Learning) Deep Learning has been getting a lot of press lately, and is one of t ...

  7. Postgresql获取所有schema

    Postgresql 连接方式_连接五要素_psql: https://blog.csdn.net/u011402596/article/details/38510547 postgresql的sho ...

  8. Android 实现两个list分别出现(在某一时刻只出现一个控件)

    第一种方法: 在.xml文件中将这两个List分别放入不同的布局管理器中,比如说 <RelativeLayout android:layout_width="match_parent& ...

  9. package.json安装依赖的箭头?

  10. Python文件操作-文件的增删改查

    需求:对文件进行增删改查 由于时间原因,本次代码没有增加任何注释,如有疑问,请联系编辑者:闫龙 其实我也是醉了,看着这些个代码,我脑袋也特么大了,没办法,大神说了,不让用新知识,只可以使用学过的,所以 ...