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. video-editing

    1. 视频编辑 2. 视频编辑软件列表 3. 视频编辑软件比较 4. 视频转换 1. 视频编辑 https://zh.wikipedia.org/wiki/视频编辑 2. 视频编辑软件列表 https ...

  2. ch8 CSS 3列(等高文本列)

    css 3可以创建等高文本列,通过column-count.column-width.column-gap属性实现.假设标记如下: <h1>Socrates</h1> < ...

  3. Mybatis入门(六)联查之一对多

    上一章说了多对一,很多学生被一个老师教,这一章是一个老师教很多学生 目录基本没有变化只是改了配置文件: 2.配置文件: TeacherMapper接口类: package com.hdlf.dao; ...

  4. 三 SVN权限设置&用户&组

    创建组,添加用户之后进行权限的设置:

  5. PCHMI工控组态开发视频教程

    PCHMI是一款适合所有PLC工程师快速上手工控组态开发的控件 下面是视频教程链接 PCHMI工控组态 02-按钮的使用 PCHMI工控组态 03-数据显示器使用 PCHMI工控组态 04-标签控件的 ...

  6. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  7. 第1节 IMPALA:4、5、linux磁盘的挂载和上传压缩包并解压

    第二步:开机之后进行磁盘挂载 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l 开始分区 fdisk /dev/sdb   这个命令执行后依次输 n  p  1  回车  回车  w ...

  8. SQL语句利用日志写shell拿权限

    outfile被禁止,或者写入文件被拦截: 在数据库中操作如下:(必须是root权限) show variables like '%general%'; #查看配置 set global genera ...

  9. gulp和npm等安装

    提前安装了node.js, https://nodejs.org/zh-cn/download/ 跟着提示安装就行,然后执行一下命令cdm看下版本号如下图就说明安装成功了 安装包里面集成了npm,然后 ...

  10. VS2019 添加控制器 主机运行转换时出现问题

    问题: 解决方案: 更换低版本VS,亲测VS2017可行(其它未实测) VS2019目前没找到解决方案,VS版本问题 原文链接