HDU-5475:An easy problem(线段树)
2017-09-13 15:12
323 查看
An easy problem
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536K (Java/Others)
Total Submission(s): 2359 Accepted Submission(s): 949
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
2015 ACM/ICPC Asia Regional Shanghai Online
思路:建立一个[1,Q]的线段树,每个节点存储的是这个区间内的数的乘积。初始所有节点的值为1。当op=1时,把当前节点值改为x。当op=2时,把x节点的值变为1。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6+10;
long long MOD;
struct lenka
{
long long sum,l,r;
}a[MAX<<2];
void build(int k,int l,int r)
{
a[k].l=l,a[k].r=r;
if(l==r){a[k].sum=1%MOD;return;}
build(2*k,l,(l+r)/2);
build(2*k+1,(l+r)/2+1,r);
a[k].sum=a[2*k].sum*a[2*k+1].sum%MOD;
}
void change(int k,long long x,long long tag)
{
if(x==a[k].l&&x==a[k].r){a[k].sum=tag%MOD;return;}
if(x<=a[2*k].r)change(2*k,x,tag);
else change(2*k+1,x,tag);
a[k].sum=a[2*k].sum*a[2*k+1].sum%MOD;
}
int main()
{
int T,n,cas=1;
cin>>T;
while(T--)
{
scanf("%d%lld",&n,&MOD);
build(1,1,n);
printf("Case #%d:\n",cas++);
for(int i=1,op;i<=n;i++)
{
long long x;
scanf("%d%lld",&op,&x);
if(op==1)change(1,i,x);
else change(1,x,1);
printf("%lld\n",a[1].sum);
}
}
return 0;
}
相关文章推荐
- hdu-5475-An easy problem-线段树求乘积
- HDU5475——An easy problem(线段树or暴力)
- hdu 5475 An easy problem(线段树)
- hdu 5475 An easy problem(线段树)
- HDU 5475 An easy problem(线段树)
- HDU 5475 An easy problem (暴力||线段树_好题 )
- hdu 5475 An easy problem(线段树单点更新)
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
- HDU-5475-An easy problem【线段树】
- hdu - 5475 - An easy problem - (线段树)
- hdu 5475 An easy problem 线段树
- HDU 5475 An easy problem (线段树)(单点更新,区间查询)2015ICPC 上海网赛
- HDU-5475 An easy problem(模拟||(倒着计算+线段树))
- [HDU 5475] An easy problem (线段树)
- HDU 5475:An easy problem 这题也能用线段树做???
- 【线段树】单点更新 hdu 5475 An easy problem
- HDU 5475 An easy problem 线段树
- HDU 5475:An easy problem 这题也能用线段树做???
- hdu 5475 An easy problem (线段树)
- HDU 5475 An easy problem(网络赛水题)