大佬们帮忙看看吧,查了好几天了_(:з」∠)_
查看原帖
大佬们帮忙看看吧,查了好几天了_(:з」∠)_
371172
RE_Automaton楼主2022/3/29 12:35

update后的第一次query返回0,下一次query才能返回正确答案 qyNYNV.png

#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define lc(pos) (pos<<1)
#define rc(pos) (pos<<1|1)
#define left(pos) (tree[pos].left)
#define right(pos) (tree[pos].right)
#define sum(pos) (tree[pos].sum)
#define lazy(pos) (tree[pos].lazy)
#define size(pos) (right(pos)-left(pos)+1)
using namespace std;
template<typename T>inline void read(T &a){
	T x=0;a=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')a=-1;ch=getchar();}
	while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	a*=x;
}
struct node{
	int left,right,lazy;
	ll sum;
}tree[maxn<<2];
int a[maxn],n,q;
inline void pushup(int pos){sum(pos)=sum(lc(pos))+sum(rc(pos));}
inline void pushdown(int pos){
	if(lazy(pos))return;
	sum(lc(pos))+=size(lc(pos))*lazy(pos),
	sum(rc(pos))+=size(rc(pos))*lazy(pos),
	lazy(lc(pos))+=lazy(pos),
	lazy(rc(pos))+=lazy(pos),
	lazy(pos)=0;
}
inline void build(int pos,int l,int r){
	left(pos)=l,right(pos)=r;
	if(l==r){sum(pos)=a[l];return;}
	int mid=(l+r)>>1;
	build(lc(pos),l,mid),build(rc(pos),mid+1,r);
	pushup(pos);
}
inline void update(int pos,int l,int r,int k){
	if(l<=left(pos)&&right(pos)<=r){
		sum(pos)+=size(pos)*k;
		lazy(pos)+=k;
		return;
	}
	int mid=(left(pos)+right(pos))>>1;
	pushdown(pos);
	if(l<=mid)update(lc(pos),l,r,k);
	if(r>mid)update(rc(pos),l,r,k);
	pushup(pos);
}
inline ll query(int pos,int l,int r){
	//cout<<pos<<endl;
	if(l<=left(pos)&&right(pos)<=r)return sum(pos);
	int mid=(left(pos)+right(pos))>>1;ll res=0;
	pushdown(pos);
	if(l<=mid)res+=query(lc(pos),l,r);
	if(r>mid)res+=query(rc(pos),l,r);
	return res;
}
int main(){
	int x,y,z,opt;
	read(n),read(q);
	for(int i=1;i<=n;i++)read(a[i]);
	build(1,1,n);for(int i=1;i<=n<<1;i++)cout<<sum(i)<<" ";cout<<endl;
	while(q--){
		read(opt);
		switch(opt){
			case 1:read(x),read(y),read(z);update(1,x,y,z);//for(int i=1;i<=n<<1;i++)cout<<sum(i)<<" ";cout<<endl;
			case 2:read(x),read(y);printf("%lld\n",query(1,x,y));//for(int i=1;i<=n<<1;i++)cout<<sum(i)<<" ";cout<<endl;
		}
	}
	return 0;
}

救救孩子谢谢了orz

2022/3/29 12:35
加载中...