http://acm.hdu.edu.cn/showproblem.php?pid=5475

An easy problem

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1146    Accepted Submission(s): 560

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
 
Source
 
Recommend
hujie
 

题意:

原数开始时是1按顺序给你Q个操作,然后其中操作分两种。

1表示将上个操作后的数乘以后面给你的数然后对取余,然后输出结果,2表示除的操作,将现在的数除以指定的先前你所乘上的第几个数

然后对M取模。

思路:

这题用线段树写,断点更新,复杂度是n*(log(n));

代码:

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 typedef long long LL;
7 void up(LL k,LL l);
8 void build(LL l,LL r,LL k);
9 LL que(LL l,LL r,LL k,LL aa,LL dd);
10 typedef struct pp
11 {
12 LL x;
13 LL y;
14 LL id;
15 LL t;
16 } ss;
17 typedef struct tree1
18 {
19 LL x;
20 LL y;
21 LL id;
22 LL cou;
23 } sd;
24 LL M,N;
25 ss cnt[100005];
26 int flag[4*100005];
27 sd tree[4*100005];
28 using namespace std;
29 int main(void)
30 {
31 LL n,i,j,k,p,q;
32 scanf("%lld",&n);
33 for(i=1; i<=n; i++)
34 {
35 scanf("%lld %lld",&N,&M);
36 memset(tree,0,sizeof(tree));
37 for(j=1; j<=N; j++)
38 {
39 scanf("%lld %lld",&cnt[j].x,&cnt[j].y);
40 cnt[j].id=j;
41 if(cnt[j].x==1)
42 {
43 cnt[j].t=cnt[j].y;//操作1时所要乘的数。
44 }
45 else cnt[j].t=1;//操作2时乘的数等价为1;
46 }
47 build(1,N,0);//建树(因为每步操作都有对应的操作,所以将操作2也放入一起操所,等价为所要乘的数为1)
48 printf("Case #%lld:\n",i);
49 for(j=1; j<=N; j++)
50 {
51 if(cnt[j].x==1)
52 {
53 LL dd=que(1,j,0,1,N);//当1时询问找点
54 printf("%lld\n",dd);
55 }
56 else if(cnt[j].x==2)
57 {
58 up(flag[cnt[j].y],j);//当2时断点更新
59 LL dd=que(1,j,0,1,N);//询问找点
60 printf("%lld\n",dd);
61 }
62 }
63 }
64 return 0;
65 }
66 void build(LL l,LL r,LL k)//建树
67 {
68 tree[k].x=l;
69 tree[k].y=r;
70 if(l==r)
71 {
72 tree[k].cou=cnt[l].t%M;
73 flag[l]=k;
74 return;
75 }
76 else
77 {
78 build(l,(l+r)/2,2*k+1);
79 build((l+r)/2+1,r,2*k+2);
80 tree[k].cou=(tree[2*k+1].cou*tree[2*k+2].cou)%M;
81 }
82 }
83 void up(LL k,LL l)//断点更新
84 {
85 tree[k].cou=1;//要删除的点处就相当于乘
86 k=(k-1)/2;
87 while(k>=0)//向上更新到根结点
88 {
89 tree[k].cou=(tree[2*k+1].cou*tree[2*k+2].cou)%M;
90 if(k==0)
91 {
92 break;
93 }
94 k=(k-1)/2;
95 }
96 }
97 LL que(LL l,LL r,LL k,LL aa,LL dd)//询问
98 {
99 if(l>dd||r<aa)
100 {
101 return 1;
102 }
103 else if(l<=aa&&r>=dd)
104 {
105 return tree[k].cou;
106 }
107 else
108 {
109 LL nx=que(l,r,2*k+1,aa,(aa+dd)/2);
110 LL ny=que(l,r,2*k+2,(aa+dd)/2+1,dd);
111 return (nx*ny)%M;
112 }
113
114 }

hud-5475 An easy problem(线段树)的更多相关文章

  1. HDU 5475 An easy problem 线段树

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

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

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

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

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

  4. POJ 2826 An Easy Problem?![线段]

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12970   Accepted: 199 ...

  5. Codeforces 803G Periodic RMQ Problem 线段树

    Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...

  6. Codeforces 903G Yet Another Maxflow Problem - 线段树

    题目传送门 传送门I 传送门II 传送门III 题目大意 给定一个网络.网络分为$A$,$B$两个部分,每边各有$n$个点.对于$A_{i} \ (1\leqslant i < n)$会向$A_ ...

  7. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  8. spoj IITWPC4F - Gopu and the Grid Problem 线段树

    IITWPC4F - Gopu and the Grid Problem no tags  Gopu is interested in the integer co-ordinates of the ...

  9. 【CF903G】Yet Another Maxflow Problem 线段树

    [CF903G]Yet Another Maxflow Problem 题意:一张图分为两部分,左边有n个点A,右边有m个点B,所有Ai->Ai+1有边,所有Bi->Bi+1有边,某些Ai ...

随机推荐

  1. java面试题目偏基础

    一.JAVA基础篇-概念1.简述你所知道的Linux:Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机(An ...

  2. 巩固java第七天

    巩固内容: HTML 属性 属性是 HTML 元素提供的附加信息. HTML 属性 HTML 元素可以设置属性 属性可以在元素中添加附加信息 属性一般描述于开始标签 属性总是以名称/值对的形式出现,比 ...

  3. 日常Java 2021/10/10

    多态就是同一个行为具有多个不同表现形式的能力 多态就是同一个接口,使用不同的实例而执行不同操作 多态的优点 1.消除类型之间的耦合关系 2.可替换性 3.可扩充性 4.接口性 5.灵活性 6.简化性 ...

  4. Linux下强制踢掉登陆用户

    1.pkill -kill -t   tty 例:pkill -kill -t tty1

  5. java列表组件鼠标双击事件的实现

    Swing中提供两种列表组件,分别是列表框(JList)和组合框(JComboBox). 一.JList组件 构造方法: public JList():构造一个空的.具有只读模型的JList.publ ...

  6. 【C/C++】最长无重复子数组

    题目描述 给定一个数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同. 子数组是连续的,比如[1,2,3,4,5]的子数组有[1,2],[2,3,4]等等,但是[1,3, ...

  7. 如何用CodeBlocks调试?

    一.简介 这篇文章我主要会介绍CodeBlocks的调试功能,并简单讲述如何使用它. 二.前言 大家好,最近和小伙伴们讨论修改程序的时候,我突然想到,授人以鱼不如授人以渔(指调试),于是这篇文章应运而 ...

  8. RDS备份到OSS增量+全量

    一.前言 阿里云的RDS备份是占用使用量的,你购买200G那备份使用量是100G左右,导致备份一般也就存半个月,2个全备份. 那半个月后之前的也就删除了,如果要持续保留更久将花费不少的金钱.所以这里用 ...

  9. Samba 源码解析之SMBclient命令流

    smbclient提供了类似FTP式的共享文件操作功能, 本篇从源码角度讲解smbclient的实现,smbclient命令的具体使用可通过help命令和互联网查到大量资料. 以下从源码角度分析一个s ...

  10. CSS的三大特性(继承、层叠和优先级)

    CSS的三大特性(继承.层叠和优先级) 1.css属性的继承 CSS中有些属性是可继承的,何为属性的继承? 一个元素如果没有设置某些属性的值,就会跟随(继承)父元素的属性值.当然,一个元素如果有设置自 ...