hdu5204Rikka with sequence+数学

Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta have a sequence. Because the sequence is very self-willed(RenXing), at first the sequence is empty. And then Yuta do n operations on this sequence, each operation is either of these two types:

1.Add a number w into each gap of the sequence. For example if w=3 and the sequence before is “2 4”, it will be changed to “3 2 3 4 3”.
after the first operation of the first type, there is only one number in the sequence

2.Query the kth small number in the subsequence [L,R]. For example if k=2, L=2, R=4 and the sequence is “3 2 3 4 2”, the answer will be 3.

Yuta wants Rikka to tell him the answer of each query.

It is too difficult for Rikka. Can you help her?

Input
The first line contains one number . Each of the following lines describes an operation: if it is “1 w” it will be the first type. Otherwise if it is “2 L R k”, it will be the second type.
R will not be larger than the length of the sequence

Output
For each query operation, output one number �C the answer.

Sample Input

6
1 3
1 1
2 2 3 2
1 2
2 3 5 2
2 1 4 4

Sample Output

3
2
3
现在有一个序列,开始时空的。接下来发生了n个事件,每一个事件是以下两种之一:
1.在序列的开头、结尾以及每相邻两个元素之间都插入一个权值为w的元素。若第一步执行事件一,执行后数列仅有一个数字w.
2.查询第L个元素到第R个中权值第k小的权值是多少。
/*
a
bab
cbcacbc
dcdbdcdadcdbdcd
*/
先手动模拟一下过程。可以发现第i次插入时,奇数为上全是w,那么我们可以知道填了多少个w咯!(r+1)/2-l/2)。然后将填的数逆回去找出来。。排个序找到第k个就好了。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL num[100008];
int cnt;
vector<pair<LL,LL> >Q;//数字x的个数y
vector<pair<LL,LL> >::iterator it;
LL querry(LL l,LL r,LL k){
    Q.clear();
    for(int i=cnt;i>=1;i--){
        Q.push_back(make_pair(num[i],(r+1)/2-l/2));
        r=r/2,l=(l+1)/2;
        if(r<l) break;
    }
    sort(Q.begin(),Q.end());
    for(it=Q.begin();it!=Q.end();it++){
        k-=it->second;//it->first的个数
        if(k<=0) return it->first;
    }
    return 0;
}
int main(){
    int n,op;
    LL l,r,k,x;
    scanf("%d",&n);
    cnt=0;
    while(n--){
        scanf("%d",&op);
        if(op==1){
            scanf("%I64d",&x);
            num[++cnt]=x;
        }
        else {
            scanf("%I64d %I64d %I64d",&l,&r,&k);
            printf("%I64d\n",querry(l,r,k));
        }
    }
    return 0;
}
/*
a
bab
cbcacbc
dcdbdcdadcdbdcd
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值