• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏吧

# E. Greedy Shopping 线段树

3天前 11次浏览

``````//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
struct Node
{
int l,r;
LL mx,mi,lazy; LL sum;
}tr[N<<2];

void pushup(int u)
{
tr[u].mx=max(tr[L].mx,tr[R].mx);
tr[u].mi=min(tr[L].mi,tr[R].mi);
tr[u].sum=tr[L].sum+tr[R].sum;
}

void pushdown(int u)
{
if(tr[u].lazy==-1) return;
tr[L].lazy=tr[R].lazy=tr[u].lazy;
tr[L].mx=tr[L].mi=tr[R].mx=tr[R].mi=tr[u].lazy;
tr[L].sum=tr[u].lazy*Len(L);
tr[R].sum=tr[u].lazy*Len(R);
tr[u].lazy=-1;
}

void build(int u,int l,int r)
{
tr[u]={l,r};
tr[u].lazy=-1;
if(l==r)
{
LL x; scanf("%lld",&x);
tr[u].mx=tr[u].mi=tr[u].sum=x;
return;
}
build(L,l,Mid); build(R,Mid+1,r);
pushup(u);
}

void modify(int u,int l,int r,LL x)
{
if(tr[u].mi>=x) return;
if(tr[u].l>=l&&tr[u].r<=r&&tr[u].mx<=x)
{
tr[u].sum=1ll*Len(u)*x;
tr[u].mx=tr[u].mi=tr[u].lazy=x;
return;
}
pushdown(u);
int mid=l+r>>1;
if(l<=Mid) modify(L,l,r,x);
if(r>Mid) modify(R,l,r,x);
pushup(u);
}

int query(int u,int l,int r,LL &x)
{
if(tr[u].mi>x) return 0;
if(tr[u].l>=l&&tr[u].r<=r&&tr[u].sum<=x)
{
x-=tr[u].sum;
return Len(u);
}
pushdown(u);
int ans=0;
if(l<=Mid) ans+=query(L,l,r,x);
if(r>Mid) ans+=query(R,l,r,x);
pushup(u);
return ans;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
int op,l; LL r; scanf("%d%d%lld",&op,&l,&r);
if(op==1) modify(1,1,l,r);
else printf("%dn",query(1,l,n,r));
}
d

return 0;
}
/*

*/

``````