hdu 5204 Rikka with sequence(BestCoder Round #37)

Rikka with sequence

                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 378    Accepted Submission(s): 75

Problem 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 n(n≤100000).
Each of the following n 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. (1≤w≤109,L≤R≤1018)

R will not be larger than the length of the sequence

Output

For each query operation, output one number – 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

官方题解:

这题看起来一副非常厉害的样子。。其实是大水题。
对于一个询问,考虑这个询问前第i次修改操作,那么这次修改操作出现在序列中第一个位置是2i?1。然后在询问范围内最多只有60个数,暴力大法好就好了,时间复杂度O(nlogR),R是询问的最大下标。

ps:自己模拟一下就会发现,对第i个要插入的数,在整个区间内插入的数的数量是2^(i-1)个,并且在[1,R]内也满足,后一个数插入的数的数量一定是前一个插入数的2倍,或2倍+1,知道这个后,[L,R]=[1,R]-[1,L-1],于是sum[i]+=v*(x+1)/2;在[1,R]中v为1,在[1,L-1]中v为-1,此时的i的顺序与插入顺序相反。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=110000;
int a[maxn],b[maxn];
long long sum[maxn];
int len=69;
void digt(long long x,int v)
{
    int i=0;
    while(x)
    {
        sum[i]+=v*(x+1)/2;
        x=x/2;
        i++;
    }
}
bool cmp(int x,int y)
{
    return a[len-x]<a[len-y];
}
int main()
{
    int t,op;
    scanf("%d",&t);
    long long l,r,k,ans;
    while(t--)
    {
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d",&a[++len]);
        }
        else
        {
            scanf("%I64d%I64d%I64d",&l,&r,&k);
            for(int i=0;i<=100;i++)
            sum[i]=0;
            digt(r,1);
            digt(l-1,-1);
            for(int i=0;i<=69;i++)
            b[i]=i;
            sort(b,b+70,cmp);
            ans=0;
            for(int i=0;i<70;i++)
            {
               // cout<<i<<"    "<<sum[b[i]]<<endl;
                if(ans+sum[b[i]]>=k)
                {
                    printf("%d\n",a[len-b[i]]);
                    break;
                }
                else
                {
                    ans+=sum[b[i]];
                }
            }
        }
    }
    return 0;
}

时间: 2024-08-27 01:33:14

hdu 5204 Rikka with sequence(BestCoder Round #37)的相关文章

HDU 5063 Operation the Sequence(BestCoder Round #13)

Problem Description: You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types:Type1: O 1 call fun1();Type2: O 2 call fun2();Type3:

HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 #define LL long long 6 const int N = 100005; 7 int t, n, p1, p2, p3; 8 LL a[N]; 9

HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans += n - j + 1 . i++ 的时候看看需不需要size-- 就可以更新了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define

HDU 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le

HDU 4908 BestCoder Sequence(BestCoder Round #3)

Problem Description: Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence. As the best coder, M

hdu5504 GT and sequence(BestCoder Round #60 )

GT and sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 334 Accepted Submission(s): 85 Problem Description You are given a sequence of N integers. You should choose some numbers(at least o

hdu 5063 Operation the Sequence(Bestcoder Round #13)

Operation the Sequence                                                                     Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 158    Accepted Submission(s): 74 Problem Description

HDU - 5210 - Delete &amp;&amp; 5211 - Mutiple (BestCoder Round #39)

题目传送:HDU - 5210 HDU - 5210 - Delete 思路:简单题 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include &l

HDU 4883 TIANKENG’s restaurant(BestCoder Round #2)

Problem Description: TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the