P11217 题解

· · 题解

线段树上二分板子,不知道什么是线段树的看这。

区间加是线段树的基本操作了,现在考虑查询。

首先考虑 youyou 会被所有垃圾桶构成的整体攻击多少次。设所有垃圾桶攻击力在没有翻倍情况下的和为 sum,那么第一波对 youyou 造成 sum 点伤害,第二次波 youyou 造成 sum \times 2 点伤害 \cdots 直到第 k 波 youyou 的生命值不超过 2 ^ {k - 1} \times sum 时停止。这个过程是可以暴力的,每扣一次生命值战斗的评分加 n。

接下来考虑 youyou 还能承受前几个垃圾桶的攻击,一个很好想的方法是二分这个位置,每二分一个位置在线段树上查询前缀和判断是否可行,复杂度 O(\log ^2 n)。

不过由线段树的结构,我们可以直接在线段树上二分。

以下是线段树上二分的代码:

int query(int root,int l,int r,long long v,long long w){
    if(l == r)
        return (int)(v * t[root].val < w);
    push_down(root,l,r);
    if(v * t[lchild].val < w)
        return len(l,mid) + query(rchild,mid + 1,r,v,w - v * t[lchild].val);
    return query(lchild,l,mid,v,w);
}

其中 v 是当前垃圾桶攻击值翻的倍数,w 是当前剩余的生命值。

#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
inline char gc(){
    static char buf[10000005],*t1 = buf,*t2 = buf;
    return t1 == t2 && (t2 = (t1 = buf) + fread(buf,1,1000000,stdin),t1 == t2) ? EOF : *t1++;
}
int read1(){
    char c = gc();
    int x = 0;
    while(c < '0' || c > '9')
        c = gc();
    while(c >= '0' && c <= '9'){
        x = (x << 3) + (x << 1) + c - 48;
        c = gc();
    }
    return x;
}
long long read2(){
    char c = gc();
    long long x = 0;
    while(c < '0' || c > '9')
        c = gc();
    while(c >= '0' && c <= '9'){
        x = (x << 3) + (x << 1) + c - 48;
        c = gc();
    }
    return x;
}
const int N = 2e5 + 9;
int n,q;
long long W;
int a[N];

struct node{
    long long val;
    long long tag;//区间加标记 
} t[N << 2];

int len(int l,int r){
    return r - l + 1;
}

bool in_range(int l,int r,int L,int R){//[l,r]是否在修改/查询区间[L,R]中 
    return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){//[l,r]是否与修改/查询区间[L,R]无交集 
    return l > R || r < L;
}

void push_up(int root){
    t[root].val = t[lchild].val + t[rchild].val;
}

void make_tag(int root,int len,long long v){
    t[root].tag += v;
    t[root].val += 1ll * v * len;
}
void push_down(int root,int l,int r){
    make_tag(lchild,len(l,mid),t[root].tag);
    make_tag(rchild,len(mid + 1,r),t[root].tag);
    t[root].tag = 0;
}

void build(int root,int l,int r){
    if(l == r){
        t[root].val = a[l];
        return;
    }
    build(lchild,l,mid);
    build(rchild,mid + 1,r);
    push_up(root);
}

//将区间 [L,R] 内的所有数加 v 
void update(int root,int l,int r,int L,int R,int v){
    if(out_range(l,r,L,R))
        return;
    if(in_range(l,r,L,R)){
        make_tag(root,len(l,r),v);
        return;
    }
    push_down(root,l,r);
    update(lchild,l,mid,L,R,v);
    update(rchild,mid + 1,r,L,R,v);
    push_up(root);
}

int query(int root,int l,int r,long long v,long long w){
    if(l == r)
        return (int)(v * t[root].val < w);
    push_down(root,l,r);
    if(v * t[lchild].val < w)
        return len(l,mid) + query(rchild,mid + 1,r,v,w - v * t[lchild].val);
    return query(lchild,l,mid,v,w);
}

signed main(){
    ios::sync_with_stdio(false);
    cout.tie(0);
    n = read1();q = read1();W = read2();
    for(int i = 1;i <= n;i++)
        a[i] = read1();
    build(1,1,n);
    while(q--){
        int l = read1(),r = read1(),d = read1();
        update(1,1,n,l,r,d);
        long long tmp = W,i = t[1].val;
        long long ans = 0;
        while(tmp > i){
            tmp -= i;
            i <<= 1;
            ans += n;
        }
        ans += query(1,1,n,i / t[1].val,tmp);
        cout << ans << '\n';
    }
    return 0;
}