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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mod 1000000007
#define inf 100000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
struct ss
{
int id,x;
bool operator < (const ss &A)const
{
return id < A.id;
}
};
#define maxn 100000+5
set<ss >s;
set<ss >::iterator it;
int main()
{
int oo=;
ll n,q,m,x[maxn],op[maxn],vis[maxn],ans[maxn],A[maxn];
int T=read();
while(T--)
{
scanf("%I64d%I64d",&n,&m);
FOR(i,,n)
{
scanf("%I64d%I64d",&op[i],&x[i]);
}
mem(vis);
FOR(i,,n)
{
if(op[i]==)vis[x[i]]=;
}
mem(ans);
ans[]=;
FOR(i,,n)
{
ans[i]=ans[i-];
if(op[i]==&&!vis[i])
{
ans[i]=(ans[i]*x[i])%m;
}
}
//cout<<ans[10]<<endl;
s.clear();
for(int i=n; i>=; i--)
{
ll tmp=;
for(it=s.begin(); it!=s.end(); it++)
{
if((*it).id>i)break;
tmp*=(*it).x;
tmp%=m;
}
A[i]=(ans[i]*tmp)%m;
if(op[i]==)
{
ss g;
g.id=x[i];
g.x=x[x[i]];
s.insert(g);
}
}
printf("Case #%d:\n",oo++);
for(int i=; i<=n; i++)
cout<<A[i]<<endl;
}
return ;
}

代码

 

HDU 5475An easy problem 离线set/线段树的更多相关文章

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

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

  2. UESTC 1591 An easy problem A【线段树点更新裸题】

    An easy problem A Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  3. 线段树:CDOJ1592-An easy problem B (线段树的区间合并)

    An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  4. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. [bzoj3339]Rmq Problem||[bzoj3585]mex_线段树

    Rmq Problem bzoj-3339||mex bzoj-3585 题目大意:给定一个长度为n的数列a,多次讯问区间l,r中最小的不属于集合{$A_l,A_{l+1}...A_r$}的非负整数. ...

  7. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  8. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  9. poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...

随机推荐

  1. 面试之Redis

    面:缓存中间件--Memcached和Redis的区别是什么? 答:Memcached的优点是简单易用,代码层次类似与Hash.支持简单数据类型,但不支持数据持久化存储,也不支持主从同步,也不支持分片 ...

  2. 51nod 1551 集合交易 最大权闭合子图

    题意: 市场中有n个集合在卖.我们想买到满足以下要求的一些集合,所买到集合的个数要等于所有买到的集合合并后的元素的个数. 每个集合有相应的价格,要使买到的集合花费最小. 这里我们的集合有一个特点:对于 ...

  3. [Python3网络爬虫开发实战] 1.4.3-Redis的安装

    Redis是一个基于内存的高效的非关系型数据库,本节中我们来了解一下它在各个平台的安装过程. 1. 相关链接 官方网站:https://redis.io 官方文档:https://redis.io/d ...

  4. 新增feeds模块

    很简单,参考<产品差异化定制> 1.在fees/luci/model下面新增文件夹,必须的有:Makefile 和 luasrc/controller/admin/xxx.lua 2.执行 ...

  5. Mysql:sql语句

    练习的表可以在博客的文件中下载 基础知识: 1.数据库的连接 mysql -u -p -h -u 用户名 -p 密码 -h host主机 2:库级知识 2.1 显示数据库: show database ...

  6. 初学微信小程序 TodoList

    微信小程序的学习 微信小程序的开始尝试 TodoList 微信开发者工具生成 目录如下: . |-- app.js |-- app.json |-- app.wxss |-- pages | |-- ...

  7. S3C2440的内存情况在NAND FLASH或者NOR FLASH启动的情况下

    1,从NANDFLASH启动时,在ARM上电时,ARM会自动把NANDFLASH前4K的内容拷贝到S3C2440内部SRAM中,同时把SRAM的地址映射到0X00000000.ARM上电后会从SRAM ...

  8. python接口测试之Http请求(三)

    python的强大之处在于提供了很多的标准库,这些标准库可以直接调用,本节部分,重点学习和总结在 接口测试中Python的Http请求的库的学习. 首先来看httplib,官方的解释为:本模块定义了类 ...

  9. HDU 3157 Crazy Circuits

    Crazy Circuits Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  10. 九度oj 题目1473:二进制数(stack)

    题目1473:二进制数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:9371 解决:2631 题目描述: 大家都知道,数据在计算机里中存储是以二进制的形式存储的. 有一天,小明学了C语 ...