Jewel HDU - 3727

http://acm.hdu.edu.cn/showproblem.php?pid=3727

乍一看有insert 以为是伸展树 还要求第k大 直接懵逼..

但是并没有查询 所以序列顺序是不变的 直接离线 把所有insert的值都记下来 建立主席树 然后1和3是静态区间第k大 2是问某个数第几大

#include <bits/stdc++.h>
using namespace std;
#define ll long long

struct node0
{
    int tp;
    int x;
    int k;
    int l;
    int r;
};

struct node1
{
    int l;
    int r;
    int val;
};

node0 order[500010];
node1 tree[4000010];
int ary[100010],tmp[100010],root[100010];
int n,q,num;

int build(int l,int r)
{
    int cur,m;
    cur=num++;
    tree[cur].l=0,tree[cur].r=0,tree[cur].val=0;
    if(l==r) return cur;
    m=(l+r)/2;
    tree[cur].l=build(l,m);
    tree[cur].r=build(m+1,r);
    return cur;
}

int update(int rot,int tar,int val,int l,int r)
{
    int cur,m;
    cur=num++;
    tree[cur]=tree[rot];
    tree[cur].val+=val;
    if(l==r) return cur;
    m=(l+r)/2;
    if(tar<=m) tree[cur].l=update(tree[rot].l,tar,val,l,m);
    else tree[cur].r=update(tree[rot].r,tar,val,m+1,r);
    return cur;
}

int queryI(int lrot,int rrot,int k,int l,int r)
{
    int val,m;
    if(l==r) return l;
    val=tree[tree[rrot].l].val-tree[tree[lrot].l].val,m=(l+r)/2;
    if(k<=val) return queryI(tree[lrot].l,tree[rrot].l,k,l,m);
    else return queryI(tree[lrot].r,tree[rrot].r,k-val,m+1,r);
}

int queryII(int rot,int pl,int pr,int l,int r)
{
    int res,m;
    if(pl<=l&&r<=pr) return tree[rot].val;
    res=0,m=(l+r)/2;
    if(pl<=m) res+=queryII(tree[rot].l,pl,pr,l,m);
    if(pr>m) res+=queryII(tree[rot].r,pl,pr,m+1,r);
    return res;
}

int main()
{
    ll ans1,ans2,ans3;
    int cas,i,cur,p,res;
    char op[10];
    cas=1;
    while(scanf("%d",&q)!=EOF)
    {
        n=0;
        for(i=1;i<=q;i++)
        {
            scanf("%s",op);
            if(strcmp(op,"Insert")==0)
            {
                order[i].tp=0;
                scanf("%d",&order[i].x);
                ary[++n]=order[i].x;
                tmp[n]=ary[n];
            }
            else if(strcmp(op,"Query_1")==0)
            {
                order[i].tp=1;
                scanf("%d%d%d",&order[i].l,&order[i].r,&order[i].k);
            }
            else if(strcmp(op,"Query_2")==0)
            {
                order[i].tp=2;
                scanf("%d",&order[i].x);
            }
            else
            {
                order[i].tp=3;
                scanf("%d",&order[i].k);
            }
        }
        sort(tmp+1,tmp+n+1);
        num=0;
        root[0]=build(1,n);
        for(i=1;i<=n;i++)
        {
            p=lower_bound(tmp+1,tmp+n+1,ary[i])-tmp;
            root[i]=update(root[i-1],p,1,1,n);
        }
        ans1=0,ans2=0,ans3=0,cur=0;
        for(i=1;i<=q;i++)
        {
            if(order[i].tp==0) cur++;
            else if(order[i].tp==1)
            {
                p=queryI(root[order[i].l-1],root[order[i].r],order[i].k,1,n);
                ans1+=(ll)(tmp[p]);
            }
            else if(order[i].tp==2)
            {
                p=lower_bound(tmp+1,tmp+n+1,order[i].x)-tmp;
                res=queryII(root[cur],1,p,1,n);
                ans2+=(ll)(res);
            }
            else
            {
                p=queryI(root[0],root[cur],order[i].k,1,n);
                ans3+=(ll)(tmp[p]);
            }
        }
        printf("Case %d:\n",cas++);
        printf("%lld\n%lld\n%lld\n",ans1,ans2,ans3);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/81952913
hdu
今日推荐