cf12D. Ball

链接

点击跳转

题解

按第一关键字排序,然后倒着扫,维护线段树,下标为第二关键字,值为第三关键字

代码

#include 
#include 
#include 
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct SegmentTree
{
    #define inf 0x3f3f3f3f
    int mn[maxn<<2], mx[maxn<<2], sum[maxn<<2], add[maxn<<2], set[maxn<<2], L[maxn<<2], R[maxn<<2];
    void maketag_set(int o, int v)
    {
        add[o]=0;
        set[o]=v;
        mx[o]=mn[o]=v;
        sum[o]=(R[o]-L[o]+1)*v;
    }
    void maketag_add(int o, int v)
    {
        add[o]+=v;
        mx[o]+=v, mn[o]+=v;
        sum[o]+=(R[o]-L[o]+1)*v;
    }
    void pushdown(int o)
    {
        if(L[o]==R[o])return;
        if(~set[o])
        {
            maketag_set(o<<1,set[o]);
            maketag_set(o<<1|1,set[o]);
            set[o]=-1;
        }
        if(add[o])
        {
            maketag_add(o<<1,add[o]);
            maketag_add(o<<1|1,add[o]);
            add[o]=0;
        }
    }
    void pushup(int o)
    {
        mx[o]=max(mx[o<<1],mx[o<<1|1]);
        mn[o]=min(mn[o<<1],mn[o<<1|1]);
        sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    void build(int o, int l, int r)
    {
        int mid(l+r>>1);
        L[o]=l, R[o]=r;
        add[o]=0;
        set[o]=-1;
        if(l==r)
        {
            mn[o]=mx[o]=sum[o]=0;
            return;
        }
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        pushup(o);
    }
    void segset(int o, int l, int r, int v)
    {
        int mid(L[o]+R[o]>>1);
        if(l<=L[o] and r>=R[o]){maketag_set(o,v);return;}
        pushdown(o);
        if(l<=mid)segset(o<<1,l,r,v);
        if(r>mid)segset(o<<1|1,l,r,v);
        pushup(o);
    }
    void segadd(int o, int l, int r, int v)
    {
        int mid(L[o]+R[o]>>1);
        if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
        pushdown(o);
        if(l<=mid)segadd(o<<1,l,r,v);
        if(r>mid)segadd(o<<1|1,l,r,v);
        pushup(o);
    }
    int segsum(int o, int l, int r)
    {
        pushdown(o);
        int mid(L[o]+R[o]>>1), ans(0);
        if(l<=L[o] and r>=R[o])return sum[o];
        if(l<=mid)ans+=segsum(o<<1,l,r);
        if(r>mid)ans+=segsum(o<<1|1,l,r);
        return ans;
    }
    int segmin(int o, int l, int r)
    {
        int mid(L[o]+R[o]>>1), ans(inf);
        if(l<=L[o] and r>=R[o])return mn[o];
        pushdown(o);
        if(l<=mid)ans=min(ans,segmin(o<<1,l,r));
        if(r>mid)ans=min(ans,segmin(o<<1|1,l,r));
        return ans;
    }
    int segmax(int o, int l, int r)
    {
        int mid(L[o]+R[o]>>1), ans(-inf);
        if(l<=L[o] and r>=R[o])return mx[o];
        pushdown(o);
        if(l<=mid)ans=max(ans,segmax(o<<1,l,r));
        if(r>mid)ans=max(ans,segmax(o<<1|1,l,r));
        return ans;
    }
    #undef inf
}segtree;
struct Lisan
{
    int tmp[maxn], tot;
    void clear(){tot=0;}
    void insert(int x){tmp[++tot]=x;}
    void run()
    {
        sort(tmp+1,tmp+tot+1);
        tot=unique(tmp+1,tmp+tot+1)-tmp-1;
    }
    void lisan(int *a, int len)
    {
        for(int i=1;i<=len;i++)a[i]=lower_bound(tmp+1,tmp+tot+1,a[i])-tmp;
    }
    int lisan(int x)
    {
        return lower_bound(tmp+1,tmp+tot+1,x)-tmp;
    }
}ls;
vector<ll> a[maxn];
int main()
{
    ll i, j, n, x, ans=0;
    n=read();
    rep(i,n)a[i].emb(read());
    rep(i,n)a[i].emb(read()), ls.insert(a[i].back());
    rep(i,n)a[i].emb(read());
    sort(a+1,a+n+1);
    ls.run();
    segtree.build(1,1,ls.tot);
    j=n;
    for(i=n;i;i--)
    {
        while(a[j].at(0) > a[i].at(0))
        {
            auto p=ls.lisan(a[j].at(1));
            ll x=segtree.segmax(1,p,p);
            segtree.segset(1,p,p,max(x,a[j].at(2)));
            j--;
        }
        if(ls.lisan(a[i].at(1))<ls.tot)
            ans += segtree.segmax(1,ls.lisan(a[i].at(1))+1,ls.tot) > a[i].at(2);
    }
    cout<<ans;
    return 0;
}

你可能感兴趣的:(#,线段树)