求调线段树,已近吐了
查看原帖
求调线段树,已近吐了
537784
saumtam楼主2023/11/13 20:46

rt,代码比较清奇

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int dd=1e6+5;
struct node{
    int l,r,d,add,mdd,da,bj;
}tree[dd*8+5];
int n,m,a[dd];
inline int read(){
	int x=0,f=0;char c=getchar();
	while(c<'0'||c>'9'){f|=c=='-';c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline void build(int p,int x,int y){
	tree[p].l=x,tree[p].r=y;
	if(x==y){
		tree[p].d=a[x];
		tree[p].da=a[x];
		return;
	}
	int mid=(x+y)>>1;
	build(p<<1,x,mid);
	build(p<<1|1,mid+1,y);
	tree[p].d=tree[p<<1].d+tree[p<<1|1].d;
    tree[p].da=max(tree[p<<1].da,tree[p<<1|1].da);
}
inline void pushdown(int p){
	if(tree[p].add!=0){
		tree[p<<1].d+=tree[p].add*(tree[p<<1].r-tree[p<<1].l+1);
		tree[p<<1|1].d+=tree[p].add*(tree[p<<1|1].r-tree[p<<1|1].l+1);
		tree[p<<1].add+=tree[p].add;
		tree[p<<1|1].add+=tree[p].add;
        tree[p<<1].da+=tree[p].add;
        tree[p<<1|1].da+=tree[p].add;
		tree[p].add=0;
	}
	if(tree[p].bj!=0){
		tree[p<<1].d=(tree[p<<1].r-tree[p<<1].l+1)*tree[p].mdd;
		tree[p<<1|1].d=(tree[p<<1|1].r-tree[p<<1|1].l+1)*tree[p].mdd;
		tree[p<<1].mdd=tree[p].mdd;
		tree[p<<1|1].mdd=tree[p].mdd;
		tree[p<<1|1].da=tree[p].mdd;
		tree[p<<1].da=tree[p].mdd;
		tree[p<<1|1].add=0;
		tree[p<<1].add=0;
		tree[p].mdd=0;
		tree[p].bj=0;
		tree[p<<1].bj=1;
		tree[p<<1|1].bj=1;
	}
}
inline void change(int p,int x,int y,int z){
	if(tree[p].l>=x&&tree[p].r<=y){
		tree[p].add=0;
		tree[p].d=z*(tree[p].r-tree[p].l+1);
		tree[p].mdd=z;
		tree[p].da=z;
		tree[p].bj=1;
		return;
	}
	pushdown(p);
	int mid=(tree[p].l+tree[p].r)>>1;
	if(mid>=x) change(p<<1,x,y,z);
	if(mid<y) change(p<<1|1,x,y,z);
	tree[p].d=tree[p<<1].d+tree[p<<1|1].d;
	tree[p].da=max(tree[p<<1].da,tree[p<<1|1].da);
}
inline void change_add(int p,int x,int y,int z){
	if(tree[p].l>=x&&tree[p].r<=y){
	    tree[p].add+=z;
		tree[p].d+=z*(tree[p].r-tree[p].l+1);
		tree[p].da+=z;
		return;
	}
	pushdown(p);
	int mid=(tree[p].l+tree[p].r)>>1;
	if(mid>=x) change_add(p<<1,x,y,z);
	if(mid<y) change_add(p<<1|1,x,y,z);
	tree[p].d=tree[p<<1].d+tree[p<<1|1].d;
	tree[p].da=max(tree[p<<1].da,tree[p<<1|1].da);
}
inline int ask(int p,int x,int y){
	if(tree[p].l>=x&&tree[p].r<=y) return tree[p].da;
	pushdown(p); 
	int mid=(tree[p].l+tree[p].r)>>1;
	int ans=-LLONG_MAX;
	if(mid>=x) ans=max(ans,ask(p<<1,x,y));
	if(mid<y) ans=max(ans,ask(p<<1|1,x,y));
	return ans;
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++) a[i]=read();
    build(1,1,n);
    while(m--){
		int opt,x,y,z;
		opt=read();
		if(opt==1){
		    x=read(),y=read(),z=read();
			change(1,x,y,z);
		}
		else if(opt==2){
			x=read(),y=read(),z=read();
			change_add(1,x,y,z);
		}
		else{
			x=read(),y=read();
			write(ask(1,x,y));
			putchar('\n');
		}
	}
	return 0;
}
2023/11/13 20:46
加载中...