您的位置:首页 > 其它

BZOJ2773

2015-08-25 20:50 344 查看
传送门:BZOJ2773

以y为key建树,维护最大值,树上二分即可。

吐槽一下学校评测机,我艹我在bzoj上AC的代码T完了。

代码上的小细节见下。

[code]#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
using namespace std;

struct People{
    int A,B;
    int ord;
    bool operator <(const People& a)const{
        return B<a.B||(B==a.B&&A<a.A);
    }
    People(){
        A=B=0;
        ord=0;
    }
    People(int A_,int B_,int ord_){
        A=A_;
        B=B_;
        ord=ord_;
    }
};

struct People2{
    int A,B;
    int ord;
    bool operator <(const People2& a)const{
        return A<a.A||(A==a.A&&B<a.B);
    }
    People2(){
        A=B=0;
        ord=0;
    }
    People2(int A_,int B_,int ord_){
        A=A_;
        B=B_;
        ord=ord_;
    }
};

struct Node{
    int ls,rs;
    int w;
    set<People2> q;
};

Node tree[400005];
set<People>::iterator p;
char what[200005];
int a[200005],b[200005];
int tot,cnt;
int ask[200005];
int n;
int zc[200005];
set<People> S[200005];
map<int,int> hash;
int root,num;

int Query(int root,int l,int r,int A,int B)
{
    if(r<=hash[B]||tree[root].w<A)
        return -1;
    if(l==r)
        return (*(tree[root].q.lower_bound(People2(A,B,1)))).ord;
    int mid=(l+r)/2;
    int ans=Query(tree[root].ls,l,mid,A,B);
    if(ans==-1)
        ans=Query(tree[root].rs,mid+1,r,A,B);
    return ans;
}

void Update(int root,int l,int r,int A,int B,int num,int where)
{
    if(l==r){
        tree[root].w=max(tree[root].w,A);
        tree[root].q.insert(People2(A,B,num));
        return;
    }
    int mid=(l+r)/2;
    if(where<=mid)
        Update(tree[root].ls,l,mid,A,B,num,where);
    else
        Update(tree[root].rs,mid+1,r,A,B,num,where);
    tree[root].w=max(tree[tree[root].ls].w,tree[tree[root].rs].w);
}

void Readdata()
{
    scanf("%d\n",&n);
    for(int i=1;i<=n;i++){
        scanf("%c",&what[i]);
        if(what[i]=='D'){
            tot++;
            scanf("%d%d\n",&a[tot],&b[tot]);
        }
        else{
            cnt++;
            scanf("%d\n",&ask[cnt]);
        }
    }
}

void First()
{
    for(int i=1;i<=tot;i++)
        zc[i]=b[i];
    sort(zc+1,zc+1+tot);
    for(int i=1;i<=tot;i++)
        hash[zc[i]]=i;
}

void Solve()
{
    tot=cnt=0;
    for(int i=1;i<=n;i++)
        if(what[i]=='D'){
            tot++;
            Update(root,1,200000,a[tot],b[tot],tot,hash[b[tot]]);
            S[hash[b[tot]]].insert(People(a[tot],b[tot],tot));
        }
        else{
            cnt++;
            p=S[hash[b[ask[cnt]]]].lower_bound(People(a[ask[cnt]],b[ask[cnt]],1));
            if(++p!=S[hash[b[ask[cnt]]]].end())
                printf("%d\n",(*p).ord);
            else{
                int ans=Query(root,1,200000,a[ask[cnt]],b[ask[cnt]]);
                if(ans==-1)
                    printf("NE\n");
                else
                    printf("%d\n",ans);
            }
        }
}

void MakeTree(int& root,int l,int r)
{
    root=++num;
    if(l==r)
        return;
    int mid=(l+r)/2;
    MakeTree(tree[root].ls,l,mid);
    MakeTree(tree[root].rs,mid+1,r);
}

void Close()
{
    fclose(stdin);
    fclose(stdout);
}

int main()
{
    Readdata();
    MakeTree(root,1,200000);
    First();
    Solve();
    Close();
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: