An easy problem

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

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
 
题意:
初始化 s = 1
操作1 x,将 s = s*x%mod;
操作2 y,将第y步的x拿出来, s = s/x%mod;
问每一步操作得到的数字是多少。
开始看的时候完全不知道如何下手,有除法操作的取模运算,逆元?不行。然后找题解,然后发现自己的思维太局限了。除法不行的话那我们就将之前的的乘法操作取消就OK了啊!所以弄个标记数组,每一次出现除法的时候再对前前面的数扫一遍就行了,标记我们要除的那位,就等于之前没有乘过了。不过这样做很冒险的,O(n^2) 10^5的数据量。。然后就是强大的线段树的用场了。
4000ms+:
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long LL;
int a[],vis[];
int main()
{
int tcase;
scanf("%d",&tcase);
int t = ;
while(tcase--){
printf("Case #%d:\n",t++);
int n;
LL mod;
LL s=;
scanf("%d%lld",&n,&mod);
for(int i=;i<=n;i++){
vis[i] = false;
int k,b;
scanf("%d%d",&k,&b);
if(k==){
vis[i] = true;
a[i] = b;
s=s*a[i]%mod;
}else{
s = ;
for(int j=;j<i;j++){
if((LL)j==b){
vis[j] = false;
}else if(vis[j]){
s=s*a[j]%mod;
}
}
}
printf("%lld\n",s);
}
}
return ;
}

线段树版本:

1400ms+

#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long LL;
LL mod;
struct Tree{
int l,r;
LL v;
}tree[*];
void pushup(int idx){
tree[idx].v = (tree[idx<<].v*tree[idx<<|].v)%mod;
}
void build(int l,int r,int idx){
tree[idx].l = l;
tree[idx].r = r;
if(l==r){
tree[idx].v = ;
return ;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
pushup(idx);
}
void update(int idx,int v,int id){
if(tree[idx].l==tree[idx].r){
tree[idx].v = v;
return ;
}
int mid = (tree[idx].l+tree[idx].r)>>;
if(mid>=id) update(idx<<,v,id);
else update(idx<<|,v,id);
pushup(idx);
}
int main()
{
int tcase;
scanf("%d",&tcase);
int t = ;
while(tcase--){
printf("Case #%d:\n",t++);
int n;
LL s=;
scanf("%d%lld",&n,&mod);
build(,n,);
for(int i=;i<=n;i++){
int k,b;
scanf("%d%d",&k,&b);
if(k==){
update(,b,i);
}else{
update(,,b);
}
printf("%lld\n",tree[].v);
}
}
return ;
}

hdu 5475(打破固定思维OR线段树)的更多相关文章

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

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

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

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

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

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

  4. [HDU]1166敌兵布阵<静态线段树>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意:给出n个点,每个点有一个值,现在有三种操作, 1.在i点加上j 2.在i点减去j 3. ...

  5. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  7. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  8. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

  9. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

随机推荐

  1. 数据结构-单链表(Linked List)

    #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...

  2. XmlSerializer 短信备份

    package com.itheima.mobileguard.utils; import java.io.File; import java.io.FileNotFoundException; im ...

  3. Struts1 部分源码学习

    Struts1工作原理    1.系统初始化(读取配置):初始化ModuleConfig对象       Struts框架是一个总控制器(ActionServlet)是一个Servlet,在web.x ...

  4. java中equals和==

    https://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html

  5. lucene.NET详细使用与优化详解

    lucene.NET详细使用与优化详解 http://www.cnblogs.com/qq4004229/archive/2010/05/21/1741025.html http://www.shan ...

  6. linux环境搭建系列之tomcat安装步骤

    前提: Linux centOS 64位 JDK 1.7 安装包从官网上下载 安装Tomcat之前要先安装JDK. 我的JDK是1.7版本的,所以Tomcat版本也选了7的 1.新建目录tomcat ...

  7. Vue+Django REST framework打造生鲜电商项目

    1-1 课程导学 2-1 Pycharm的安装和简单使用 2-2 MySQL和Navicat的安装和使用 2-3 Windows和Linux下安装Python2和Python3 2-4 虚拟环境的安装 ...

  8. shell执行mysql的脚本(包括mysql执行shell脚本)

    在Shell中执行mysql的脚本,这里介绍比较容易使用的一种方法 首先写好sql的脚本,后缀为.sql,比如 sql_file.sql:内容如下 #这是SQL的脚本create table if n ...

  9. java流(二)

    目录 1 ObjectOutputStream/ObjectInputStream的使用 2 序列化 3 具体序列化的过程 4 Externalizable的简易介绍 实现序列化的Person类 /* ...

  10. web项目中各种路径的获取(复制,为以后好找资源)

    web项目中各种路径的获取 1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获 ...