HDU 5475:An easy problem 这题也能用线段树做???
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
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.
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.
Then Q lines follow, each line please output an answer showed by the calculator.
1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7
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 这题也能用线段树做???的更多相关文章
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 线段树:CDOJ1597-An easy problem C(区间更新的线段树)
An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- 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 ...
- 2015上海网络赛 HDU 5475 An easy problem 线段树
题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. #include<iostream> #include<cstdio> #include<cstr ...
- 【BZOJ4999】This Problem Is Too Simple!(线段树)
[BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...
- BZOJ_3038_上帝造题的七分钟2_线段树
BZOJ_3038_上帝造题的七分钟2_线段树 题意: XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分 ...
- 数据结构(主席树):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 ...
- 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 ...
随机推荐
- Linux CentOS7 VMware 文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr
一.文件和目录权限chmod u User,即文件或目录的拥有者:g Group,即文件或目录的所属群组:o Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围:a All,即全部 ...
- Github版本控制系统
Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). 特别推荐简单易懂的廖雪锋大神制作的学习教程: https://www.liaoxuefeng.com/wiki/896043 ...
- 了解AOP以及实现方式
AOP是什么? 面向切面编程,把那些与业务无关,却为业务模块所共同调用的逻辑封装成一个可重的模块,即切面 使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点.业 ...
- dwr超时
DWR可以指定超时设置: 1.设置局部超时: RemoteBean.remoteMethod(param1, param2, ..., { callback: callbackfun, //回调函数 ...
- upper_bound()和low_bound函数的基本使用和理解(转载,已获博主授权)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sdz20172133/article/details/80101838 前提:一个非降序列!!!!! ...
- SmartAssembly .net混淆后,无法找到部分类型
两种解决方式: 1,在vs项目引用中,COM 嵌入互操作类型, 全部设为false. 2, 在混淆选项中,排除所有 引有的 外部COM类
- Acwing200 Hankson的趣味题
原题面:https://www.acwing.com/problem/content/202/ 题目大意:gcd(x,a0)=a1,lcm(x,b0)=b1,问你有多少满足条件的正整数x. 输入描述: ...
- Window Server 2019 配置篇(2)- 在window server core上安装网络跟DHCP服务
上一篇我们已经建立了自己的域服务器 之后我们将安装一个window server core,也就是没有GUI只有命令行的window server,并在其上安装网络服务和DHCP 首先创建一个新的虚拟 ...
- ES6学习笔记-扩展运算符(...)
扩展运算符的定义: es6中引入扩展运算符(...),它用于把一个数组转化为用逗号分隔的参数序列. 它常用在不定参数个数时的函数调用,数组合并等情形. 用法一:不定参数个数时的函数调用 <scr ...
- 20200119日志 EPLAN高压房 VFD 单线图 心得
提纲: EPLAN 画单线图的方法,可以先绘制原理图,然后在一个柜子里面的器件 用方框圈起来.方框名称一样.注意 一个工程里面的器件编号是唯一的. 断路器选型. PT 手车 接地刀作用 具体内 ...