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) 

InputThere 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) 

OutputOutput 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.

题解:可持续化线段树的模板题

AC代码为:

/*
有四种操作
1、在序列最优插入一个数字(该数字从没出现过)
2、询问序列内某区间第k小值
3、询问当前序列内数字x是第几小的(x一定在序列中)
4、询问当前序列内第k小的值
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;  
int tot,rt[N]; 
char s[10];
vector<int> v;  
struct data  
{  
    int id;  
    int l,r,x;  
}data[N*2];  
   
inline int getid(int x) {return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;}  
  
void input(int n)  
{  
    char s[10];
    int x;  
    for(int i=0;i<n;i++)  
    {  
        scanf("%s",s);  
        if(s[0]=='I')  
        {  
            scanf("%d",&x); 
            data[i].id = 0;  
            data[i].x = x;  
            v.push_back(x);  
        }  
        else if(s[6]=='1')  
        {  
            int a,b,c;  
            scanf("%d%d%d",&a,&b,&c);  
            data[i].id = 1;  
            data[i].l = a, data[i].r = b, data[i].x = c;  
        }  
        else if(s[6]=='2')  
        {  
            scanf("%d",&x);  
            data[i].id = 2;  
            data[i].x = x;  
        }  
        else  
        {  
            scanf("%d",&x);  
            data[i].id = 3;  
            data[i].x = x;  
        }  
    }  
}    
struct node  
{  
    int l,r,sum;  
}tree[N*24];   
    
inline void build(int l,int r,int &x) //1~v.size() 
{  
    x = ++tot;  
    tree[x].sum = 0;  
    if(l==r) return;  
    int m = (l+r) >> 1;  
    build(l,m,tree[x].l);  
    build(m+1,r,tree[x].r);  
}  
  
inline void update(int l,int r,int &x,int y,int k)  
{  
   tree[++tot] = tree[y], tree[tot].sum++,x=tot;  
    if(l==r) return;  
    int m = (l+r) >> 1;  
    if(k<=m) update(l,m,tree[x].l,tree[y].l,k);  
    else update(m+1,r,tree[x].r,tree[y].r,k);  
}  
  
inline int query1(int l,int r,int y,int x,int k)  //查找区间第k小的数  
{  
    if(l==r) return l;  
    int mid = (l+r) >> 1;  
    int sum = tree[tree[x].l].sum - tree[tree[y].l].sum;  
    if(k<=sum) return query1(l,mid,tree[y].l,tree[x].l,k);  
    else return query1(mid+1,r,tree[y].r,tree[x].r,k-sum);  
}  
  
inline int query2(int l,int r,int x,int k) //在当前序列中,输出X是第几小的数。  
{  
    if(l==r) return 1;  
    int mid = (l+r) >> 1;  
    if(k<=mid) return query2(l,mid,tree[x].l,k);  
    else  
    {  
        int sum = tree[tree[x].l].sum;  
        return sum += query2(mid+1,r,tree[x].r,k);  
    }  
}  
  
inline int query3(int l,int r,int x,int k) //找到当前序列中第X小的数是几  
{  
    if(l==r) return l;  
    int mid = (l+r) >> 1;  
    int sum = tree[tree[x].l].sum;  
    if(sum>=k) return query3(l,mid,tree[x].l,k);  
    else return query3(mid+1,r,tree[x].r,k-sum);  
}      
int main()  
{   
    ios::sync_with_stdio(0);  
    cin.tie(0); 
    int n;  
    int cas = 1;  
    while(~scanf("%d",&n))  
    {  
        v.clear();  
        tot = 0;  
        LL ans1 = 0, ans2 = 0, ans3 = 0;  
        input(n);  
        sort(v.begin(),v.end());  
        v.erase(unique(v.begin(),v.end()),v.end());  
        int cnt = v.size();  
        build(1,cnt,rt[0]);  
        int now = 1;  
        for(int i=0;i<n;i++)  
        {  
            if(data[i].id==0)  
            {  
                update(1,cnt,rt[now],rt[now-1],getid(data[i].x));  
                now++;  
            }  
            else if(data[i].id==1)  
            {  
                int l = data[i].l, r = data[i].r, x = data[i].x;  
                ans1 += v[query1(1,cnt,rt[l-1],rt[r],x)-1];  
            }  
            else if(data[i].id==2) ans2+=query2(1,cnt,rt[now-1],getid(data[i].x));               
            else ans3+=v[query3(1,cnt,rt[now-1],data[i].x)-1];      
        }    
        printf("Case %d:\n%I64d\n%I64d\n%I64d\n",cas++,ans1,ans2,ans3);  
    }  
    return 0;  
}

最新文章

  1. 过年7天乐,学nodejs 也快乐
  2. react UI交互 简单实例
  3. Android中Bitmap,byte[],Drawable相互转化
  4. 8 ways rich people view the world differently than the average person
  5. open Live Writer配置步骤
  6. javascript 函数节流方法
  7. 自制获取data-自定义属性
  8. 百度地图Marker优化方案
  9. 03-MySQL表操作
  10. opencv 图片旋转
  11. git笔记(1)-搭建
  12. Signalr实现消息推送
  13. php-mysql问题:mysqli_connect(): Headers and client library minor version mismatch. Headers:50556 Library:50637
  14. Spring webFlux:坐等spring-boot-starter-data-mysql-reactive
  15. HDU4278
  16. nine
  17. 游戏行业的女性拥有强大的新盟友:Facebook
  18. 命令行启用IIS Express
  19. hdwiki 前后台版权信息在哪修改
  20. Java开发 - 异常 - 使用throws

热门文章

  1. python模块——socket
  2. SpringMVC错误:Failed to read candidate component class:file... ...
  3. SQlALchemy session详解
  4. 用IDEA导入Eclipse的JavaWEB项目
  5. Jquery 处理返回的 Json 数组
  6. ReadWriteLock: 读写锁
  7. 优秀的github项目学习
  8. vue引用组件的两个方法
  9. ArcGIS 切片与矢量图图层顺序问题
  10. PHP变量的初始化以及赋值方式介绍