hdu 3727 Jewel(主席树)

Jewel

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1805    Accepted Submission(s): 455


Problem Description
Jimmy wants to make a special necklace for his girlfriend. He bought many beads with various sizes, and no two beads are with the same size. Jimmy can't remember all the details about the beads, for the necklace is so long. So he turns to you for help.

Initially, there is no bead at all, that is, there is an empty chain. Jimmy always sticks the new bead to the right of the chain, to make the chain longer and longer. We number the leftmost bead as Position 1, and the bead to its right as Position 2, and so on. Jimmy usually asks questions about the beads' positions, size ranks and actual sizes. Specifically speaking, there are 4 kinds of operations you should process:

Insert x 
Put a bead with size x to the right of the chain (0 < x < 231, and x is different from all the sizes of beads currently in the chain)
Query_1 s t k 
Query the k-th smallest bead between position s and t, inclusive. You can assume 1 <= s <= t <= L, (L is the length of the current chain), and 1 <= k <= min (100, t-s+1)
Query_2 x
Query the rank of the bead with size x, if we sort all the current beads by ascent order of sizes. The result should between 1 and L (L is the length of the current chain)
Query_3 k
Query the size of the k-th smallest bead currently (1 <= k <= L, L is the length of the current chain)

 

Input
There are several test cases in the input. The first line for each test case is an integer N, indicating the number of operations. Then N lines follow, each line contains one operation, as described above. 

You can assume the amount of "Insert" operation is no more than 100000, and the amounts of "Query_1", "Query_2" and "Query_3" are all less than 35000.
There are several test cases in the input. The first line for each test case is an integer N, indicating the number of operations. Then N lines follow, each line contains one operation, as described above. 

You can assume the amount of "Insert" operation is no more than 100000, and the amounts of "Query_1", "Query_2" and "Query_3" are all less than 35000.Query the rank of the bead with size x, if we sort all the current beads by ascent order of sizes. The result should between 1 and L (L is the length of the current chain)
Query_3 k
Query the size of the k-th smallest bead currently (1 <= k <= L, L is the length of the current chain)

 

Output
Output 4 lines for each test case. The first line is "Case T:", where T is the id of the case. The next 3 lines indicate the sum of results for Query_1, Query_2 and Query_3, respectively. 

 

Sample Input
  
  
10 Insert 1 Insert 4 Insert 2 Insert 5 Insert 6 Query_1 1 5 5 Query_1 2 3 2 Query_2 4 Query_3 3 Query_3 1
 

Sample Output
  
  
Case 1: 10 3 5
Hint
The answers for the 5 queries are 6, 4, 3, 4, 1, respectively.

题意:有一个空链,现有如下几种操作:

Insert x 把数x插入链中(从右端插入)
Query_1 s t k 查询链上区间[s, t]内的第k小值
Query_2 x 查询x在链上是第几小
Query_3 k 查询整条链上的第k小值

数组没记得初始化,Mle到死

用树状数组记录

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
int rt[N], ls[N*20], rs[N*20], sum[N*20];
int  b[N], c[2*N];
LL ans2;
int tot, k;
void build(int &o,int l,int r)
{
    o= ++tot;
    sum[o]=0;
    if(l==r) return ;
    int mid=(l+r)/2;
    build(ls[o],l,mid);
    build(rs[o],mid+1,r);
    return ;
}
void update(int &o,int l,int r,int last,int p,int v)
{
    o= ++tot;
    ls[o]=ls[last],rs[o]=rs[last];
    sum[o]=sum[last]+v;
    if(l==r) return ;
    int mid=(l+r)/2;
    if(p<=mid) update(ls[o],l,mid,ls[last],p,v);
    else update(rs[o],mid+1,r,rs[last],p,v);
    return ;
}
int query(int ss,int tt,int l,int r,int cnt)
{
    if(l==r) return l;
    int tmp=(sum[ls[tt]]-sum[ls[ss]]);
    int mid=(l+r)/2;
    if(tmp>=cnt) return query(ls[ss],ls[tt],l,mid,cnt);
    else return query(rs[ss],rs[tt],mid+1,r,cnt-tmp);
}
void query1(int tt,int l,int r,int p)
{
    if(l==r)
    {
        ans2+=(LL)(sum[tt]);
        return ;
    }
    int mid=(l+r)/2;
    if(p<=mid) return query1(ls[tt],l,mid,p);
    else
    {
        ans2+=(LL)sum[ls[tt]];
        return query1(rs[tt],mid+1,r,p);
    }
}
char str[20];
void add(int x)
{
    while(x<=k)
    {
        c[x]+=1;
        x+=(x&-x);
    }
    return ;
}
void get(int x)
{
    while(x>0)
    {
        ans2+=c[x];
        x-=(x&-x);
    }
    return ;
}
struct node
{
    int l, r, v, mark;
} px[2*N];
int main()
{
    int n, ncase=1;
    while(scanf("%d", &n)!=EOF)
    {
        k=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%s",str);
            if(str[0]=='I')
            {
                px[i].mark=0, scanf("%d", &px[i].v),b[++k]=px[i].v;
            }
            else if(str[6]=='1')
            {
                px[i].mark=1,scanf("%d %d %d",&px[i].l,&px[i].r,&px[i].v);
            }
            else if(str[6]=='2')
            {
                px[i].mark=2,scanf("%d",&px[i].v);
            }
            else
            {
                px[i].mark=3,scanf("%d",&px[i].v);
            }
        }
        memset(c,0,sizeof(c));
        sort(b+1,b+k+1);
        tot=0;
        build(rt[0],1,k);
        LL ans1=0, ans3=0;
        int k1=1;
        ans2=0;
        for(int i=1; i<=n; i++)
        {
            if(px[i].mark==0)
            {
                int pos=lower_bound(b+1,b+k+1,px[i].v)-(b);
                update(rt[k1],1,k,rt[k1-1],pos,1);
                k1++;
                add(pos);
            }
            else if(px[i].mark==1)  ans1+=(LL)b[query(rt[px[i].l-1],rt[px[i].r],1,k,px[i].v)];
            else if(px[i].mark==2)
            {
                int pos=lower_bound(b+1,b+k+1,px[i].v)-(b);
                get(pos);
            }
            else ans3+=(LL)b[query(rt[0],rt[k1-1],1,k,px[i].v)];
        }
        printf("Case %d:\n",ncase++);
        printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
    }
    return 0;
}

没用树状数组记录

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
const int N = 1e5+1000;
typedef long long LL;
int rt[N], ls[N*20], rs[N*20], sum[N*20];
int  b[N];
LL ans2;
int tot;
void build(int &o,int l,int r)
{
    o= ++tot;
    sum[o]=0;
    if(l==r) return ;
    int mid=(l+r)/2;
    build(ls[o],l,mid);
    build(rs[o],mid+1,r);
    return ;
}
void update(int &o,int l,int r,int last,int p,int v)
{
    o= ++tot;
    ls[o]=ls[last],rs[o]=rs[last];
    sum[o]=sum[last]+v;
    if(l==r) return ;
    int mid=(l+r)/2;
    if(p<=mid) update(ls[o],l,mid,ls[last],p,v);
    else update(rs[o],mid+1,r,rs[last],p,v);
    return ;
}
int query(int ss,int tt,int l,int r,int cnt)
{
    if(l==r) return l;
    int tmp=(sum[ls[tt]]-sum[ls[ss]]);
    int mid=(l+r)/2;
    if(tmp>=cnt) return query(ls[ss],ls[tt],l,mid,cnt);
    else return query(rs[ss],rs[tt],mid+1,r,cnt-tmp);
}
void query1(int tt,int l,int r,int p)
{
    if(r<=p)
    {
        ans2+=(LL)(sum[tt]);
        return ;
    }
    int mid=(l+r)/2;
    if(p<=mid) return query1(ls[tt],l,mid,p);
    else
    {
        ans2+=(LL)sum[ls[tt]];
        return query1(rs[tt],mid+1,r,p);
    }
}
char str[20];
struct node
{
    int l, r, mint, v, mark;
} p[2*N];
int main()
{
    int n, ncase=1;
    while(scanf("%d", &n)!=EOF)
    {
        int k=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%s",str);
            if(str[0]=='I') p[i].mark=0, scanf("%d", &p[i].v),b[++k]=p[i].v;
            else if(str[6]=='1')  p[i].mark=1,scanf("%d %d %d",&p[i].l,&p[i].r,&p[i].mint);
            else if(str[6]=='2')  p[i].mark=2,scanf("%d",&p[i].v);
            else  p[i].mark=3,scanf("%d",&p[i].mint);
        }
        sort(b+1,b+k+1);
        ans2=0, tot=0;
        build(rt[0],1,k);
        LL ans1=0, ans3=0;
        int k1=1;
        for(int i=1;i<=n;i++)
        {
            if(p[i].mark==0)
            {
                int pos=lower_bound(b+1,b+k+1,p[i].v)-(b);
                update(rt[k1],1,k,rt[k1-1],pos,1);
                k1++;
            }
            else if(p[i].mark==1)  ans1+=(LL)b[query(rt[p[i].l-1],rt[p[i].r],1,k,p[i].mint)];
            else if(p[i].mark==2)
            {
                int pos=lower_bound(b+1,b+k+1,p[i].v)-(b);
                query1(rt[k1-1],1,k,pos);
            }
            else ans3+=(LL)b[query(rt[0],rt[k1-1],1,k,p[i].mint)];
        }
        printf("Case %d:\n",ncase++);
        printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值