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. 数据结构-哈夫曼(Huffman)

    #include <iostream> #include <cstdio> #include <malloc.h> #define LIST_INIT_SIZE 1 ...

  2. Django2.2使用mysql数据库pymysql版本不匹配问题的解决过程与总结

    前置条件 django版本:2.2.1 python版本:3.6.6 mysql版本:mysql-community8.0.15 问题 在搭建django项目,配置mysql数据库时遇到无法迁移数据库 ...

  3. Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)

    题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...

  4. [Poj3133]Manhattan Wiring (插头DP)

    Description 题目大意:给你个N x M(1≤N, M≤9)的矩阵,0表示空地,1表示墙壁,2和3表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...

  5. V4L2学习(二)结构介绍

    v4l2_device v4l2_device在v4l2框架中充当所有v4l2_subdev的父设备,管理着注册在其下的子设备.以下是v4l2_device结构体原型(去掉了无关的成员): struc ...

  6. layer父页面调用子页面的方法

    由于不知道如何在子页面获取到layer定义的确定按钮,于是就在子页面上定义了一个方法,然后在由父页面在点确定按钮时调用子页面所定义的这个方法,从而执行子页面方法里面的内容: 子页面代码: functi ...

  7. TCP/IP网络编程之多线程服务端的实现(一)

    为什么引入线程 为了实现服务端并发处理客户端请求,我们介绍了多进程模型.select和epoll,这三种办法各有优缺点.创建(复制)进程的工作本身会给操作系统带来相当沉重的负担.而且,每个进程有独立的 ...

  8. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  9. XSS Challenges(1-12关)

    XSS Challenges解析: 1. 什么过滤也没有,而且是直接输出.<scrip>alert(document.domain);</script>即可通过 2. 直接输入 ...

  10. PHP 获取上一个页面的url

    php $_SERVER["HTTP_REFERER"]变量可以获取上一个或前一个页面的URL地址. 比如有一个a.php页面,这个页面上有一个链接指向b.php页面,如果我们在a ...