Rikka with sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 515 Accepted Submission(s): 95
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?
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
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
Source
BestCoder Round #37 ($)
好厉害的题目,下面是官方题解
这题看起来一副非常厉害的样子。。其实是大水题。
对于一个询问,考虑这个询问前第i次修改操作,那么这次修改操作出现在序列中第一个位置是2i−1。然后在询问范围内最多只有60个数,暴力大法好就好了,时间复杂度O(nlogR),R是询问的最大下标。
Hack点:pretest里的操作1最多只有30次,即没有超过int。所以可能有人会爆int?不过我觉得是不会有那么逗逼的人的。。
好厉害的题目,下面是官方题解
这题看起来一副非常厉害的样子。。其实是大水题。
对于一个询问,考虑这个询问前第i次修改操作,那么这次修改操作出现在序列中第一个位置是2i−1。然后在询问范围内最多只有60个数,暴力大法好就好了,时间复杂度O(nlogR),R是询问的最大下标。
Hack点:pretest里的操作1最多只有30次,即没有超过int。所以可能有人会爆int?不过我觉得是不会有那么逗逼的人的。。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int SIGMA_SIZE=70;
typedef long long LL;
int N,cnt;
int X[maxn];
LL s[maxn];
int pos[maxn];
bool cmp(int x,int y)
{
return X[cnt-x]<X[cnt-y];
}
void process(LL x,int c)
{
for(int i=0;i<=SIGMA_SIZE;i++)
s[i]+=c*((x+1)/2),x/=2;
}
int main()
{
int op;
LL x,y,k;
while(scanf("%d",&N)!=EOF)
{
cnt=0;
while(N--)
{
scanf("%d",&op);
if(op==1)
{
int tmp;
scanf("%d",&tmp);
X[++cnt]=tmp;
}
else
{
scanf("%I64d%I64d%I64d",&x,&y,&k);
int ans=0;
for(int i=0;i<=SIGMA_SIZE;i++)
s[i]=0;
process(y,1);
process(x-1,-1);
for(int i=0;i<=SIGMA_SIZE;i++)
pos[i]=i;
sort(pos,pos+SIGMA_SIZE+1,cmp);
LL sum=0;
for(int i=0;i<=SIGMA_SIZE;i++)
{
if(sum+s[pos[i]]>=k)
{
printf("%d\n",X[cnt-pos[i]]);
break;
}
else sum+=s[pos[i]];
}
}
}
}
return 0;
}