An easy problem

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 467    Accepted Submission(s): 258

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),
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≤105,1≤M≤109)

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≤109)

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

题意是一台计算器,最开始的数字是1,然后对其不断操作,输入的操作为1时,乘以后面的数y。输入的操作为2时,除以第y次操作的数。问每一次操作后的结果是多少。

哇,这题居然是线段树做法,真的是没想到啊,发现线段树的题竟然这么广。

对每一个定点进行更新,对整个线段树求乘积。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no
{
int L,R;
long long mul;
}tree[400015]; int root,n;
long long mod; void buildtree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R; tree[root].mul=1; if(L!=R)
{
buildtree(root*2+1,L,(L+R)/2);
buildtree(root*2+2,(L+R)/2+1,R);
}
} void insert(int root,int s,int e,long long val)
{
if(tree[root].L==s&&tree[root].R==e)
{
tree[root].mul=val%mod;
return;
}
if(e<=(tree[root].L+tree[root].R)/2)
{
insert(root*2+1,s,e,val);
}
else if(s>=(tree[root].L+tree[root].R)/2+1)
{
insert(root*2+2,s,e,val);
}
else
{
insert(root*2+1,s,(tree[root].L+tree[root].R)/2,val);
insert(root*2+2,(tree[root].L+tree[root].R)/2+1,e,val);
}
tree[root].mul = (tree[2*root+1].mul * tree[2*root+2].mul)%mod;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int test,i,j,oper;
long long val;
scanf("%d",&test); for(i=1;i<=test;i++)
{
printf("Case #%d:\n",i);
scanf("%d%lld",&n,&mod); buildtree(0,1,n);
for(j=1;j<=n;j++)
{
scanf("%d%lld",&oper,&val);
if(oper==1)
{
insert(0,j,j,val);
}
else
{
insert(0,val,val,1);
}
printf("%lld\n",tree[0].mul);
}
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5475:An easy problem 这题也能用线段树做???的更多相关文章

  1. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  2. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. 线段树:CDOJ1597-An easy problem C(区间更新的线段树)

    An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  4. HDOJ(HDU) 2123 An easy problem(简单题...)

    Problem Description In this problem you need to make a multiply table of N * N ,just like the sample ...

  5. 2015上海网络赛 HDU 5475 An easy problem 线段树

    题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. #include<iostream> #include<cstdio> #include<cstr ...

  6. 【BZOJ4999】This Problem Is Too Simple!(线段树)

    [BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...

  7. BZOJ_3038_上帝造题的七分钟2_线段树

    BZOJ_3038_上帝造题的七分钟2_线段树 题意: XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分 ...

  8. 数据结构(主席树):HDU 4729 An Easy Problem for Elfness

    An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (J ...

  9. HDOJ(HDU) 2132 An easy problem

    Problem Description We once did a lot of recursional problem . I think some of them is easy for you ...

随机推荐

  1. Shiro登录身份认证(从SecurityUtils.getSubject().login(token))到Realm的doGetAuthenticationInfo

    ssm框架下,controller接收到登录请求交给Service并开始处理流程: 1.Service的login方法: @Service public class SysUserServiceImp ...

  2. 清除DNS解析缓存

    接下来在弹出的命令提示符窗口中输入“ipconfig /displaydns”,我们会看到系统中有多条我们之前使用过的DNS地址,如下图所示 5 然后,我们接着输入命令“ipconfig /flush ...

  3. PHP几个快速读取大文件例子

    PHP几个快速读取大文件例子 感谢 把我给崩了 的投递 时间:2014-10-16 来源:三联 在PHP中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents ...

  4. (转)C#的 GC工作原理基础

    作为一位C++出身的C#程序员,我最初对垃圾收集(GC)抱有怀疑态度,怀疑它是否能够稳定高效的运作:而到了现在,我自己不得不说我已经逐渐习惯并依赖GC与我的程序“共同奔跑”了,对“delete”这个习 ...

  5. Java自动检测文件编码(字符集)

    // 使用之前请调用getAllDetectableCharsets()检查是否满足要求,中文仅有{gb18030, big5,utf-*}import com.ibm.icu.text.Charse ...

  6. APP自动化测试获取包名的两种方法

    获取包名的两种方法: 一.通过aapt获取 1.进入aapt.exe所在路径 2.在地址栏输入cmd回车,打开dos命令窗口. 3.在命令窗口输入 aapt dump badging 拖入apk 回车 ...

  7. ionic实现滑动的三种方式

    在移动端受屏幕大小所限,展示内容很多的时候,就要使部分区域进行滑动.本文展示项目中所有到的几种方式,大家可以看自己的需求选择合适的滑动方式.实现滑动的基本原理,有两个容器A.B,假如A在外层,B在内层 ...

  8. 一个自己实现的Vector 完善版本

    一个自己实现的Vector(只能处理基本类型数据) 转载自: https://www.ev0l.art/index.php/archives/22/ string 类型不行 bool char* in ...

  9. 吴裕雄--天生自然java开发常用类库学习笔记:SortedSet接口

    import java.util.SortedSet ; import java.util.TreeSet ; public class TreeSetDemo05{ public static vo ...

  10. poj 1318 Word Amalgamation

    Word Amalgamation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9968   Accepted: 4774 ...