代码求调
  • 板块灌水区
  • 楼主liuhaowen2011
  • 当前回复6
  • 已保存回复6
  • 发布时间2024/11/21 19:58
  • 上次更新2024/11/21 21:17:50
查看原帖
代码求调
1082470
liuhaowen2011楼主2024/11/21 19:58
#include<bits/stdc++.h>
using namespace std;
struct book {
	long num;
	int start;
	int end;
} tree[400001];
int n,m;
int a[100001];
int lazy_tag[400001]= {0};
long ans;
int q;
int aa,bb,cc;
void put_tag(int l,int tl,int tr,int num) {
	if(tree[l].start>=tl&&tree[l].end<=tr) {
		tree[l].num+=num*(tree[l].end-tree[l].start+1);
		if(tree[l].end-tree[l].start)
			lazy_tag[l]+=num;
		return;
	}
	if(tree[l*2].start<=tl&&tree[l*2].end>=tl)
		put_tag(l*2,tl,tr,num);
	if(tree[l*2+1].start<=tr&&tree[l*2+1].end>=tr)
		put_tag(l*2+1,tl,tr,num);
	tree[l].num+=(min(tree[l].end,tr)-max(tree[l].start,tl))*num;
	return;
}
long make_tree(int w,int l,int r) {
	tree[w].start=l;
	tree[w].end=r;
	if(l==r)
		tree[w].num=a[l];
	else
		tree[w].num=make_tree(2*w,l,(l+r)/2)+make_tree(2*w+1,(l+r)/2+1,r);
	return tree[w].num;
}
void find_tree(int l,int r,int w,int tag) {
	tree[w].num+=(min(tree[w].end,r)-max(tree[w].start,l))*tag;
	lazy_tag[w]+=tag;
	if(!(tree[w].start>=l&&tree[w].end<=r)) {
		if((tree[w].start+tree[w].end)/2>=l)
			find_tree(l,r,w*2,lazy_tag[w]);
		if((tree[w].start+tree[w].end)/2+1<=r)
			find_tree(l,r,w*2+1,lazy_tag[w]);
	} else
		ans+=tree[w].num;
	return;
}
int main() {
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++)
		scanf("%d",&a[i]);
	make_tree(1,1,n);
	for(int i=1; i<=m; i++) {
		scanf("%d",&q);
		if(q==1) { //jia
			scanf("%d%d%d",&aa,&bb,&cc);
			put_tag(1,aa,bb,cc);
		}
		if(q==2) { //he
			scanf("%d%d",&aa,&bb);
			ans=0;
			find_tree(aa,bb,1,0);
			cout<<ans<<endl;
		}
	}
	return 0;
}

p3372,哪位大佬调一下?(首答必关,忘私)

2024/11/21 19:58
加载中...