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. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

  2. BZOJ 1452 Count

    长知识啦..二维BIT. #include<iostream> #include<cstdio> #include<cstring> #include<alg ...

  3. 内核打上yaffs2补丁遇到的问题

    移植yaffs2文件系统时,首先要在内核中添加对yaffs2的支持,使用命令:./patch-ker.sh c 内核目录时,出现下面错误: usage:  ./patch-ker.sh  c/l m/ ...

  4. Java [Leetcode 96]Unique Binary Search Trees

    题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...

  5. appdata文件夹有什么用途?C盘appdata可以删除吗?

    在内存紧张的时候,我们都会选择删除一些无关紧要的大文件来释放内存,有不少网友发现在系统C盘下有一个appdata文件夹,而且体积挺大的,不知道能不能删除,针对此问题,本文就为大家介绍appdata文件 ...

  6. makefile实例(3)-多个文件实例优化

    我们先看一下make是如何工作的在默认的方式下,也就是我们只输入make命令.那么,1.make会在当前目录下找名字叫“Makefile”或“makefile”的文件.2.如果找到,它会找文件中的第一 ...

  7. C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置

    一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...

  8. XSS 前端防火墙(1):内联事件拦截

    关于 XSS 怎样形成.如何注入.能做什么.如何防范,前人已有无数的探讨,这里就不再累述了.本文介绍的则是另一种预防思路. 几乎每篇谈论 XSS 的文章,结尾多少都会提到如何防止,然而大多万变不离其宗 ...

  9. Yii系列教程(三):集成Redis

    1安装Redis 切换至/usr/local/src下,下载并安装redis: $ wgethttp://redis.googlecode.com/files/redis-2.6.12.tar.gz ...

  10. Redis pipeline and list

    Redis Redis 是一个开源的基于内存的数据结构存储器.通常可作为数据库,缓存和消息中介.它支持的数据结构有:字符串.哈希表.列表.集合.支持范围查询的有序集合.位图.hyperloglogs和 ...