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

必须ac

Posted by 080528lj at 2021-08-26 16:09:37 on Problem 3468 and last updated at 2021-08-26 16:10:03
就是一道树状数组
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

const int N=100010;
int a[N];
int n,m;
typedef long long LL;
LL c[2][N];
LL s[N];

int lowbit(int x)
{
	return x&-x;
}

LL sum(int k,int x)
{
	LL ans=0;
	for(int i=x;i;i-=lowbit(i)) ans+=c[k][i];
	return ans;
}

void add(int k,int x,int y)
{
	for(int i=x;i<=n;i+=lowbit(i))
	{
		c[k][i]+=y;
	}
}

int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		s[i]=s[i-1]+a[i];
	}
	while(m--)
	{
		char op[2];
		int l,r,d;
		scanf("%s",op);
		scanf("%d%d",&l,&r);
		if(op[0]=='C')
		{
			scanf("%d",&d);
			add(0,l,d);
			add(0,r+1,-d);
			add(1,l,l*d);
			add(1,r+1,-(r+1)*d);
		}
		else
		{
			LL ans=s[r]+(r+1)*sum(0,r)-sum(1,r);
			ans-=s[l-1]+l*sum(0,l-1)-sum(1,l-1);
			cout<<ans<<endl;
		}
	}
	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