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 ;
}