BZOJ 5334--[Tjoi2018]数学计算(线段树)

5334: [Tjoi2018]数学计算

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 220  Solved: 147
[Submit][Status][Discuss]

Description

小豆现在有一个数x,初始值为1. 小豆有Q次操作,操作有两种类型: 
1 m: x = x  *  m ,输出 x%mod;
2 pos: x = x /  第pos次操作所乘的数(保证第pos次操作一定为类型1,对于每一个类型1 的操作至多会被除一次),输出x%mod
 

 

Input

一共有t组输入(t ≤ 5)
对于每一组输入,第一行是两个数字Q, mod(Q ≤ 100000, mod  ≤ 1000000000); 
接下来Q行,每一行为操作类型op,操作编号或所乘的数字m(保证所有的输入都是合法的).
1 ≤ Q ≤ 100000
 

 

Output

对于每一个操作,输出一行,包含操作执行后的x%mod的值
 

 

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

2
1
2
20
10
1
6
42
504
84
 

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=5334 

Solution

  都2018年了,居然还有省选出模板题的吗?

  对询问建一颗线段树,直接维护。。。

代码

#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
const int N=1e5+50;
LL mod;
int n;
struct node{
	int l,r;
	LL sum;
}d[N<<2];
void build(int l,int r,int t){
	d[t].l=l;d[t].r=r;d[t].sum=1;
	if(l==r)return;
	int mid=l+r>>1;
	build(l,mid,t<<1);
	build(mid+1,r,t<<1|1);
}
void update(int l,int t,LL val){
	if(d[t].l==d[t].r){
		d[t].sum=val%mod;
		return;
	}
	if(l<=d[t<<1].r) update(l,t<<1,val);
	else update(l,t<<1|1,val);
	d[t].sum=d[t<<1].sum*d[t<<1|1].sum%mod;
}
int main(){
	int T;scanf("%d",&T);
	LL x;
	int pos,op;
	while(T--){
		scanf("%d%lld",&n,&mod);
		build(1,n,1);
		for(int i=1;i<=n;++i){
			scanf("%d",&op);
			if(op==1){
				scanf("%lld",&x);
				update(i,1,x);
			}
			else{
				scanf("%d",&pos);
				update(pos,1,1);
			}
			printf("%lld\n",d[1].sum);
		}
	}
	return 0;
}

  

  

This passage is made by Iscream-2001.

 

posted @ 2018-09-15 22:27  Iscream-2001  阅读(229)  评论(0编辑  收藏  举报
/* */ /* */