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 ...
随机推荐
- SystemVerilog基本语法总结(下)
2018年IC设计企业笔试题解析-(验证方向) 1.请简述:定宽数组,动态数组,关联数组,队列四种数据类型的各自特点.解析:(1)定宽数组:其宽度在声明的时候就指定了,故其宽度在编译时就确定了.(2) ...
- [SUCTF 2019]CheckIn
进入靶场只有一个文件上传界面,上传php一句话木马提示非法后缀. 尝试利用php后缀的多样性绕过,发现都被过滤掉了,先把php改成.jpg的图片,提示说有问号在内容中. php后缀的多样性:php语言 ...
- P1091合唱队形(LIS问题)
题目描述(题目链接:https://www.luogu.org/problem/P1091) NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱 ...
- 「SP1043」GSS1 - Can you answer these queries I
传送门 Luogu 解题思路 这题就是 GSS3 的一个退化版,不带修改操作的区间最大子段和,没什么好讲的. 细节注意事项 咕咕咕 参考代码 #include <algorithm> #i ...
- 嵊州普及Day6T1
题意:有一个矩形,由正负整数构成.一个位子的魅力值为相邻的格子,若与邻格同号则减去绝对值,若异号则加上绝对值. 思路:一个格子一个格子计算即可,没什么好说的. 见代码: #include<ios ...
- 通过JAVA反射修改JDK1.6*当中DNS缓存内容
时间 2012-05-28 17:09:03 Taobao QA Team原文 http://qa.taobao.com/?p=15523 为了实现性能压测时的域名动态绑定功能,尝试通过java反 ...
- 性能测试中TPS上不去的几种原因
性能测试中TPS上不去的几种原因 什么叫TPS: TPS(Transaction Per Second):每秒事务数,指服务器在单位时间内(秒)可以处理的事务数量,一般以request/second为 ...
- 吴裕雄--天生自然JAVAIO操作学习笔记:File类
import java.io.File ; import java.io.IOException ; public class FileDemo01{ public static void main( ...
- C语言整理复习——指针
指针是C的精华,不会指针就等于没学C.但指针又是C里最难理解的部分,所以特意写下这篇博客整理思路. 一.指针类型的声明 C的数据类型由整型.浮点型.字符型.布尔型.指针这几部分构成.前四种类型比较好理 ...
- $.ajax方法提交数组参数
springmvc框架 var param = new Object(); var arr = new Array(); arr.push(1,2,3); param.ids=JSON.stringi ...