hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)
给出有多少次操作 和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题 线段树)的更多相关文章
- ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)
Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...
- 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 ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- UVAlive7141 BombX 14年上海区域赛D题 线段树+离散化
题意:一个无限大的棋盘, 有n个小兵, 给出了n个兵的坐标, 现在有一个长为width 高为height的炸弹放在棋盘上, 炸弹只能上下左右平移, 不能旋转. 且放炸弹的区域不能含有士兵, 炸弹可以一 ...
- HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)
突然想到的节约时间的方法,感觉6翻了 给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...
- hdu 5475(2015上海网赛) An easy problem
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5475 题意就是给X赋初值1,然后给Q个操作,每个操作对应一个整数M:如果操作是1则将X乘以对应的M,如果是 ...
- hdu 5455 (2015沈阳网赛 简单题) Fang Fang
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...
- hdu 5443 (2015长春网赛G题 求区间最值)
求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...
- hdu 5053 (2014上海网赛L题 求立方和)
题目大意:给你L到N的范围,要求你求这个范围内的所有整数的立方和. Sample Input2 //T1 32 5 Sample OutputCase #1: 36Case #2: 224 # inc ...
随机推荐
- bzoj千题计划121:bzoj1033: [ZJOI2008]杀蚂蚁antbuster
http://www.lydsy.com/JudgeOnline/problem.php?id=1033 经半个下午+一个晚上+半个晚上 的 昏天黑地调代码 最终成果: codevs.洛谷.tyvj上 ...
- 搭建zookeeper单机版以及简单命令的使用
1:创建目录 #数据目录dataDir=/opt/hadoop/zookeeper-3.3.5-cdh3u5/data#日志目录dataLogDir=/opt/hadoop/zookeeper-3.3 ...
- pentaho bi server 配置MySQL数据库
软件版本: jdk 1.7 MySQL 5.5 biserver-ce-6.1.0.1-196 (选择右下方的所有选项See All Activities) 一.前置环境安装 1.安装jdk(略) 2 ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...
- 守卫者的挑战(guard)
problem Pro 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如果你能通过 ...
- tensorflow环境安装
tensorflow环境安装1.安装虚拟机Virtrualbox下载地址:https://www.virtualbox.org/wiki/Downloads 2.下载安装Ubuntu镜像下载地址:ht ...
- 关于Unix/Linux的终端、伪终端、控制台和shell
历史是什么:是过去传到将来的回声,是将来对过去的反映. ——雨果(法)<笑面人> 阅读本文大概需要花费你15分钟 文章导航: 计算机的发展 UNIX系统的诞生 UNIX系统的发展 终端与控 ...
- css3新增的属性
由于CSS5标准还未完全订下来,所以各种内核的浏览器都有自己的标准,为了不使属性混淆,所以各家在各自标准前加了一个前缀, 如:-moz- firefox火狐 -ms- IE ...
- excel导入时候日期格式转成date
最近在做导入的时候发现,excel中设置数值格式是不能有日期的那些符号出现的,/ - : 之类的,否则就会变成数字到了java后台,设置成日期,比如 yyyy-mm-dd 到了后台也是数字,即距离19 ...
- pytorch梯度裁剪(Clipping Gradient):torch.nn.utils.clip_grad_norm
torch.nn.utils.clip_grad_norm(parameters, max_norm, norm_type=2) 1.梯度裁剪原理(http://blog.csdn.net/qq_29 ...