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. Python入门必学:字符串和编码正确的使用方法

    字符编码,我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...

  2. 893E - Counting Arrays

    E. Counting Arrays time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  3. DiyCode开源项目 TopicActivity 分析

    1.首先看看TopActivity效果.    2.TopicActivity是一个继承BaseActivity的.前面分析过BaseActivity了.主要有一个标题栏,有返回的图标. 3.贴一下T ...

  4. Android stadio 调试太掉了

    1.evalute expresstion 可以看任何变量的任何属性,就算是一个字符串url,你可以url.length(),你不用输入完就有提示.对象的方法有提示! 2.调试技巧 就是当一行里面有很 ...

  5. “帮你APP”团队冲刺8

    1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...

  6. 03013_动态页面技术-JSP

    1.jsp的出现 2.jsp脚本和注释 (1)jsp脚本 ①<%java代码%> ----- 内部的java代码翻译到service方法的内部: ②<%=java变量或表达式> ...

  7. Falsk

    flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...

  8. laravel5.2总结--数据填充

      1 生成一个seeder文件 你可以通过 make:seeder artisan命令来生成一个 Seeder.所有通过框架生成的 Seeder 都将被放置在 database/seeds 路径: ...

  9. 【网易严选】iOS持续集成打包(Jenkins+fastlane+nginx)

    本文来自网易云社区 作者:孙娇 严选iOS客户端的现有打包方式是通过远程连接打包机执行脚本去打包,打完包会输出相应的ipa的二维码,扫一扫二维码可以安装,但是随着测试队伍的壮大,外包同学越来越多,在打 ...

  10. centOS如何设置时间同步

     1.进入系统-管理-时间和日期  2.这个需要root权限才能进行设置,在弹出框中填入root密码  3.设置时间和日期-勾选同步,并且选择NTP时间服务器,点击确定 4.选择时区为亚洲上海点击保存 ...