当前位置:网站首页>Codeforces round 684 (Div. 2) e - green shopping (line segment tree)

Codeforces round 684 (Div. 2) e - green shopping (line segment tree)

2022-07-05 08:52:00 Qizi K

I have time to fill in my thoughts or something qwq

#include <bits/stdc++.h>
#define rep(n) for(int i = 1; i <= (n); ++i)
#define ll long long
using namespace std;

const int N = 400010;

int n,m;
ll w[N];

struct node{
    
	int l, r;
	ll maxx, minn, sum, lazy;
}tr[4 * N];

void pushup(int u){
    
	tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
	tr[u].maxx = max(tr[u << 1].maxx, tr[u << 1 | 1].maxx);
	tr[u].minn = min(tr[u << 1].minn, tr[u << 1 | 1].minn);
}

void pushdown(int u){
    
	if(tr[u].lazy){
    
		tr[u << 1].sum = tr[u].lazy * (tr[u << 1].r - tr[u << 1].l + 1);
		tr[u << 1 | 1].sum = tr[u].lazy * (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1);
		tr[u << 1].lazy = tr[u << 1].minn = tr[u << 1].maxx = tr[u].lazy;
		tr[u << 1 | 1].lazy = tr[u << 1 | 1].minn = tr[u << 1 | 1].maxx = tr[u].lazy;
		tr[u].lazy = 0;
	}
}

void build(int u, int l, int r){
    
	if(l == r)	tr[u] = {
    l,r,w[l],w[l],w[l],0};
	else{
    
		tr[u].l = l, tr[u].r = r;
		int mid = l + r >> 1;
		build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
		pushup(u);
	}
}

void modify(int u, int l, int r, ll val){
    
	if(tr[u].minn >= val)	return ;
	if(tr[u].l >= l && tr[u].r <= r && tr[u].maxx <= val){
    
		tr[u].sum = val * (tr[u].r - tr[u].l + 1);
		tr[u].maxx = tr[u].minn = tr[u].lazy = val;
	}else{
    
		pushdown(u);
		int mid = tr[u].l + tr[u].r >> 1;
		if(l <= mid)	modify(u << 1, l, r, val);
		if(r > mid)		modify(u << 1 | 1, l, r, val);
		pushup(u);
	}
}

ll query(int u, int l, int r, ll& val){
    
	if(tr[u].minn > val)	return 0;
	if(tr[u].l >= l && tr[u].r <= r && tr[u].sum <= val){
    
		val -= tr[u].sum;
		return tr[u].r - tr[u].l + 1;
	}else{
    
		pushdown(u);
		ll res = 0;
		int mid = tr[u].l + tr[u].r >> 1;
		if(l <= mid)	res += query(u << 1, l, r, val);
		if(r > mid)		res += query(u << 1 | 1, l, r, val);
		return res;
	}
}

int main(){
    
	cin >> n >> m;
	rep(n)	scanf("%lld", &w[i]);
	build(1,1,n);
	rep(m){
    
		int op, x;
		ll y;
		scanf("%d%d%lld",&op,&x,&y);
		if(op == 1)	modify(1,1,x,y);
		else	printf("%lld\n", query(1,x,n,y));
	}
	
	return 0;
}
原网站

版权声明
本文为[Qizi K]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140539523490.html