Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

尼玛!!!线段树像乌龟!!

Posted by wocha at 2012-07-26 22:17:57 on Problem 3468
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define M 1000000+5
using namespace std;
struct note
{
	__int64  left,right,sum,add;
int  flag;
}tree[M*4];
__int64  a[M];

void build (__int64  l,__int64  r,__int64  i)
{
	if(l==r) tree[i].sum=a[r];
	tree[i].left=l;
	tree[i].right=r;
	tree[i].add=0;
	if(l==r) return;
	__int64  mid=(l+r)/2;
	build(l,mid,i<<1);
	build(mid+1,r,i<<1|1);
	tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
void insert(__int64 l,__int64  r,__int64  i,__int64  val)
{

	if(tree[i].left==l&&tree[i].right==r)
    {
 	   tree[i].add+=val;
		return;
   	}
   if(tree[i].left==tree[i].right) return ;
    __int64      mid=(tree[i].left+tree[i].right)/2;
 	if(r<=mid)     insert(l,r,i<<1,val);
 	else if(l>mid) insert(l,r,i<<1|1,val);
 	else
     {
         insert(l,mid,i<<1,val);
			   insert(mid+1,r,i<<1|1,val);
	   }
	  tree[i].sum=   tree[i<<1].sum+tree[i<<1].add*(tree[i<<1].right-tree[i<<1].left+1);
    tree[i].sum+=tree[i<<1|1].sum+tree[i<<1|1].add*(tree[i<<1|1].right-tree[i<<1|1].left+1);


}
__int64  s;
void  query(__int64  l,__int64 r,__int64 i,__int64 k )
{

	if(tree[i].left==l&&tree[i].right==r)
	{
		s+=(tree[i].sum+(tree[i].add+k)*(tree[i].right-tree[i].left+1));
		return;
	}
	if(tree[i].left==tree[i].right) return ;

	
	__int64         mid=(tree[i].left+tree[i].right)/2;
	 if(r<=mid)     query(l,r,i<<1,k+tree[i].add);
	 else if(l>mid) query (l,r,i<<1|1,k+tree[i].add);
	 else
	 {
 		              query(l,mid,i<<1,k+tree[i].add);
 		              query(mid+1,r,i<<1|1,k+tree[i].add);
 	}
  
 }
int main()
{
  __int64  N,Q;
  while(~scanf("%I64d%I64d",&N,&Q))
  {
  __int64 i;
  	for( i=1;i<=N;++i)
	  scanf("%I64d",&a[i]);
    build (1,N,1);
    while(Q--)
    {
    	char str[45];
    	scanf("%s",str);
    	if(str[0]=='Q')
    	{
	    	__int64  st,en;
	    	scanf("%I64d%I64d",&st,&en);
	    	s=0;
	    	query(st,en,1,0);
      	printf("%I64d\n",s);
	    }
	    else
	    {
         __int64  q,w,e;
    		scanf("%I64d%I64d%I64d",&q,&w,&e);
    		insert(q,w,1,e);
    	}
    }
  }
}





Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator