Loading

数学计算

数学计算

solution

这个题似乎没啥要想的东西,直接线段树区间乘数然后取%
注意一点,乘法不同于加法,不能够先%再乘
WA

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define int long long
using namespace std;

inline int read(){
	int x = 0, w = 1;
	char ch = getchar();
	for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') w = -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return x * w;
}

const int maxn = 520520;
int tot, q, m, opt;
int mod;
int a[maxn << 1];

inline void build(int u, int l, int r){
	if(l == r){
		a[u] = 1;
		return;
	}
	int mid = l + r >> 1;
	build(u << 1, l, mid);
	build(u << 1 | 1, mid + 1, r);
	a[u] = a[u << 1] * a[u << 1 | 1];
}

inline void change(int u, int l, int r, int pos, int x){
	if(l == r){
		a[u] = x % mod;
		return;
	}
	int mid = l + r >> 1;
	if(pos <= mid) change(u << 1, l, mid, pos, x);
	else change(u << 1 | 1, mid + 1, r, pos, x);
	a[u] = a[u << 1] * a[u << 1 | 1] % mod;
}

inline void query(int u, int l, int r, int pos, int x){
	if(l == r){
		a[u] = x;
		return;
	}
	int mid = l + r >> 1;
	if(pos <= mod)
		query(u << 1, l, mid, pos, x);
	else query(u << 1 | 1, mid + 1, r, pos, x);
	a[u] = a[u << 1] * a[u << 1 | 1] % mod;
}

signed main(){
	int T = read();
	while(T--){
		int n = read();
		mod = read();
		build(1, 1, n);
		for(int i = 1; i <= n; i++){
			opt = read();
			int m = read();
			if(opt == 1){
				cout << a[1] % mod * m % mod << '\n';
				change(1, 1, n, i, m);
			}
			else{
				query(1, 1, n, m, 1);
				cout << a[1] % mod << '\n';
			}
		}
	}
	return 0;
}

AC

#include<bits/stdc++.h>
#define in read()
#define lc (k<<1)
#define rc (k<<1)|1
#define N 100009
#define ll long long
using namespace std;
inline int read(){
	char ch;int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9'){
		res=(res<<3)+(res<<1)+ch-'0';
		ch=getchar();
	}
	return f==1?res:-res;
}
int t,q,mod,m,op;
ll a[N<<2];
void build(int k,int l,int r){
	if(l==r){a[k]=1;return;}
	int mid=l+r>>1;
	build(lc,l,mid);build(rc,mid+1,r);
	a[k]=a[lc]*a[rc];
}
void insert(int k,int l,int r,int pos,ll x){
	if(l==r){a[k]=x%mod;return;}
	int mid=l+r>>1;
	if(pos<=mid) insert(lc,l,mid,pos,x);
	else insert(rc,mid+1,r,pos,x);
	a[k]=a[lc]*a[rc]%mod;
}
void modify(int k,int l,int r,int pos,int x){//dandian xiugai
	if(l==r){a[k]=x;return;}
	int mid=l+r>>1;
	if(pos<=mid) modify(lc,l,mid,pos,x);
	else modify(rc,mid+1,r,pos,x);
	a[k]=a[lc]*a[rc]%mod;
}
int main(){
	t=in;
	while(t--){
		q=in;mod=in;
		int i,j,k;
		build(1,1,q);
		for(i=1;i<=q;++i){
			op=in;m=in;
			if(op==1){
				printf("%d\n",a[1]%mod*m%mod);
				insert(1,1,q,i,m);
			}
			else{
				modify(1,1,q,m,1);
				printf("%d\n",a[1]%mod);
			}
		}
	}
	return 0;
}
posted @ 2020-05-28 16:33  Gary_818  阅读(187)  评论(0编辑  收藏  举报