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

An easy problem

Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 755 Accepted Submission(s):
431

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^5), 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≤10^5,1≤M≤10^9)
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≤10^9)
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
 
题目大意:t组测试数据,q个操作,每个操作(a, y)的结果X对m取余,X最初值为1,如果a==1,将X乘以y
如果a==2,将X除以第y次操作的数(即X要除的数曾出现在乘操作里)
 
线段树:线段树区间维护乘积,将q次操作当做q个节点,每个区间的初始值是1,乘操作(a,y):将该次操作即该节点的数值改成其要乘的值y,
除操作(a,y):将第y次操作即y节点的数值改成1(即可让其免去乘操作),输出从1到q区间的乘积即可
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define Lson root<<1, L, tree[root].Mid()
#define Rson root<<1|1, tree[root].Mid() + 1, R const int N = ;
typedef long long ll; struct Tree
{
ll L, R;
ll sum;
int Mid()
{
return (L + R) / ;
}
} tree[N * ]; ll a[N], m; void Push(int root)
{
tree[root].sum = (tree[root<<].sum * tree[root<<|].sum) % m;
}//维护区间乘积 void Build(int root, ll L, ll R)
{
tree[root].L = L, tree[root].R = R;
if(L == R)
{
tree[root].sum = ;
return ;
} Build(Lson);
Build(Rson); Push(root);
}//建树 void Update(int root, ll op, ll e)
{
if(tree[root].L == op && tree[root].R == op)
{
tree[root].sum = e % m;
return ;
}
if(op <= tree[root].Mid())
Update(root<<, op, e);
else
Update(root<<|, op, e);
Push(root);
}//区间单点更新 int main()
{
int t, q, op, x = ;
scanf("%d", &t);
while(t--)
{
x++;
scanf("%d%lld", &q, &m);
printf("Case #%d:\n", x);
Build(, , q);
for(int i = ; i <= q ; i++)
{
scanf("%d%lld", &op, &a[i]);
if(op == )
Update(, i, a[i]);
else
Update(, a[i], );
printf("%lld\n", tree[].sum);
}
}
return ;
}
 
通过用线段树的思路可以用暴力的方法来解题
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define Lson root<<1, L, tree[root].Mid()
#define Rson root<<1|1, tree[root].Mid() + 1, R const int N = ;
typedef long long ll; ll a[N], m; int main()
{
int t, op, q, x = ;
scanf("%d", &t);
while(t--)
{
x++;
ll ans = ;
scanf("%d%lld", &q, &m);
printf("Case #%d:\n", x);
for(int i = ; i <= q ; i++)
{
scanf("%d%lld", &op, &a[i]);
if(op == )
ans = ans * a[i] % m;
else
{
a[a[i]] = ;
a[i] = ;
ans = ;
for(int j = ; j < i ; j++)
ans = ans * a[j] % m;
}
printf("%lld\n", ans);
}
}
return ;
}
 
 
 

hdu 5475 An easy problem(暴力 || 线段树区间单点更新)的更多相关文章

  1. HDU 1394 Minimum Inversion Number(线段树的单点更新)

    点我看题目 题意 :给你一个数列,a1,a2,a3,a4.......an,然后可以求出逆序数,再把a1放到an后,可以得到一个新的逆序数,再把a2放到a1后边,,,,,,,依次下去,输出最小的那个逆 ...

  2. hdu 1394 Minimum Inversion Number(线段树之 单点更新求逆序数)

    Minimum Inversion Number                                                                           T ...

  3. HDU 1754 I Hate It(线段树之单点更新 区间最值查询)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. hdu 1754 I Hate It(线段树之 单点更新+区间最值)

    I Hate It                                                                             Time Limit: 90 ...

  5. hdu1754线段树的单点更新区间查询

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. HDUOJ---1754 I Hate It (线段树之单点更新查区间最大值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. HDU 1698 Just a Hook (线段树区间更新)

    题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...

  8. hdu 3911 Black And White (线段树 区间合并)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...

  9. HDU 3308 LCIS (线段树&#183;单点更新&#183;区间合并)

    题意  给你一个数组  有更新值和查询两种操作  对于每次查询  输出相应区间的最长连续递增子序列的长度 基础的线段树区间合并  线段树维护三个值  相应区间的LCIS长度(lcis)  相应区间以左 ...

随机推荐

  1. hdu4618 Palindrome Sub-Array dp+记忆化搜索 或者直接暴力

    题意就是找一个 左右上下对称的正方形矩阵. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 没想到记忆+dp和暴力就能水过... //记忆话搜索+d ...

  2. 淘宝主搜索离线集群完成Hadoop 2

    淘宝搜索离线dump集群(hadoop&hbase)2013进行了几次重大升级,本文中将这些升级的详细过程.升级中所遇到的问题以及这些问题的解决方案分享给大家.至此,淘宝主搜索离线集群完全进入 ...

  3. 深入理解OpenERP的工作流(Workflow)

    一.工作流定义: <?xml version="1.0"?>  <terp><data>    <record model="w ...

  4. hibernate不关闭session后果

    (转自:百度知道) 看是怎么获得session的. 方法1: 通过配置监听器后,在Dao中用getCurrentSession获取(内部原理....),此时无需管理session的关闭与否: 方法2: ...

  5. (转) Python Generators(生成器)——yield关键字

    http://blog.csdn.net/scelong/article/details/6969276 生成器是这样一个函数,它记住上一次返回时在函数体中的位置.对生成器函数的第二次(或第 n 次) ...

  6. (六) 6.3 Neurons Networks Gradient Checking

    BP算法很难调试,一般情况下会隐隐存在一些小问题,比如(off-by-one error),即只有部分层的权重得到训练,或者忘记计算bais unit,这虽然会得到一个正确的结果,但效果差于准确BP得 ...

  7. 内存泄露(OOM)现象及举例

    一.HeapSize OOM(堆空间内存溢出) A.eg:List.add(" ")在一个死循环中不断的调用add却没有remove. B.并发导致. 解决方法有:1.代码提速.这 ...

  8. mysql数据库中查询汉字的拼音首字母

    本人提供的方法有如下特点: 1.代码精简,使用简单,只要会基本的SQL语句就行2.不用建立mysql 函数等复杂的东西3.汉字库最全,可查询20902个汉字 方法如下: 1.建立拼音首字母资料表Sql ...

  9. linux下动态库编译的依赖问题

    这里主要是想试验一下,对一个具有多层嵌套的动态库进行编译时,是否要把最底层的库也包含进来的问题,结论是:只要直接依赖的库名称,不需要最底层库名称. 一,目录结构ZZZ├── add│   ├── ad ...

  10. 康乐不风流之爱解题的pde灌水王张祖锦

    康乐不风流之爱解题的pde灌水王张祖锦 师弟: 邓洪存 (现在烟台大学任教) 好吧, 我一直想写写康乐园里与我相熟的这几个人, 不如趁此机会开始. 第一批人物为张祖锦.苏延辉.张会春.黄显涛.刘兴兴. ...