线段树 2015上海网络赛 An easy probleam

其实就是一个线段树裸体

==

先build一棵树,树上的每个值为1

ans储存当前树上的所有value之积%m

查询如果为1

那么输出ans[0]

如果为2

那么更新当前点的值为1

代码如下

/********************************************
Author         :Crystal
Created Time   :
File Name      :
********************************************/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
//#define LOCAL
ll ans[1000005];
int q,m;
void pushup(int root){
	ans[root]=(ans[root*2+1]*ans[root*2+2])%m;
	return;
}
void build(int root,int l,int r){
	if(l==r){
		ans[root]=1;
		return;
	}
	int mid = (l+r)>>1;
	build(2*root+1,l,mid);
	build(2*root+2,mid+1,r);
	pushup(root);
	return;
}
void update(int root,int l,int r,int pos,int val){
	if(l==r){
		if(val>0)ans[root]*=val;
		else ans[root]=1;
		return;
	}
	int mid = (l+r)>>1;
	if(pos <= mid)update(root*2+1,l,mid,pos,val);
	else update(root*2+2,mid+1,r,pos,val);
	pushup(root);
	return;
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	int t;cin >> t;
	int kase = 1;
	while(t--){
		cout << "Case #" << kase++ << ':' << endl;
		scanf("%d%d",&q,&m);
		build(0,1,q);
		for(int i=1;i<=q;i++){
			int choose;
			int t;
			scanf("%d%d",&choose,&t);
			if(choose==1){
				update(0,1,q,i,t);
			}
			else{
				update(0,1,q,t,-t);
			}
			printf("%d\n",ans[0]%m);
		}
	}
	return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值