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. vbox虚拟机vdi文件用VMware打开

    转自:https://blog.51cto.com/dahui09/1863486 方法一: 使用VirtualBox 自带的VBoxManage来进行格式转换: 1.安装VBoxManage 2.使 ...

  2. java程序员的就业指导(重点)

    想要成为合格的Java程序员或工程师到底需要具备哪些专业技能,面试者在面试之前到底需要准备哪些东西呢?本文陈列的这些内容既可以作为个人简历中的内容,也可以作为面试的时候跟面试官聊的东西,你可以把这些内 ...

  3. android 桌面透明

      目录(?)[-] public void setWallpaperOffsetSteps float xStep float yStep Parameters public void setWal ...

  4. 软件环境常识 --dev sit uat

    DEV环境:DEV顾名思义就是develop,即代码开发的环境. SIT环境:System Integration Test系统集成测试,开发人员自己测试流程是否走通. UAT环境:User Acce ...

  5. 闲谈“如何优化SSH框架的项目”

    使用struts框架的好处之一就是所有action类继承一个基类,将访问控制在基类中处理.2.所有的action类都继承自baseaction,一个资源对应一个action类.1.实现一个继承自str ...

  6. 一 CRM 注册功能实现

    前端:登陆页面按钮跳转到注册页面 dao:  配置连接池 配置session工厂,Hibernate核心配置,映射 配置UserDao,注入session工厂 UserDao:继承HibernateD ...

  7. anaconda 创建虚拟环境(自己版本)

    首先安装anaconda(3) Anacond的介绍Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Ana ...

  8. css - flex 定义排列方向

    flex-direction定义伸缩项目放置在伸缩容器的排列方向,对应有四个值: (1)row:从左到右或从右到左 (2)row-reverse:与row属性相反 (3)column:从上到下排列 ( ...

  9. Day6 - I - Sticks Problem POJ - 2452

    Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented b ...

  10. 史上最全的mysql聚合函数总结(与分组一起使用)

    1.首先我们需要了解下什么是聚合函数 聚合函数aggregation function又称为组函数. 认情况下 聚合函数会对当前所在表当做一个组进行统计. 2.聚合函数的特点 1.每个组函数接收一个参 ...