题目;http://acm.hdu.edu.cn/showproblem.php?pid=5475

题意就是给X赋初值1,然后给Q个操作,每个操作对应一个整数M;如果操作是1则将X乘以对应的M,如果是2则除以第M次操作对应的M',求最后X的值对给定值取摸的结果,

直接暴力会爆long long,用数组存每一步的话 会超时,所以用线段树优化进行区间更新

 #include<cstdio>
using namespace std;
typedef long long ll;
struct point {
int l,r;
ll sum;
};
point tree[*];
int n,m;
void build(int i,int left,int right)
{
tree[i].l=left;
tree[i].r=right;
if (left==right)
{
tree[i].sum=;
return;
}
int mid=(left+right)>>;
build(i<<,left,mid);
build(i<<|,mid+,right);
tree[i].sum=(tree[i<<].sum*tree[i<<|].sum)%m;
}
void update(int i,int num,int val)
{
if (tree[i].l==tree[i].r)
{
tree[i].sum=val;
return;
}
int mid=(tree[i].l+tree[i].r)>>;
if (num<=mid) update(i<<,num,val);
else update(i<<|,num,val);
tree[i].sum=(tree[i<<].sum*tree[i<<|].sum)%m;
}
ll _find(int i,int left,int right)
{
ll ans=;
if (left<=tree[i].l&&tree[i].r<=right)
return tree[i].sum;
int mid=(tree[i].l+tree[i].r)>>;
if (left<=mid) ans*=_find(i<<,left,right)%m;
if (right>mid) ans*=_find(i<<|,left,right)%m;
return ans%m;
}
int main()
{
int t,x,y,i,w=;
scanf("%d",&t);
while (t--)
{
printf("Case #%d:\n",w++);
scanf("%d %d",&n,&m);
build(,,n);
for (i=;i<=n;i++)
{
scanf("%d %d",&x,&y);
if (x==)
{
update(,i,y);
printf("%I64d\n",_find(,,i)%m);
}
else
{
update(,y,);
printf("%I64d\n",_find(,,i)%m);
}
}
}
return ;
}

hdu 5475(2015上海网赛) An easy problem的更多相关文章

  1. hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)

    给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...

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

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

  3. hdu 5493 (2015合肥网赛) Queue

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5493 题目大意,t组数据,n个人,n行每行分别是人的身高和这个人的左边或右边比他高的人的个数,输出符合条件 ...

  4. hdu 5491(2015合肥网赛)The Next

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5491 题意就是,T组测试数据.然后L,S1,S2.L的二进制中有x个1,x满足  S1<=x< ...

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

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

  6. hdu 5461(2015沈阳网赛 简单暴力) Largest Point

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5461 题意就是在数组中找出a*t[i]*t[i]+b*t[j]的最大值,特别注意的是这里i和i不能相等,想 ...

  7. hdu 5459(2015沈阳网赛) Jesus Is Here

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意 给出一组字符串,每个字符串都是前两个字符串相加而成,求第n个字符串的c的各个坐标的差的和,结果 ...

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

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

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

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

随机推荐

  1. redmine安装-BitNami 提供的一键安装程序

    redmine安装-BitNami 提供的一键安装程序          博客分类: REDMINE redmine安装redmine一键安装bitNami redmine  BitNami 提供re ...

  2. Python系列之 __new__ 与 __init__

    很喜欢Python这门语言.在看过语法后学习了Django 这个 Web 开发框架.算是对 Python 有些熟悉了.不过对里面很多东西还是不知道,因为用的少.今天学习了两个魔术方法:__new__ ...

  3. 学JS的心路历程-闭包closure

    闭包是是纯函式语言的一个特性,也是JS的一个关键性的特色,虽然不了解也能开发程序,但我们不是这种人对吧? 闭包不仅可以减少某些高阶功能的代码数量和复杂度,并且可以让我们做到原本无法做的复杂功能.听到这 ...

  4. 拓扑排序-有向无环图(DAG, Directed Acyclic Graph)

    条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一 ...

  5. vue如果是首页了 不让其后退

    history.pushState(null, null, document.URL); //首页加载时候先置空 window.addEventListener('popstate', functio ...

  6. ArrayList删除--------ConcurrentModificationException问题

    在做项目中用到List存储数据,在里面做数据操作时候用到了删除.结果抛出ConcurrentModificationException异常.在这里把问题总结一下. 原因: ArrayList进行for ...

  7. mysql执行流程

      https://www.jianshu.com/p/71a98f1347b9 image   image SQL示例: SELECT DISTINCT < select_list > ...

  8. 用js实现九九乘法口诀两种方式

    js实现九九乘法口诀两种方式: 第一种是用户输入一个数弹出所对应的乘法口诀: <script type="text/javascript"> function art( ...

  9. Light Probe Proxy Volume

    [Light Probe Proxy Volume] The Light Probe Proxy Volume (LPPV) component allows you to use more ligh ...

  10. 运行PowerShell脚本

    [运行PowerShell脚本] powershell脚本以ps1为扩展名.最的一个是数字1,不是字母l. 当右键ps1文件时,会有用powershell运行的选项,选中这个选项即可运行. 团体pow ...