P11217 【MX-S4-T1】「yyOI R2」youyou 的垃圾桶 · znzryb
## 思路讲解
线段树上二分
[https://www.luogu.com.cn/article/2a6kvgjo](https://www.luogu.com.cn/article/2a6kvgjo)
参考这篇题解。
当然,我们先要枚举一下需要操作几轮。
```cpp
FOR(j,1,64){
if(w>sum){
w-=sum;
ans+=N;
}else{
break;
}
bei*=2;
sum*=2;
}
```
## AC代码
[https://www.luogu.com.cn/record/233323384](https://www.luogu.com.cn/record/233323384)
::: 源代码
```cpp
// by Gospel_rock
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = __int128;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<DB>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT,W;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>lT;
// while(lT--)
__();
return 0;
}
/*
*/
ll bei=1;
// 参考zkw的PPT 以及 https://www.luogu.com.cn/record/207546670
// 参考题解 https://www.luogu.com.cn/article/w3gfeebh
// AC https://www.luogu.com.cn/record/232827448 区间加
constexpr ll preset=0;
struct SEG{
static ll op(const ll &a,const ll &b){
return a+b;
}
int n,sz=1,dep;
vector<ll> tr;vector<ll> tag;
SEG(int n){ // 传入的时候可以传大一点
this->n=n+2;
dep=__lg(this->n)+1;
sz=1<<dep;
tr.resize((sz<<1)+5,preset);
tag.resize((sz<<1)+5,preset);
}
void pushup(int o){tr[o]=op(tr[o<<1],tr[o<<1|1]);}
void settag(int o,ll x){
tr[o]=op(tr[o],x);
tag[o]=op(tag[o],x);
}
void pushdown(int o){
int l=o<<1,r=l|1; // 左儿子,右儿子
settag(l, tag[o]/2);
settag(r, tag[o]/2);
tag[o]=0;
}
void assign(int i,ll x){
tr[i+sz]=x;
}
void build(){
for(int o=sz-1;o>=1;--o){
pushup(o);
}
}
void push(int l,int r){
// 在j层,l,r变为了同一点
int i=dep,j=__lg(l^r);
while (i>j) { // 就是为了避免一下重复下推
pushdown(l>>i--);
}
while(i){
pushdown(l>>i),pushdown(r>>i--);
}
}
ll query(int l,int r){
ll res=preset;
// 变为开区间,并加上sz
l=l-1+sz,r=r+1+sz;
push(l,r);
for(;l^r^1;l>>=1,r>>=1){
// l^r^1 表示 只要 l 与 r 还不是同一个父亲的两兄弟,就继续上跳。
if(~l&1){
// l是偶数,说明l是父节点的左儿子,开区间,将其右儿子加入答案
res=op(res,tr[l^1]);
}
if(r&1){
res=op(tr[r^1],res);
}
}
return res;
}
void add(int l,int r,ll x){
int w=1;
l=l-1+sz,r=r+1+sz;
push(l, r);
for(;l^r^1;pushup(l>>=1),pushup(r>>=1),w<<=1){
if(~l&1){
settag(l^1, w*x);
}
if(r&1){
settag(r^1, w*x);
}
}
for (l >>= 1; l; l >>= 1) pushup(l);
}
// AC https://qoj.ac/submission/1263556
// 找到最靠近r的不符合位置 fun return true 是不符合
template<class F> int findl(int r,F&&fun){
return findl(1,r,0,sz-1,0,fun);
}
// 找到最靠近r的不符合位置
template<class F> int findl(int o,int qr,int l,int r,ll sum,F&&fun){
if(l==r){
return l;
}
pushdown(o);
ll ls=o<<1,rs=ls|1;
ll mid=l+r>>1;
ll pos=-1;
if(fun(tr[rs]*bei+sum+tr[ls]*bei) && mid+1<=qr)
pos=findl(rs,qr,mid+1,r,sum+tr[ls]*bei,fun);
if(pos!=-1) return pos;
if(fun(tr[ls]*bei+sum))
pos=findl(ls,qr,l,mid,sum,fun);
return pos;
}
// 找最靠近l的不符合位置
template<class F> int findr(int l,F&&fun){
return findr(1,l,0,sz-1,0,fun);
}
// 找最靠近l的不符合位置
template<class F> int findr(int o,int ql,int l,int r,ll sum,F&&fun){
if(l==r){
return l;
}
pushdown(o);
ll ls=o<<1,rs=ls|1;
ll mid=l+r>>1;
ll pos=-1;
if(fun(tr[ls]*bei+sum) && mid>=ql) pos=findr(ls,ql,l,mid,sum,fun);
if(pos!=-1) return pos;
if(fun(tr[rs]*bei+sum+tr[ls]*bei)) pos=findr(rs,ql,mid+1,r,sum+tr[ls]*bei,fun);
return pos;
}
};
// 来自wida github仓库 https://github.com/hh2048/XCPC/tree/main 加了一些新功能
//46 ms https://acm.hdu.edu.cn/contest/view-code?cid=1179&rid=14912 读入字符getc(a) 的AC记录
// AC https://vjudge.net/solution/63197697 一道测试读写的codechef题目
namespace QuickRead { // 快读
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
template <typename T> void Cin(T &a) {
T ans = 0;
bool f = 0;
char c = getc();
for (; c < '0' || c > '9'; c = getc()) {
if (c == '-') f = 1;
}
for (; c >= '0' && c <= '9'; c = getc()) {
ans = ans * 10 + c - '0';
}
a = f ? -ans : ans;
}
void getc(char &a){
char c=getc();
while (isspace(c)) {
c=getc();
}
a=c;
}
template <typename T, typename... Args> void Cin(T &a, Args &...args) {
Cin(a), Cin(args...);
}
// ===== 输出 =====
static constexpr size_t OUT_CAP = 1 << 21; // 大概是2MB
char obuf[OUT_CAP];
size_t op = 0;
inline void flush() {
if (op) {
fwrite(obuf, 1, op, stdout);
op = 0;
}
}
inline void putc(char c) {
if (op == OUT_CAP) flush();
obuf[op++] = c;
}
template <typename T> void write(T x) { // 注意,这里输出不带换行
if (x < 0) putc('-'), x = -x;
if (x > 9) write(x / 10);
putc(x % 10 + '0');
}
struct Flusher { // 析构时自动刷新
~Flusher() { flush(); }
} flusher;
} // namespace QuickRead
using namespace QuickRead;
inline void __(){
Cin(N,Q,W);
SEG tr(N+1);
FOR(i,1,N){
ll a;Cin(a);
tr.assign(i,a);
}
// tr.assign(N+1, INF);
tr.build();
FOR(i,1,Q){
ll l,r,d;
Cin(l,r,d);
tr.add(l, r, d);
long long ans=0;
ll sum=tr.tr[1];
ll w=W;
bei=1;
FOR(j,1,64){
if(w>sum){
w-=sum;
ans+=N;
}else{
break;
}
bei*=2;
sum*=2;
}
#ifdef LOCAL
debug(ans);
debug( (long long)w );
debug((long long)tr.tr[1])
#endif
ans+=tr.findr(1,[&](ll x){
if(x>=w) return true;
return false;
})-1;
#ifdef LOCAL
debug(ans);
cend;
#endif
write(ans);putc('\n');
}
}
/*
AC
https://www.luogu.com.cn/record/233323384
*/
```
:::
## 心路历程(WA,TLE,MLE……)