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. java中如何判断一个字符串是否包含另外一个字符串的方法

    indexOf(String s)的使用,如果包含,返回的值是包含该子字符串在父类字符串中起始位置: 如果不包含必定全部返回值为-1 package my_automation; public cla ...

  2. Delphi字符串处理函数

    1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即被复制的字符串),第二个参数是从字符串某一处开始复制,第三个参数是要复制字符串的长度(即个数) ...

  3. MFC定时关机程序的实现2-添加启动项到注册表

    虽然上一篇实现了的定时关机,但是还不够完善,比如开机自动启动,然后按照配置的时间定时关机,并最小化到任务栏. 先来说开机启动怎么实现,开机启动实现的方法有好几种,比如直接在开始菜单启动项里添加一个程序 ...

  4. 详细的Windows下安装 binwalk

    1. https://github.com/ReFirmLabs/binwalk到这里下载binwalk,下载后解压. 2. 找到下载后的文件夹, 在这里要进行安装步骤,一边按着shift,一边按着鼠 ...

  5. PostgreSQL查看索引的使用情况

    查看某个表的索引使用情况 select relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch from pg_stat_user_i ...

  6. python re 模块小结

    前言: 本人环境windows 7 64位,python2.7 re是什么: regular expression缩写,意为正则表达式,是python的众多模块之一 re用途: 从文本中有选择的批量抽 ...

  7. 软工实践 - 第十次作业 Alpha 冲刺 (2 / 10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/9960710.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去 ...

  8. 使用xcache加速PHP运行

    XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编 ...

  9. AngularJs之HelloWorld

    <!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="UT ...

  10. POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 ...