An easy problem

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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
思路:线段树单点更新;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+,M=4e6+,inf=1e9+;
ll sum[N<<],mod;
void pushup(int pos)
{
sum[pos]=(sum[pos<<|]*sum[pos<<])%mod;
}
void buildtree(int l,int r,int pos)
{
if(l==r)
{
sum[pos]=;
return;
}
int mid=(l+r)>>;
buildtree(l,mid,pos<<);
buildtree(mid+,r,pos<<|);
pushup(pos);
}
void update(int point,ll change,int l,int r,int pos)
{
if(l==r&&l==point)
{
sum[pos]=change;
return;
}
int mid=(l+r)>>;
if(point<=mid)
update(point,change,l,mid,pos<<);
else
update(point,change,mid+,r,pos<<|);
pushup(pos);
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%lld",&n,&mod);
buildtree(,n,);
printf("Case #%d:\n",cas++);
for(int i=;i<=n;i++)
{
int flag;
ll l;
scanf("%d%lld",&flag,&l);
if(flag==)
update(i,l,,n,);
else
update(l,,,n,);
printf("%lld\n",sum[]);
}
}
return ;
}

hdu 5475 线段树的更多相关文章

  1. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. hdu 3397 线段树双标记

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  6. hdu 4533 线段树(问题转化+)

    威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  7. hdu 2871 线段树(各种操作)

    Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. hdu 1542 线段树扫描(面积)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. Android Java 线程池 ThreadPoolExecutor源代码篇

    线程池简单点就是任务队列+线程组成的. 接下来我们来简单的了解下ThreadPoolExecutor的源代码. 先看ThreadPoolExecutor的简单类图,对ThreadPoolExecuto ...

  2. 第四篇: Ansible 常用模块使用

    一. command | shell 模块 该模块为:执行命令模块 (由于命令模块较为强大,可根据自身对操作系统的熟练程度而增加.所以这里不再介绍类似可以实现同功能模块,比如service模块,需要重 ...

  3. 25线程基础-CLR

    由CLR via C#(第三版) ,摘抄记录... 1.线程是CPU的虚拟化,windows为每个进程提供专用线程(CPU)2.线程开销:内存和时间. 线程内核对象—OS为系统中创建的每个线程都分配并 ...

  4. phpStorm pycharm编辑器主题修改,自定义颜色

    新的启程 注: 本人小菜鸟一枚,内容也是从其他博客中借鉴的,谨以此作为写博客开端. phpstorm修改主题: 1. phpstorm主题下载 http://www.phpstorm-themes.c ...

  5. 大数据学习系列(7)-- hadoop集群搭建

    1.配置ssh免登陆 #进入到我的home目录 cd ~/.ssh ssh-keygen -t rsa 执行完这个命令后,会生成两个文件id_rsa(私钥).id_rsa.pub(公钥) 将公钥拷贝到 ...

  6. Eureka集群搭建

    服务注册.发现是微服务架构的关键原理之一,由于微服务架构是由一系列职责单一的细粒度服务构成的网状结构,服务之间通过轻量机制进行通信,这就必然引入一个服务注册发现的问题,也就是说服务提供方要注册报告服务 ...

  7. 反应器模式 vs 观察者模式

    反应器模式(Reactor pattern)与观察者模式(Observer pattern) 反应器模式 是一种为处理服务请求并发提交到一个或者多个服务处理程序的事件设计模式.当请求抵达后,服务处理程 ...

  8. The hidden layers are either convolutional, pooling or fully connected.

    https://en.wikipedia.org/wiki/Convolutional_neural_network Convolutional layers apply a convolution ...

  9. php输出缓冲区

    ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush( ...

  10. ABAP制作密码输入框

    [转自 http://blog.csdn.net/saphome/article/details/6956911] 这几天做一个系统维护的程序,需要用户输入用户名和密码登录.可怎样实现输入密码显示星号 ...