听灌佬多(玄关)
  • 板块灌水区
  • 楼主__ycy1124__
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/11/13 19:51
  • 上次更新2024/11/13 21:46:24
查看原帖
听灌佬多(玄关)
1287433
__ycy1124__楼主2024/11/13 19:51

线段树模板2

#include<bits/stdc++.h>
#define N (int)1e5+5
using namespace std;
inline int rint(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-'){
			f=-f;
		}
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+(ch^'0');
		ch=getchar();
	}
	return x*f;
}
struct Node{
	long long w;
	int bj,l,r,k;
}node[8*N];
int n,q,m,v[N];
void Down_date(int p){
	if(node[p].bj==1){
		node[p].w*=node[p].k;
		node[p].w%=m;
	}
	else{
		node[p].w+=node[p].k*(node[p].r-node[p].l+1);
		node[p].w%=m;
	}
	if(node[p].l==node[p].r){
		return;
	}
	if(node[p*2].bj!=0){
		Down_date(p*2);
	}
	if(node[p*2+1].bj!=0){
		Down_date(p*2+1);
	}
	node[p*2].bj=node[p].bj;
	node[p*2].k=node[p].k;
	node[p*2+1].bj=node[p].bj;
	node[p*2+1].k=node[p].k;
	node[p].bj=0;
	node[p].k=0;
}
void Push_up(int p){
	if(node[p].l==node[p].r){
		return;
	}
	if(node[p*2].bj!=0){
		Push_up(p*2);
		Down_date(p*2);
	}
	if(node[p*2+1].bj!=0){
		Push_up(p*2+1);
		Down_date(p*2+1);
	}
	node[p].w=node[p*2].w+node[p*2+1].w;
	node[p].w%=m;
}
void New_Tree(int p,int l,int r){
	node[p].l=l;
	node[p].r=r;
	int mid=l+r>>1;
	if(l==r){
		node[p].w=v[l];
		return;
	}
	New_Tree(p*2,l,mid);
	New_Tree(p*2+1,mid+1,r);
	Push_up(p);
}
void Push_date(int p,int l,int r,int op,int k){
//	Push_up(p);
	if(node[p].bj!=0){
		Push_up(p); 
		Down_date(p);
		node[p].bj=0;
		node[p].k=0;
	}
	if(node[p].l==l&&node[p].r==r){
		node[p].bj=op;
		node[p].k=k;
		return;
	}
	if(l==r){
		return;
	}
	int mid=node[p].l+node[p].r>>1;
	if(mid>=r){
		Push_date(p*2,l,r,op,k);
	}
	else if(mid<l){
		Push_date(p*2+1,l,r,op,k);
	}
	else{
		Push_date(p*2,l,mid,op,k);
		Push_date(p*2+1,mid+1,r,op,k);
	}
	Push_up(p);
}
int Get_ans(int p,int x,int y){
	Push_up(p);
	Down_date(p);
	if(node[p].l==x&&node[p].r==y){
		return node[p].w;
	}
	int mid=node[p].l+node[p].r>>1;
	if(x>mid){
		return Get_ans(p*2+1,x,y);
	}
	else if(mid>=y){
		return Get_ans(p*2,x,y);
	}
	else{
		return (Get_ans(p*2,x,mid)+Get_ans(p*2+1,mid+1,y))%m;
	}
	Push_up(p);
}
int main(){
	n=rint(),q=rint(),m=rint();
	for(int i=1;i<=n;i++){
		v[i]=rint();
	}
	New_Tree(1,1,n);
	for(int i=1;i<=q;i++){
		int op=rint();
		if(op==1||op==2){
			int x=rint(),y=rint(),k=rint();
			Push_date(1,x,y,op,k);
		}
		else{
			int x=rint(),y=rint();
			printf("%d\n",Get_ans(1,x,y));
		}
	}
	return 0;
}//2 12 10 6 3
// 2 12 15 11 8
/*
9 5 38
1 5 4 2 3
2 1 4 1
3 2 5
1 2 4 2
2 3 5 5
3 1 1
3 2 2
3 3 3
3 4 4
3 5 5
*/
2024/11/13 19:51
加载中...