(题目)[https://codeforces.com/contest/1440/problem/E]
题意:
给你一个非递减序列,有两种操作:
- 输入 x , y x,y x,y 将 a [ i ] = m a x ( a [ i ] , y ) a[i]=max(a[i],y) a[i]=max(a[i],y) , i ∈ [ 1 , x ] i∈[1,x] i∈[1,x] ;
- 输入 x , y x,y x,y , 这个人有y钱,买东西 从下标 i=x 开始 到 n,遇到能买的就买下,不能买的跳过去下一个。问 从 x 走到 n 可以买几个东西?
思路:
用线段树优化这些操作
- 优化操作2,我们统计一个区间和,当能买这个区间全部的时候,直接 就减去区间和,返回区间长度了.
- 优化操作2:维护一个区间最小值,当 手上的钱小于区间最小值的时候,就别进入找个区间了
- 优化操作1:维护一个区间最大值,当 y 大于等于的时候,修改
- 优化操作1:当 y 小于 区间最小值的时候 就不用修改了
还有一句废话就是:懒标记 是 区间修改 标记上的。 只有 y 大于等于 区间最大值才会修改。所以懒标记 down 的时候 是直接给 maxx minn 赋值就行。
代码:
#include<bits/stdc++.h>
#define ks ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ft first
#define sd second
#define pb push_back
#define pf push_front
#define mp make_pair
#define bk back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pa;
typedef set<int>::iterator sit;
typedef multiset<int>::iterator msit;
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
void wenjian(){freopen("concatenation.in","r",stdin);freopen("concatenation.out","w",stdout);}
ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
ll qpow(ll a,ll b,ll mod){a %= mod;ll ans = 1;while(b){if(b & 1)ans = ans * a % mod;a = a * a % mod;b >>= 1;}return ans;}
struct chongzai{int c; bool operator<(const chongzai &b )const{ return c>b.c; } }sss;
const int maxn=3e6+177;
const int maxm=1e6+177;
const ll mod=1e18+7;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll a[maxn];
ll num;
struct Tree{
int l;
int r;
ll sum;
ll maxx,minn;
ll add;
}t[maxn];
void up(int p){
t[p].maxx=max(t[p<<1].maxx,t[p<<1|1].maxx);
t[p].minn=min(t[p<<1].minn,t[p<<1|1].minn);
t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}
void build(int p,int l,int r){
t[p].l=l;
t[p].r=r;
t[p].add=0;
if(l==r){
t[p].sum=a[l];
t[p].maxx=a[l];
t[p].minn=a[l];
return ;
}
t[p].sum=0;
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
up(p);
}
void down(int p){
if(t[p].add){
t[p<<1].maxx=t[p].add;
t[p<<1].minn=t[p].add;
t[p<<1].sum=(t[p<<1].r-t[p<<1].l+1LL)*t[p].add;
t[p<<1].add=t[p].add;
t[p<<1|1].maxx=t[p].add;
t[p<<1|1].minn=t[p].add;
t[p<<1|1].sum=(t[p<<1|1].r-t[p<<1|1].l+1LL)*t[p].add;
t[p<<1|1].add=t[p].add;
t[p].add=0;
}
}
void change(int p,int l,int r,ll num){
if(t[p].minn>num){
return ;
}
if(l<=t[p].l&&t[p].r<=r&&t[p].maxx<=num){
t[p].maxx=num;
t[p].add=num;
t[p].minn=num;
t[p].sum=(t[p].r-t[p].l+1LL)*num;
return ;
}
down(p);
int mid=t[p].l+t[p].r>>1;
if(l<=mid){
change(p<<1,l,r,num);
}
if(r>mid){
change(p<<1|1,l,r,num);
}
up(p);
}
ll query(int p,int l,int r){
if(t[p].minn>num){
return 0;
}
if(l<=t[p].l&&t[p].r<=r&&t[p].sum<=num){
num-=t[p].sum;
return t[p].r-t[p].l+1;
}
down(p);
int mid=t[p].l+t[p].r>>1;
ll ans=0;
if(mid>=l){
ans+=query(p<<1,l,r);
}
if(r>mid){
ans+=query(p<<1|1,l,r);
}
return ans;
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
build(1,1,n);
while(q--){
int op;
int x;
ll y;
scanf("%d%d%lld",&op,&x,&y);
if(op==1){
change(1,1,x,y);
}else if(op==2){
num=y;
ll ans=query(1,x,n);
printf("%lld\n",ans);
}
}
}