# 【loj6029】「雅礼集训 2017 Day1」市场 线段树+均摊分析

$n\le 100000$ ，每次加的数在 $[-10^4,10^4]$ 之间，每次除的数在 $[2,10^9]$ 之间。

【uoj#228】基础数据结构练习题 类似的均摊分析题。

#include <cstdio>
#include <algorithm>
#define N 400010
#define lson l , mid , x << 1
#define rson mid + 1 , r , x << 1 | 1
using namespace std;
typedef long long ll;
ll sum[N] , mx[N] , mn[N] , add[N];
inline ll fdiv(ll x , ll y)
{
return x > 0 ? x / y : (x - y + 1) / y;
}
inline void pushup(int x)
{
sum[x] = sum[x << 1] + sum[x << 1 | 1];
mx[x] = max(mx[x << 1] , mx[x << 1 | 1]);
mn[x] = min(mn[x << 1] , mn[x << 1 | 1]);
}
inline void pushdown(int l , int r , int x)
{
{
int mid = (l + r) >> 1;
sum[x << 1] += add[x] * (mid - l + 1) , sum[x << 1 | 1] += add[x] * (r - mid);
mx[x << 1] += add[x] , mx[x << 1 | 1] += add[x];
mn[x << 1] += add[x] , mn[x << 1 | 1] += add[x];
}
}
void build(int l , int r , int x)
{
if(l == r)
{
scanf("%lld" , &sum[x]) , mx[x] = mn[x] = sum[x];
return;
}
int mid = (l + r) >> 1;
build(lson) , build(rson);
pushup(x);
}
void vadd(int b , int e , ll a , int l , int r , int x)
{
if(b <= l && r <= e)
{
sum[x] += a * (r - l + 1) , mx[x] += a , mn[x] += a , add[x] += a;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) vadd(b , e , a , lson);
if(e > mid) vadd(b , e , a , rson);
pushup(x);
}
void vdiv(int b , int e , ll d , int l , int r , int x)
{
if(b <= l && r <= e && mx[x] - fdiv(mx[x] , d) == mn[x] - fdiv(mn[x] , d))
{
ll a = mx[x] - fdiv(mx[x] , d);
sum[x] -= a * (r - l + 1) , mx[x] -= a , mn[x] -= a , add[x] -= a;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) vdiv(b , e , d , lson);
if(e > mid) vdiv(b , e , d , rson);
pushup(x);
}
ll qmin(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e) return mn[x];
pushdown(l , r , x);
int mid = (l + r) >> 1;
ll ans = 1ll << 62;
if(b <= mid) ans = min(ans , qmin(b , e , lson));
if(e > mid) ans = min(ans , qmin(b , e , rson));
return ans;
}
ll qsum(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e) return sum[x];
pushdown(l , r , x);
int mid = (l + r) >> 1;
ll ans = 0;
if(b <= mid) ans += qsum(b , e , lson);
if(e > mid) ans += qsum(b , e , rson);
return ans;
}
int main()
{
int n , m , opt , x , y;
ll z;
scanf("%d%d" , &n , &m);
build(1 , n , 1);
while(m -- )
{
scanf("%d%d%d" , &opt , &x , &y) , x ++ , y ++ ;
if(opt == 1) scanf("%lld" , &z) , vadd(x , y , z , 1 , n , 1);
if(opt == 2) scanf("%lld" , &z) , vdiv(x , y , z , 1 , n , 1);
if(opt == 3) printf("%lld\n" , qmin(x , y , 1 , n , 1));
if(opt == 4) printf("%lld\n" , qsum(x , y , 1 , n , 1));
}
return 0;
}


posted @ 2018-04-04 09:40 GXZlegend 阅读(...) 评论(...) 编辑 收藏