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

wa sos

Posted by majia110 at 2009-08-13 10:41:05 on Problem 3468
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	int left,right,mid;
	int lch,rich,count;
}root[100010];
int top;
void marktree(int cnt,int a,int b){
	root[cnt].left=a;
	root[cnt].right=b;
	root[cnt].count=0;
	if(a+1==b)return ;
	int mid=root[cnt].mid=(a+b)>>1;
	root[cnt].lch=++top;
	marktree(top,a,mid);
	root[cnt].rich=++top;
	marktree(top,mid,b);
}
inline void markchild(int cnt){
	if(root[cnt].count==-20000)return ;
	int le=root[cnt].lch,ri=root[cnt].rich;
	root[le].count=root[ri].count=root[cnt].count;
	root[cnt].count=-20000;
}
inline void markfather(int cnt){
	int le=root[cnt].lch,ri=root[cnt].rich;
	if(root[le].count==root[ri].count)root[cnt].count=root[le].count;
}
void inseart(int s,int cnt,int a,int b){
	if(a==root[cnt].left&&b==root[cnt].right)
	{
		if(root[cnt].count!=-20000)root[cnt].count+=s;
		else
		{
			inseart(s,root[cnt].lch,a,root[cnt].mid);
			inseart(s,root[cnt].rich,root[cnt].mid,b);
		}
		return ;
	}
	markchild(cnt);
	if(b<=root[cnt].mid)inseart(s,root[cnt].lch,a,b);
	else if(a>=root[cnt].mid)inseart(s,root[cnt].rich,a,b);
	else
	{
		inseart(s,root[cnt].lch,a,root[cnt].mid);
		inseart(s,root[cnt].rich,root[cnt].mid,b);
	}
	markfather(cnt);
}
long long sum;
void dfs(int cnt,int a,int b){
	if(a==root[cnt].left&&b==root[cnt].right)
	{
		if(root[cnt].count!=-20000)sum+=(long long)(b-a)*(long long)root[cnt].count;
		else
		{
			dfs(root[cnt].lch,a,root[cnt].mid);
			dfs(root[cnt].rich,root[cnt].mid,b);
		}
		return ;
	}
	if(root[cnt].left+1==root[cnt].right)return ;
	if(b<=root[cnt].mid)dfs(root[cnt].lch,a,b);
	else if(a>=root[cnt].mid)dfs(root[cnt].rich,a,b);
	else
	{
		dfs(root[cnt].lch,a,root[cnt].mid);
		dfs(root[cnt].rich,root[cnt].mid,b);
	}
}
long long c[100010],a[100010];
inline void init(int n){
	int i,x;
	memset(c,0,sizeof(c));
	for(i=1;i<=n;++i)
	{
		c[i]+=a[i];
		for(x=i,x+=(x&(x^(x-1)));x<=n;x+=(x&(x^(x-1))))
			c[x]+=a[i];
	}
}
inline long long Sum(int a,int b){
	long long s1,s2;
	int x;
	for(x=a,s1=0;x>0;x-=(x&(x^(x-1))))
		s1+=c[x];
	for(x=b,s2=0;x>0;x-=(x&(x^(x-1))))
		s2+=c[x];
	return s2-s1;
}
int main()
{
	int n,q,i,x,y,c;
	long long sumx;
	char str[5];
	while(scanf("%d%d",&n,&q)!=EOF)
	{
		for(i=1;i<=n;++i)
			scanf("%lld",a+i);
		init(n);
		top=0;
		marktree(0,0,n+1);
		for(i=0;i<q;++i)
		{
			scanf("%s",str);
			if(str[0]=='Q')
			{
				scanf("%d%d",&x,&y);
				sumx=Sum(x-1,y);
				sum=0;
				dfs(0,x,y+1);
				sumx+=sum;
				printf("%lld\n",sumx);
			}
			else
			{
				scanf("%d%d%d",&x,&y,&c);
				inseart(c,0,x,y+1);
			}
		}
	}
	return 0;
}

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