划分树(2010天津现场赛)hdu3727

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Jewel

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


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.


题意:有四种操作

0:在后面插入x

1:询问区间第k大的是多少

2:询问当前整个区间中x排第几

3:询问当前整个区间第k大的是谁

思路:离线操作,poj2104的加强版,只不过是多了第2种操作,这个操作跟query操作差不多,只是返回的东西不一样,思路是一样的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N;

struct OP
{
    int id;
    int x,l,r;
}op[maxn*2];

struct P_TREE
{
    int n,order[maxn];
    int val[20][maxn],num[20][maxn];
    void init()
    {
        n--;
        for(int i=0;i<20;i++)val[i][0]=num[i][0]=0;
        for(int i=1;i<=n;i++)val[0][i]=order[i];
        sort(order+1,order+n+1);
        build(0,1,n);
    }
    void build(int ind,int l,int r)
    {
        if(l==r)return;
        int mid=(l+r)>>1;
        int ln=l,rn=mid+1,same=r-l+1;
        for(int i=l;i<=r;i++)
            if(val[ind][i]<order[mid])same--;
        for(int i=l;i<=r;i++)
        {
            int flag=0;
            if(val[ind][i]<order[mid]||(val[ind][i]==order[mid]&&same))
            {
                flag=1;
                val[ind+1][ln++]=val[ind][i];
                if(val[ind][i]==order[mid])same--;
            }
            else val[ind+1][rn++]=val[ind][i];
            num[ind][i]=num[ind][i-1]+flag;
        }
        build(ind+1,l,mid);
        build(ind+1,mid+1,r);
    }
    int query(int ind,int l,int r,int q1,int q2,int k)//询问区间内排第k的是谁
    {
        if(k==0)return -1;
        if(q2-q1+1<k)return 0;
        if(l==r)return val[ind][l];
        int mid=(l+r)>>1;
        int lx=num[ind][q1-1]-num[ind][l-1];
        int ly=num[ind][q2]-num[ind][q1-1];
        int rx=q1-1-l+1-lx;
        int ry=q2-q1+1-ly;
        if(ly>=k)return query(ind+1,l,mid,l+lx,l+lx+ly-1,k);
        else
        {
            int st=mid+1+rx;
            int en=mid+1+rx+ry-1;
            return query(ind+1,mid+1,r,st,en,k-ly);
        }
    }
    int getvalue(int ind,int l,int r,int q1,int q2,int x)//查询区间内x排第几
    {
        if(l>=r)return 1;
        int mid=(l+r)>>1;
        int lx=num[ind][q2]-num[ind][q1-1];
        int ly=num[ind][q1-1]-num[ind][l-1];
        int ry=q1-1-l+1-ly;
        int rx=q2-q1+1-lx;
        if(x<=order[mid])return getvalue(ind+1,l,mid,l+ly,l+lx+ly-1,x);
        else
        {
            int st=mid+1+ry;
            int en=mid+1+ry+rx-1;
            return lx+getvalue(ind+1,mid+1,r,st,en,x);
        }
    }

}tree;

int main()
{
    int cas=1;
    while(scanf("%d",&N)!=EOF)
    {
        char s[20];
        tree.n=1;
        for(int i=1;i<=N;i++)
        {
            scanf("%s",s);
            if(s[0]=='I'){op[i].id=0;scanf("%d",&op[i].x);tree.order[tree.n++]=op[i].x;}
            else if(s[6]=='1'){op[i].id=1;scanf("%d%d%d",&op[i].l,&op[i].r,&op[i].x);}
            else {op[i].id=s[6]-'0';scanf("%d",&op[i].x);}
        }
        tree.init();
        int cnt=0;
        long long ans1=0,ans2=0,ans3=0;
        for(int i=1;i<=N;i++)
        {
            if(op[i].id==0)cnt++;
            else if(op[i].id==1){ans1=ans1+tree.query(0,1,tree.n,op[i].l,op[i].r,op[i].x);}
            else if(op[i].id==2){ans2=ans2+tree.getvalue(0,1,tree.n,1,cnt,op[i].x);}
            else {ans3=ans3+tree.query(0,1,tree.n,1,cnt,op[i].x);}
        }
        printf("Case %d:\n",cas++);
        printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值