Loading

NOIP 模拟 $89\; \rm 混凝土粉末$

题解 \(by\;zj\varphi\)

将询问离线,用扫描线扫下标,然后在操作序列上建立权值树状数组。

对于一个修改,将它转换为在 \(l\) 加上 \(h\),在 \(r+1\) 加上 \(-h\),对于询问,直接在树状数组上二分出哪个最早的查询使得加和高度比询问高度大。

因为博主不会树状数组二分,所以改成了二分树状数组。

Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    #define debug1(x) std::cerr << #x"=" << x << ' '
    #define debug2(x) std::cerr << #x"=" << x << std::endl
    #define Debug(x) assert(x)
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            bool f=false;x=0;char ch=gc();
            while(!isdigit(ch)) f|=ch=='-',ch=gc();
            while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
            return x=f?-x:x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    using ll=long long;
    static const int N=1e6+7;
    int *qu[N],cntc[N],cntq[N],ans[N],n,q;
    struct Que{int opt,l,r,h,x;ll y;}que[N];
    struct Cha{int x,h;}*ch[N];
    struct BIT{
        #define lowbit(x) ((x)&-(x))
        ll c[N];
        func(void(int,int)) update=[&](int x,int k) {for (;x<=q;x+=lowbit(x)) c[x]+=k;};
        func(ll(int)) query=[&](int x) {
            ll res=0;
            for (;x;x-=lowbit(x)) res+=c[x];
            return res;
        };
    }B;
    inline int main() {
        FI=freopen("concrete.in","r",stdin);
        FO=freopen("concrete.out","w",stdout);
        cin >> n >> q;
        for (ri i(1);i<=q;pd(i)) {
            cin >> que[i].opt;
            if (que[i].opt==1) {
                cin >> que[i].l >> que[i].r >> que[i].h;
                ++cntc[que[i].l],++cntc[que[i].r+1];
            } else {
                cin >> que[i].x >> que[i].y;
                ++cntq[que[i].x];
            }
        }
        for (ri i(1);i<=n;pd(i)) {
            ch[i]=new Cha[cntc[i]+3];
            qu[i]=new int[cntq[i]+3];
            cntc[i]=cntq[i]=0;
        }
        for (ri i(1);i<=q;pd(i))
            if (que[i].opt==1) {
                ++cntc[que[i].l];
                if (que[i].r+1<=n) ++cntc[que[i].r+1];
                ch[que[i].l][cntc[que[i].l]]={i,que[i].h};
                if (que[i].r+1<=n) ch[que[i].r+1][cntc[que[i].r+1]]={i,-que[i].h};
            } else qu[que[i].x][++cntq[que[i].x]]=i;
        for (ri i(1);i<=n;pd(i)) {
            for (ri j(1);j<=cntc[i];pd(j)) B.update(ch[i][j].x,ch[i][j].h);
            for (ri j(1);j<=cntq[i];pd(j)) {
                ll am=que[qu[i][j]].y;
                int l=1,r=q,res=N;
                while(l<=r) {
                    int mid=(l+r)>>1;
                    if (B.query(mid)>=am) res=mid,r=mid-1;
                    else l=mid+1;
                }
                if (res<qu[i][j]) ans[qu[i][j]]=res;
            }
        }
        for (ri i(1);i<=q;pd(i)) if (que[i].opt==2) printf("%d\n",ans[i]);
        return 0;
    }
}
int main() {return nanfeng::main();}
posted @ 2021-11-05 06:32  ナンカエデ  阅读(82)  评论(0编辑  收藏  举报