码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3468 A Simple Problem with Integers

时间:2017-08-05 17:52:38      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:names   注意   stream   void   simple   cout   标记   out   integer   

线段树成段更新。成段求和

開始没有注意到更新的值能够是负数。结果吧标记初始化为-1。wa到死

#include<iostream>
#include<cstdio>
#define maxn 111111
#define ll long long 
using namespace std;
ll num[maxn];
int n,m;
string cmd;
int a,b,k; 
struct stu
{
	int l,r,m;
	ll sum,flag;
	ll len()
	{
		return r-l+1;
	}
};
stu mapp[maxn*4];
void pushdown(int count)
{
	if(mapp[count].flag!=0)
	{
		mapp[count*2].flag+=mapp[count].flag;
		mapp[count*2+1].flag+=mapp[count].flag;
		mapp[count*2].sum+=mapp[count].flag*mapp[count*2].len();
		mapp[count*2+1].sum+=mapp[count].flag*mapp[count*2+1].len();
		mapp[count].flag=0;
	}
}
void build(int l,int r,int count)
{
	mapp[count].l=l;
	mapp[count].r=r;
	mapp[count].m=(l+r)/2;
	mapp[count].flag=0;
	if(l==r)
	{
		mapp[count].sum=num[l];
		return;
	}
	build(l,(l+r)/2,count*2);
	build((l+r)/2+1,r,count*2+1);
	mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
}
void updata(int l,int r,int count)
{
	if(mapp[count].l==l&&mapp[count].r==r)
	{
		mapp[count].flag+=k;
		mapp[count].sum+=mapp[count].len()*k;
		return;
	}
	pushdown(count);
	if(r<=mapp[count].m) updata(l,r,count*2);
	else if(l>=mapp[count].m+1) updata(l,r,count*2+1); 
	else
	{
		updata(l,mapp[count].m,count*2);
		updata(mapp[count].m+1,r,count*2+1);
	}
	mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
	//cout<<mapp[count].l<<"~"<<mapp[count].r<<"~"<<mapp[count].sum<<endl; 
}
long long que(int l,int r,int count)
{
	if(mapp[count].l==l&&mapp[count].r==r) return mapp[count].sum;
	pushdown(count);
	if(r<=mapp[count].m) return que(l,r,count*2);
	else if(l>=mapp[count].m+1) return que(l,r,count*2+1); 
	else
	{
		return que(l,mapp[count].m,count*2)+que(mapp[count].m+1,r,count*2+1);
	}
	//cout<<mapp[count].l<<"~"<<mapp[count].r<<"~"<<mapp[count].sum<<endl; 
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%I64d",&num[i]);
		}
		build(1,n,1);
		for(int i=0;i<m;i++)
		{
			cin>>cmd;
			if(cmd=="Q")
			{
				scanf("%d%d",&a,&b);
				printf("%I64d\n",que(a,b,1));
			}
			else
			{
				scanf("%d%d%d",&a,&b,&k);
				updata(a,b,1);
			}
		}
	}
	return 0;
} 


poj 3468 A Simple Problem with Integers

标签:names   注意   stream   void   simple   cout   标记   out   integer   

原文地址:http://www.cnblogs.com/clnchanpin/p/7290624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!