java求调,第二个AC,最后三个MLE,其他WA
查看原帖
java求调,第二个AC,最后三个MLE,其他WA
1316333
190859136kkkk楼主2024/3/25 17:11
import java.util.ArrayList;
import java.util.Scanner;

class node{
	int sum,l,r,add;
}

public class SegmentTree {
	static void build(int p,int l,int r,ArrayList<node> a,int[] b) {
		a.get(p).l=l;  a.get(p).r=r;  a.get(p).add=0;  a.get(p).sum=b[l];
		if(a.get(p).l==a.get(p).r) 	
			return;
		int m = (l+r)>>1;
		build(p<<1,l,m,a,b);
		build(p<<1|1,m+1,r,a,b);
		pushup(p,a);
	}
	
	static void update(int x,int y,int n,int p,ArrayList<node> a) { 
		if(x<=a.get(p).l&&y>=a.get(p).r) {
			a.get(p).sum+=(a.get(p).r-a.get(p).l+1)*n;
			a.get(p).add+=n;
			return;
		}
		int m=(a.get(p).l+a.get(p).r)>>1;
		pushdown(p,a);
		if(x<=m) update(x,y,n,p<<1,a);
		if(y>m) update(x,y,n,p<<1|1,a);
		pushup(p,a);
	}
	
	static void pushup(int p,ArrayList<node> a) {
		a.get(p).sum=a.get(p<<1).sum+a.get(p<<1|1).sum;
	}
	
	static void pushdown(int p,ArrayList<node> a) {
		if(a.get(p).add!=0) {
			a.get(p<<1).sum+=(a.get(p<<1).r-a.get(p<<1).l+1)*a.get(p).add;
			a.get(p<<1|1).sum+=(a.get(p<<1|1).r-a.get(p<<1|1).l+1)*a.get(p).add;
			a.get(p<<1).add+=a.get(p).add;
			a.get(p<<1|1).add+=a.get(p).add;
			a.get(p).add=0;
		}
	}
	
	static long query(int p,int x,int y,ArrayList<node> a) {
		if(x<=a.get(p).l&&y>=a.get(p).r) {
			return a.get(p).sum;
		}
		int m =(a.get(p).l+a.get(p).r)>>1;
		int sum =0;
		pushdown(p,a);
		if(x<=m) sum+=query(p<<1,x,m,a);
		if(y>m) sum+=query(p<<1|1,m+1,y,a);
		return sum;
	}
	
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		int n =in.nextInt();
		int m =in.nextInt();
		int[] num =new int[n+1];
		ArrayList<node> a =new ArrayList<>();
		for(int i=1;i<n+1;i++) {
			num[i]=in.nextInt();
		}
		for(int i=0;i<4*n+1;i++) {
			node no =new node();
			a.add(no);
		}
		build(1,1,n,a,num);
		for(int i=0;i<m;i++) {
			int op =in.nextInt();
			if(op==1) {
				int x= in.nextInt(),y=in.nextInt(),k=in.nextInt();
				update(x,y,k,1,a);
			}
			else if(op==2) {
				int x=in.nextInt();
				int y=in.nextInt();
				System.out.println(query(1,x,y,a));	
				}
		}
	}

}

2024/3/25 17:11
加载中...