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. TransactionScope的用法

    using (TransactionScope ts = new TransactionScope()) { Model.user_login_log model = new Model.user_l ...

  2. rmdir 命令

    rmdir  不能删除非空目录 1.rmdir aa  删除空文件夹aa 2.rmdir   -p   aa   递归删除文件夹aa   当aa子文件夹被删除后aa也变成空文件夹的话aa也被删除

  3. 【数据挖掘】分类之Naïve Bayes(转载)

    [数据挖掘]分类之Naïve Bayes 1.算法简介 朴素贝叶斯(Naive Bayes)是监督学习的一种常用算法,易于实现,没有迭代,并有坚实的数学理论(即贝叶斯定理)作为支撑. 本文以拼写检查作 ...

  4. 大型网站技术学习-2. 云计算之OpenStack简述

    上一章讲,虚拟化能够充分的利用资源,带来各种各样的好处. 当一个网站不大,只需要四五台机器就可以支撑的时候,可以采用手工的方式虚拟机,但是当网站流量很高,需要成千上万台机器的时候,那就非常不方便了. ...

  5. JS中单引號/双引號以及外部js引入的一些问题

    一.单引號和双引號的使用方法的问题 在JavaScript中能够使用单引號.双引號.二者也能够混合使用. 可是,身为菜鸟的我,却碰到了一些引號的使用问题. <body> <div s ...

  6. Stockbroker Grapevine - poj 1125 (Floyd算法)

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30454   Accepted: 16659 Description S ...

  7. sublime text 配置golang开发环境

    一.准备工作: 1.下载Go语言包: https://code.google.com/p/go/downloads/list 2.下载Git: https://code.google.com/p/ms ...

  8. div+css 画三角形

            <style type="text/css"> .rightdirection { width:0;height:0; line-height:0; b ...

  9. KVM和QEMU的关系(转载)

    From:http://blog.sina.com.cn/s/blog_605f5b4f0102uyjv.html KVM是一种基于CPU硬件辅助的全虚拟化技术,没有CPU硬件虚拟化的支持,KVM无法 ...

  10. php 前台post多维数组

    post多维数组时,在后台用$_POST接收不正常,应使用$_REQUEST