zoukankan      html  css  js  c++  java
  • NOIP 模拟 $89; m 混凝土粉末$

    题解 (by;zjvarphi)

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

    对于一个修改,将它转换为在 (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
    ",ans[i]);
            return 0;
        }
    }
    int main() {return nanfeng::main();}
    
  • 相关阅读:
    逻辑架构设计目标和任务
    业务架构设计
    架构设计概念
    可扩展设计:如何做到增加功能不修改调用方代码?
    P2661 信息传递
    Network of Schools POJ
    1002 过河卒
    P3254 圆桌问题
    P2765 魔术球问题
    P1141 01迷宫
  • 原文地址:https://www.cnblogs.com/nanfeng-blog/p/15511454.html
Copyright © 2011-2022 走看看