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. sql server 2008r2 清除数据库日志

    USE [master] GO ALTER DATABASE data SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE data  SET REC ...

  2. Qt之模式、非模式、半模式对话框

    简述 关于"模式"和"非模式"对话框,相信大家都比较熟悉,但其中有一个可能很多人都比较陌生,介于两者之间的状态,我们称之为"半模式". 简述 ...

  3. Android利用Http下载文件

    Android利用Http下载文件 一.场景 下载存文本文件和下载如mp3等大容量的文件 界面 二.代码编写 1.AndroidMainfest.xml中配置 主要是解决网络权限和写SDCard的权限 ...

  4. 作业调度框架 Quartz.NET 2.0 StepByStep

    注:目前网上诸多介绍Quartz.net的文章,甚至Quartz.net官网上的Tutorial都是1.0版本的,而这个项目在2.0版本对项目进行了比较大规模的修改,使得原有的很多例子都不能运行,故写 ...

  5. window+git+AndroidStudio+github

    1. 安装配置git 安装:需要从网上下载一个,然后进行默认安装即可.安装完成后,找到 “Git Bash”,点击: 配置: 注意:name和email 只是用来标识身份,但是一定要配置好 2. St ...

  6. 旧书重温:0day2【3】 详细解读PEB法 查找kener32地址

    题外话:上一篇文章中的 PEB法查找kerner32地址的方法 对TEB.PEB .PE结构 知识要求很高,确实在写汇编代码时候小编 感觉自己能力,信手啪啪一顿乱撸,结果一运行,非法访问了,没办法翻阅 ...

  7. HDU 5319 Painter (模拟)

    题意: 一个画家画出一张,有3种颜色的笔,R.G.B.R看成'\',B看成'/',G看成这两种的重叠(即叉形).给的是一个矩阵,矩阵中只有4种符号,除了3种颜色还有'.',代表没有涂色.问最小耗费多少 ...

  8. ASIHTTPRequest详解

    ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRe ...

  9. JSTL获取list的大小,jstl获取list 的长度,EL表达式获取list的长度,EL表达式获取list大小

    在jsp页面中不能通过${list.size}取列表长度,而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pref ...

  10. every day english

    job is in your freedom, not your compliance. through no fault of his own. as far as I understand you ...