HDU 5474 - An easy problem (线段树的单点更新)【好题】

版权声明:欢迎转载 https://blog.csdn.net/l18339702017/article/details/81869686

An easy problem

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


 

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

思路:

题目意思非常明确。本来是想着直接模拟求解。但是存在除法取余的操作,并且MOD并非是素数,所以逆元无法得解。因此排除这种思路。

我们利用线段树维护,每个叶子节点的值为当前的操作数,(若y == 1,就把叶子节点的值更新为操作数,若 y==2,就把叶子节点的值更新为1(相当于在之前操作的基础上做了除法))。对于每次询问,我们询问根节点的值即可。

#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;

ll q,m,op[maxn],y;
struct node{
	ll l,r,v;
	ll mid (){
		return (l + r) >> 1;
	}
}tree[maxn<<2];

void pushup(ll x){
	tree[x].v = tree[x<<1].v * tree[x<<1|1].v % m;
}
void build(ll x,ll l,ll r){
	tree[x].l = l;
	tree[x].r = r;
	if(l == r){
		tree[x].v = 1;
		return ;
	}
	int mid = tree[x].mid();
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	pushup(x);
}
void update(ll x,ll pos,ll v){
	if(tree[x].l == pos && tree[x].r == pos){
		tree[x].v = v % m;
		return ;
	}
	int mid = tree[x].mid();
	if(pos <= mid) 	update(x<<1,pos,v);
	else  update(x<<1|1,pos,v);
	pushup(x);
}

int main(){
	int t;
	scanf("%d",&t);
	int T = 1;
	while(t--){
		printf("Case #%d:\n",T++);
		scanf("%lld%lld",&q,&m);
		build(1,1,q);
		line;
		for(int i=1.l;i<=20;i++){
			printf("%lld ",tree[i].v);
		}cout << endl;
		line;
		for(int i=1;i<=q;i++){
			scanf("%lld%lld",&y,&op[i]);
			if(y == 1) update(1,i,op[i]);
			else  update(1,op[i],1);
			printf("%lld\n",tree[1].v);
			line;
			for(int i=1.l;i<=20;i++){
				printf("%lld ",tree[i].v);
			}cout << endl;
			line;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81869686