• 欢迎光临~

# 【loj6092】【雅礼集训 2017 Day1】市场（线段树）

[x-lfloorfrac{x}{d}rfloor=z-lfloorfrac{z}{d}rfloor ]

[x-lfloorfrac{x}{d}rfloor=y-lfloorfrac{y}{d}rfloor=z-lfloorfrac{z}{d}rfloor ]

(xleqslant y leqslant z),那么

[lfloorfrac{x}{d}rfloor leqslant lfloorfrac{y}{d}rfloor leqslant lfloorfrac{z}{d}rfloor\$\$且\$\$lfloorfrac{y}{d}rfloor-lfloorfrac{x}{d}rfloor leqslant y-x]

[x-lfloorfrac{x}{d}rfloor leqslant y-lfloorfrac{y}{d}rfloor ]

[y-lfloorfrac{y}{d}rfloor leqslant z-lfloorfrac{z}{d}rfloor ]

[x-lfloorfrac{x}{d}rfloor leqslant y-lfloorfrac{y}{d}rfloor leqslant z-lfloorfrac{z}{d}rfloor]

[x-lfloorfrac{x}{d}rfloor=z-lfloorfrac{z}{d}rfloor ]

[x-lfloorfrac{x}{d}rfloor = y-lfloorfrac{y}{d}rfloor =z-lfloorfrac{z}{d}rfloor]

[minn-lfloorfrac{minn}{d}rfloor=maxn-lfloorfrac{maxn}{d}rfloor ]

[a_l-lfloorfrac{a_l}{d}rfloor=a_{l+1}-lfloorfrac{a_{l+1}}{d}rfloor=...=a_r-lfloorfrac{a_r}{d}rfloor ]

``````void update2(int k,int l,int r,int ql,int qr,ll val)
{
int x=floor(1.0*minn[k]/val)-minn[k];
int y=floor(1.0*maxn[k]/val)-maxn[k];
if(ql<=l&&r<=qr&&x==y)
{
sum[k]+=x*(r-l+1);//区间加（减）
minn[k]+=x;
maxn[k]+=x;
lazy[k]+=x;
return;
}
int mid=(l+r)>>1;
down(k,l,r,mid);
if(ql<=mid)update2(k<<1,l,mid,ql,qr,val);
if(qr>mid)update2(k<<1|1,mid+1,r,ql,qr,val);
up(k);
}
``````

``````#include<bits/stdc++.h>

#define N 100010
#define ll long long
#define LNF 0x7fffffffffffffff

using namespace std;

int n,m,a[N];
ll sum[N<<2],minn[N<<2],maxn[N<<2],lazy[N<<2];

void downn(int k,int l,int r,ll val)
{
sum[k]+=val*(r-l+1);
minn[k]+=val;
maxn[k]+=val;
lazy[k]+=val;
}

void down(int k,int l,int r,int mid)
{
if(lazy[k])
{
downn(k<<1,l,mid,lazy[k]);
downn(k<<1|1,mid+1,r,lazy[k]);
lazy[k]=0;
}
}

void up(int k)
{
sum[k]=sum[k<<1]+sum[k<<1|1];
minn[k]=min(minn[k<<1],minn[k<<1|1]);
maxn[k]=max(maxn[k<<1],maxn[k<<1|1]);
}

void build(int k,int l,int r)
{
if(l==r)
{
scanf("%lld",&sum[k]);
maxn[k]=minn[k]=sum[k];
return;
}
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
up(k);
}

void update1(int k,int l,int r,int ql,int qr,ll val)
{
if(ql<=l&&r<=qr)
{
downn(k,l,r,val);
return;
}
int mid=(l+r)>>1;
down(k,l,r,mid);
if(ql<=mid)update1(k<<1,l,mid,ql,qr,val);
if(qr>mid)update1(k<<1|1,mid+1,r,ql,qr,val);
up(k);
}

void update2(int k,int l,int r,int ql,int qr,ll val)
{
if(ql<=l&&r<=qr&&floor(1.0*minn[k]/val)-minn[k]==floor(1.0*maxn[k]/val)-maxn[k])
{
ll tmp=floor(1.0*minn[k]/val)-minn[k];
downn(k,l,r,tmp);
return;
}
int mid=(l+r)>>1;
down(k,l,r,mid);
if(ql<=mid)update2(k<<1,l,mid,ql,qr,val);
if(qr>mid)update2(k<<1|1,mid+1,r,ql,qr,val);
up(k);
}

ll query1(int k,int l,int r,int ql,int qr)
{
if(ql<=l&&r<=qr)
return minn[k];
int mid=(l+r)>>1;ll ans=LNF;
down(k,l,r,mid);
if(ql<=mid)ans=min(ans,query1(k<<1,l,mid,ql,qr));
if(qr>mid)ans=min(ans,query1(k<<1|1,mid+1,r,ql,qr));
return ans;
}

ll query2(int k,int l,int r,int ql,int qr)
{
if(ql<=l&&r<=qr)
return sum[k];
int mid=(l+r)>>1;ll ans=0;
down(k,l,r,mid);
if(ql<=mid)ans+=query2(k<<1,l,mid,ql,qr);
if(qr>mid)ans+=query2(k<<1|1,mid+1,r,ql,qr);
return ans;
}

int main()
{
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
int opt,l,r;
scanf("%d%d%d",&opt,&l,&r);
l++,r++;
if(opt==1)
{
ll val;
scanf("%lld",&val);
update1(1,1,n,l,r,val);
}
if(opt==2)
{
ll val;
scanf("%lld",&val);
update2(1,1,n,l,r,val);
}
if(opt==3)
printf("%lldn",query1(1,1,n,l,r));
if(opt==4)
printf("%lldn",query2(1,1,n,l,r));
}
return 0;
}
``````