洛谷题解——P10516

P10516 数据结构 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <algorithm>
using namespace std ;

#define lc p<<1 
#define rc p<<1|1 
typedef long long LL ;
const int N = 1e5+10 ;

int n , m ;
LL a[N] , b[N] ;
LL muln[4*N] , sum[4*N] ;
struct node {
	int l , r ;
};
node tr[4*N] ;

void pushup(int p)
{
	sum[p] = sum[lc] + sum[rc] ;
	muln[p] = min (muln[lc],muln[rc]) ;
	return ;
}

void build (int p,int l,int r)
{
	tr[p].l = l , tr[p].r = r ;
	sum[p] = a[l] + b[l] , muln[p] = a[l] * b[l] ;
	if (l == r)
	{
		sum[p] = a[l] + b[l] ;
		muln[p] = a[l] * b[l] ;
		return ;
	}
	int mid = l + r >> 1 ;
	build (lc,l,mid) ;
	build (rc,mid+1,r) ;
	pushup (p) ;
}

void update1 (int p,int x,int y,int k,int t)
{
	if (muln[p] > k) 
		return ;
	if (tr[p].l == tr[p].r)
	{
		sum[p] += 2*t ;
		a[tr[p].l] += t , b[tr[p].l] += t ;
		muln[p] = a[tr[p].l] * b[tr[p].l] ;
		return ;
	}
	int mid = tr[p].l + tr[p].r >> 1 ;
	if (x <= mid) update1 (lc,x,y,k,t) ;
	if (y > mid) update1 (rc,x,y,k,t) ;
	pushup (p) ;
}

void update2 (int p,int t,int x,int y)
{
	if (tr[p].l == tr[p].r&&tr[p].l == t)
	{
		a[t] = x , b[t] = y ;
		sum[p] = a[t] + b[t] ;
		muln[p] = a[t]*b[t] ;
		return ;
	}
	int mid = tr[p].l + tr[p].r >> 1 ;
	if (t <= mid) update2(lc,t,x,y) ;
	if (t > mid) update2 (rc,t,x,y) ;
	pushup (p) ;
} 

LL query (int p,int x,int y)
{
	if (x <= tr[p].l && tr[p].r <= y)
		return sum[p] ;
	int mid = tr[p].l + tr[p].r >> 1 ;
	LL ans = 0 ;
	if (x <= mid) ans += query (lc,x,y) ;
	if (y > mid) ans += query(rc,x,y) ;
	return ans ;
}

int main ()
{
	scanf ("%d%d",&n,&m) ;
	for (int i = 1;i <= n;i++)
		scanf ("%lld",&a[i]) ;
	for (int i = 1;i <= n;i++)
		scanf ("%lld",&b[i]) ;
	build (1,1,n) ;
	while (m--)
	{
		int op ; 
		scanf ("%d",&op) ;
		if (op == 1)
		{
			int l , r ;
			LL k , t ;
			scanf ("%d%d%lld%lld",&l,&r,&k,&t) ;
			if (t)
				update1(1,l,r,k,t) ;
		}
		else if (op == 2)
		{
			int t ;
			LL x , y ;
			scanf ("%d%lld%lld",&t,&x,&y) ;
			update2(1,t,x,y) ;
		}
		else 
		{
			int x , y ;
			scanf ("%d%d",&x,&y) ;
			LL res = query (1,x,y) ;
			cout << res << endl ;
		}
	}
	return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值