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

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园的建设目标是通过数据整合、全面共享,实现校园内教学、科研、管理、服务流程的数字化、信息化、智能化和多媒体化,以提高资源利用率和管理效率,确保校园安全。 智慧校园的建设思路包括构建统一支撑平台、建立完善管理体系、大数据辅助决策和建设校园智慧环境。通过云架构的数据中心与智慧的学习、办公环境,实现日常教学活动、资源建设情况、学业水平情况的全面统计和分析,为决策提供辅助。此外,智慧校园还涵盖了多媒体教学、智慧录播、电子图书馆、VR教室等多种教学模式,以及校园网络、智慧班牌、校园广播等教务管理功能,旨在提升教学品质和管理水平。 智慧校园的详细方案设计进一步细化了教学、教务、安防和运维等多个方面的应用。例如,在智慧教学领域,通过多媒体教学、智慧录播、电子图书馆等技术,实现教学资源的共享和教学模式的创新。在智慧教务方面,校园网络、考场监控、智慧班牌等系统为校园管理提供了便捷和高效。智慧安防系统包括视频监控、一键报警、阳光厨房等,确保校园安全。智慧运维则通过综合管理平台、设备管理、能效管理和资产管理,实现校园设施的智能化管理。 智慧校园的优势和价值体现在个性化互动的智慧教学、协同高效的校园管理、无处不在的校园学习、全面感知的校园环境和轻松便捷的校园生活等方面。通过智慧校园的建设,可以促进教育资源的均衡化,提高教育质量和管理效率,同时保障校园安全和提升师生的学习体验。 总之,智慧校园解决方案通过整合现代信息技术,如云计算、大数据、物联网和人工智能,为教育行业带来了革命性的变革。它不仅提高了教育的质量和效率,还为师生创造了一个更加安全、便捷和富有智慧的学习与生活环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值