题目链接:https://codeforces.com/contest/1439/problem/C
题目大意:
给定一个单调不升的序列
操作一:输入 \(x,y\), 将 \([1,x]\) 内的所有元素变成 \(max[a_i,y]\)
操作二:输入 \(x,y\), 从 \(a_x\) 开始一直到 \(a_n\),如果 \(y\) 大于 \(a_i\),则减去 \(a_i\), 问一共减了几次
题解:
注意到序列元素一直是单调不升的
第一个操作,找到左边最远的比 \(y\) 小的位置,然后区间覆盖即可
第二个操作,在线段树上二分,找到比剩余钱数少的位置,具体实现参考代码
时间复杂度 \(O(nlogn)\)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200010;
int n, q;
int a[maxn];
struct Node{
int mi, tag;
ll sum;
}t[maxn << 2];
inline void pushup(int i){
t[i].sum = t[i << 1].sum + t[i << 1 | 1].sum;
t[i].mi = min(t[i << 1].mi, t[i << 1 | 1].mi);
}
inline void build(int i, int l, int r){
if(l == r) {
t[i].mi = t[i].sum = a[l];
return;
}
int mid = (l + r) >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
pushup(i);
}
inline void pushdown(int i, int l, int r){
if(t[i].tag){
t[i << 1].mi = t[i << 1 | 1].mi = t[i].tag;
int mid = (l + r) >> 1;
t[i << 1].sum = 1ll * (mid - l + 1) * t[i].tag;
t[i << 1 | 1].sum = 1ll * (r - mid) * t[i].tag;
t[i << 1].tag = t[i << 1 | 1].tag = t[i].tag;
t[i].tag = 0;
}
}
inline void modify(int i, int k, int l, int r, int x, int y){
if(x <= l && r <= y){
t[i].mi = k;
t[i].sum = 1ll * (r - l + 1) * k;
t[i].tag = k;
return;
}
pushdown(i, l, r);
int mid = (l + r) >> 1;
if(x <= mid) modify(i << 1, k, l, mid, x, y);
if(y > mid) modify(i << 1 | 1, k, mid + 1, r, x, y);
pushup(i);
}
inline ll query_sum(int i, int l, int r, int x, int y){
if(x <= l && r <= y){
return t[i].sum;
}
pushdown(i, l, r);
int mid = (l + r) >> 1;
ll res = 0;
if(x <= mid) res += query_sum(i << 1, l, mid, x, y);
if(y > mid) res += query_sum(i << 1 | 1, mid + 1, r, x, y);
return res;
}
inline int find(int i, int l, int r, int k, int p){
if(l == r){
return l;
}
pushdown(i, l, r);
int mid = (l + r) >> 1;
if(t[i << 1].mi < k) return find(i << 1, l, mid, k, p);
else if(p >= mid + 1) return find(i << 1 | 1, mid + 1, r, k, p);
else return -1;
}
int solve(int i, ll &k, int l, int r, int p){
if(t[i].sum <= k && p <= l){
k -= t[i].sum;
return r - l + 1;
}
if(l == r) return 0;
pushdown(i, l, r);
int mid = (l + r) >> 1;
int res = 0;
if(t[i << 1].mi <= k && p <= mid) res += solve(i << 1, k, l, mid, p);
if(t[i << 1 | 1].mi <= k) res += solve(i << 1 | 1, k, mid + 1, r, p);
return res;
}
ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
int main(){
n = read(), q = read();
for(int i = 1 ; i <= n ; ++i) a[i] = read();
build(1, 1, n);
int t, x; ll y;
for(int i = 1 ; i <= q ; ++i){
t = read(), x = read(), y = read();
if(t == 1){
int pos = find(1, 1, n, y, x);
if(pos == -1) continue;
modify(1, y, 1, n, pos, x);
} else{
printf("%d\n", solve(1, y, 1, n, x));
}
}
return 0;
}