为什么会TLE3个点??!
查看原帖
为什么会TLE3个点??!
290095
UKE_自动机楼主2020/8/27 22:58

大佬帮看一下为什么会T掉3个点?? 拿线段树写的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=500010;
struct tree{
	int l,r;
	int sum,add;
}tree[4*N+10]; 
int n,m,a[N];
void build(int p,int l,int r)
{
	tree[p].r=r;
	tree[p].l=l;
	if(r==l)
	{
		tree[p].sum=a[l];
		return;
	}
	int mid=(tree[p].r+tree[p].l)/2;
	build(2*p,l,mid);
	build(2*p+1,mid+1,r);
	tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
}
void spread(int p)
{
	if(tree[p].add)
	{
		tree[2*p].sum+=(tree[2*p].r-tree[2*p].l+1)*tree[p].add;
		tree[2*p+1].sum+=(tree[2*p+1].r-tree[2*p+1].l+1)*tree[p].add;
		tree[2*p].add+=tree[p].add;
		tree[2*p+1].add+=tree[p].add;
		tree[p].add=0; 
	}
}
void change(int p,int l,int r,int d)
{
	if(tree[p].l>=l&&tree[p].r<=r)
	{
		tree[p].sum+=(tree[p].r-tree[p].l+1)*d;
		tree[p].add+=d;
		return;
	}
	spread(p);
	int mid=(tree[p].r+tree[p].l)/2;
	if(mid>=l) change(2*p,l,r,d);
	if(mid<r) change(2*p+1,l,r,d);
	tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
}
ll query(int p,int l,int r)
{
	if(tree[p].l>=l&&tree[p].r<=r) return tree[p].sum;
	spread(p);
	int mid=(tree[p].r+tree[p].l)/2;
	ll ans=0;
	if(mid>=l) ans+=query(2*p,l,r);
	if(mid<r) ans+=query(2*p+1,l,r);
	return ans; 
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		cin>>x>>y>>z;
		if(x==1) change(1,y,y,z);
		else cout<<query(1,y,z)<<endl;
	}
	return 0;
}

2020/8/27 22:58
加载中...