[模板]线段树2-线段树

真的是这题有毒,坑坑坑坑坑坑!!!

但还是写出来了~~~

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6+5;
struct segment_tree{
	ll l,r,sum,add,mul;
}tree[maxn<<2];
ll Mod;
ll n,m;
ll num;
inline void pushup(ll root){
	tree[root].sum = (tree[root<<1].sum+tree[root<<1|1].sum)%Mod;
}
inline void pushdown(ll root){
	tree[root<<1].add = (tree[root].add+tree[root<<1].add*tree[root].mul)%Mod;
    tree[root<<1|1].add = (tree[root].add+tree[root<<1|1].add*tree[root].mul)%Mod;
    tree[root<<1].mul = (tree[root<<1].mul*tree[root].mul)%Mod;
    tree[root<<1|1].mul = (tree[root<<1|1].mul*tree[root].mul)%Mod;
    tree[root<<1].sum = (tree[root<<1].sum*tree[root].mul+tree[root].add*(tree[root<<1].r-tree[root<<1].l+1))%Mod;
    tree[root<<1|1].sum = (tree[root<<1|1].sum*tree[root].mul+tree[root].add*(tree[root<<1|1].r-tree[root<<1|1].l+1))%Mod;
    tree[root].add = 0;
    tree[root].mul = 1;
}
inline ll build(ll x,ll L,ll R){
	tree[x]=segment_tree{L,R,0,0,1};
	if(L == R){
		scanf("%lld",&num);
		return tree[x].sum = num%Mod;
	}
	ll mid=(L+R)>>1;
	return tree[x].sum = (build(x<<1,L,mid)+build(x<<1|1,mid+1,R))%Mod;
}
inline void update(ll x,ll v,ll op,ll L,ll R){
	pushdown(x);
	if(op == 1 && tree[x].l >= L && tree[x].r <= R){
		tree[x].mul = (tree[x].mul*v)%Mod;
		tree[x].add = (tree[x].add*v)%Mod;
		tree[x].sum = (tree[x].sum*tree[x].mul)%Mod;
		return;
	}
	if(op == 2 && tree[x].l >= L && tree[x].r <= R){
		tree[x].add = (tree[x].add+v)%Mod;
		tree[x].sum = (tree[x].sum+tree[x].add*(tree[x].r-tree[x].l+1))%Mod;
		return;
	}
	ll mid = (tree[x].l+tree[x].r)>>1;
	if(L <= mid)update(x<<1,v,op,L,R);
	if(R > mid)update(x<<1|1,v,op,L,R);
	pushup(x);
}
inline ll query(ll x,ll L,ll R){
	pushdown(x);
	if(tree[x].l >= L && tree[x].r <= R)return tree[x].sum%Mod;
	ll mid = (tree[x].l+tree[x].r)>>1;
	return ((L <= mid ? query(x<<1,L,R) : 0)+(R > mid ? query(x<<1|1,L,R) : 0))%Mod;
}
int main(){
	scanf("%lld%lld%lld",&n,&m,&Mod);
	build(1,1,n);
	for(ll i = 1,op,x,y;i <= m;i++){
		ll k;
		scanf("%lld",&op);
		if(op != 3)scanf("%lld%lld%lld",&x,&y,&k),update(1,k,op,x,y);
		if(op == 3)scanf("%lld%lld",&x,&y),printf("%lld\n",query(1,x,y)%Mod);
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/wangyifan124/p/10306756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值