Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:
1.查询k在区间内的排名
2.查询区间内排名为k的值
3.修改某一位值上的数值
4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)
5.查询k在区间内的后继(后继定义为大于x,且最小的数)
Input
第一行两个数 n,m 表示长度为n的有序序列和m个操作
第二行有n个数,表示有序序列
下面有m行,opt表示操作标号
若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名
若opt=2 则为操作2,之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数
若opt=3 则为操作3,之后有两个数pos,k 表示将pos位置的数修改为k
若opt=4 则为操作4,之后有三个数l,r,k 表示查询区间[l,r]内k的前驱
若opt=5 则为操作5,之后有三个数l,r,k 表示查询区间[l,r]内k的后继
Output
对于操作1,2,4,5各输出一行,表示查询结果
Sample Input
9 6
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5
Sample Output
2
4
3
4
9
4
3
4
9
HINT
1.n和m的数据范围:n,m<=50000
2.序列中每个数的数据范围:[0,1e8]
3.虽然原题没有,但事实上5操作的k可能为负数
直接裸体,测试代码用,还可以主席树做,还没学,只是学了树套树而已:
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define maxn 50003
#define inf 0x7FFFFFFF
using namespace std;
class multi_treap//multi_treap和treap类似,也是一种排序二叉树,但支持重复元素,其他功能上和treap一样
{
private:
struct node//multi_treap的节点定义
{
node* ch[2];
int v, r, s, num;
int cmp(int x)
{
if (x == v) return -1;
return(x < v ? 0 : 1);
}
};
node *root;//multi_treap的根
void updata(node* o)
{
if (!o) return;
o->s = o->num;
if (o->ch[0]) o->s += o->ch[0]->s;
if (o->ch[1]) o->s += o->ch[1]->s;
}
void rateto(node* &o, int d)
{
node* k;
k = o->ch[d ^ 1];
o->ch[d ^ 1] = k->ch[d];
k->ch[d] = o;
updata(o);
updata(k);
o = k;
}
void add(node* &o, int x)
{
if (!o)
{
o = new node();
o->ch[0] = o->ch[1] = 0;
o->v = x;
o->r = rand()*rand();
o->s = 1;
o->num = 1;
return;
}
int d = o->cmp(x);
if (d == -1)
{
o->num++;//multi的开关,注释掉可以关闭multi功能
o->s++;
}
else
{
add(o->ch[d], x);
updata(o);
if (o->r < o->ch[d]->r) rateto(o, d ^ 1);
}
}
void remove(node* &o, int x)
{
int d = o->cmp(x);
if (d == -1)
{
if (o->num>1)
{
o->num--;
o->s--;
}
else
{
if (!(o->ch[0]))
{
node *p = o;
o = o->ch[1];
free(p);
p = 0;
}
else if (!(o->ch[1]))
{
node *p = o;
o = o->ch[0];
free(p);
p = 0;
}
else
{
int d2 = o->ch[0]->r > o->ch[1]->r ? 1 : 0;
rateto(o, d2);
remove(o->ch[d2], x);
}
}
}
else remove(o->ch[d], x);
updata(o);
}
int Kth(int k)
{
node *p = root;
while (1)
{
int su = (p->ch[0] ? p->ch[0]->s : 0);
if (su + 1 <= k&&k <= su + p->num) return p->v;
else if (k <= su) p = p->ch[0];
else
{
k -= su + p->num;
p = p->ch[1];
}
}
}
int Rank(int x)
{
int ans = 0;
node *p = root;
while (p)
{
if (p->v == x)
{
ans += 1 + (p->ch[0] ? p->ch[0]->s : 0);
return ans;
}
else if (x < p->v) p = p->ch[0];
else
{
ans += p->num + (p->ch[0] ? p->ch[0]->s : 0);
p = p->ch[1];
}
}
return ans+1;
}
int RankPlus(int x)
{
int ans = 0;
node *p = root;
while (p)
{
if (p->v == x)
{
ans += p->num + (p->ch[0] ? p->ch[0]->s : 0);
return ans;
}
else
if (x < p->v) p = p->ch[0];
else
{
ans += p->num + (p->ch[0] ? p->ch[0]->s : 0);
p = p->ch[1];
}
}
return ans;
}
int Suc(int x)
{
node* p = root;
int ans = 0;
bool find = 0;
while (p)
{
if (p->v > x)
{
ans = p->v;
find = 1;
}
p = p->ch[p->v <= x];
}
if (find) return ans;
else return -1;
}
int Pre(int x)
{
node *p = root;
int ans = 0;
bool find = 0;
while (p)
{
if (p->v < x)
{
ans = p->v;
find = 1;
}
p = p->ch[p->v < x];
}
if (find) return ans;
else return -1;
}
bool Find(node* o, int x)
{
while (o)
{
int d = o->cmp(x);
if (d == -1) return 1;
else o = o->ch[d];
}
return 0;
}
void mymemory(node* &o)
{
if (!o) return;
if (o->ch[0]) mymemory(o->ch[0]);
if (o->ch[1]) mymemory(o->ch[1]);
free(o);
o = 0;
}
//以上是multi_treap的实现部分
public:
int size() //返回multi_treap中元素的个数
{
if (root) return root->s;
else return 0;
}
void insert(int x)//在multi_treap中插入一个x
{
add(root, x);
}
void erase(int x)//删除multi_treap中的元素x,如果multi_treap中x的有多个,则只删除一个
{
remove(root, x);
}
int kth(int x) //返回multi_treap中排名为x的元素的值,如果x非法,则返回-1
{
if (x <= 0 || x > size()) return -1;
else return Kth(x);
}
int rank(int x) //返回x在multi_treap中的排名,如果x没有在multi_treap中,则返回-1
{
return Rank(x);
}
int suc(int x) //返回multi_treap中大于x的第一个元素的值(后驱),如果x大于等于multi_treap中的最大值,则返回-1
{
return Suc(x);
}
int pre(int x) //返回multi_treap中小于x的第一个元素的值(前驱),如果x小于等于multi_treap中的最小值,则返回-1
{
return Pre(x);
}
bool find(int x) //返回x是否在该multi_treap
{
return Find(root, x);
}
void clear() //清空multi_treap
{
mymemory(root);
}
multi_treap() //multi_treap初始化
{
root = 0;
}
int rankplus(int x)
{
return RankPlus(x);
}
}treap[maxn<<1];
int va[maxn],use[maxn];
int cnt;
void build(int l,int r)
{
int id=l+r|l!=r;
for(int i=l;i<=r;i++) treap[id].insert(va[i]);
if(l==r) return;
int mid=(l+r)>>1;
build(l,mid);
build(mid+1,r);
}
void choose(int L,int R,int l,int r)
{
int id=L+R|L!=R;
if(l<=L&&r>=R)
{
use[++cnt]=id;
return;
}
int mid=(L+R)>>1;
if(l<=mid) choose(L,mid,l,r);
if(r>mid) choose(mid+1,R,l,r);
}
void change(int l,int r,int pos,int v)
{
int id=l+r|l!=r;
treap[id].erase(va[pos]);
treap[id].insert(v);
if(l==r) return;
int mid=(l+r)>>1;
if(pos<=mid) change(l,mid,pos,v);
else change(mid+1,r,pos,v);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<(maxn<<1);i++) treap[i].clear();
for(int i=1;i<=n;i++) scanf("%d",&va[i]);
build(1,n);
while(m--)
{
int op;
scanf("%d",&op);
if(op==3)///将pos位置的数修改为k
{
int pos,k;
scanf("%d%d",&pos,&k);
change(1,n,pos,k);
va[pos]=k;
}
else
{
int L,R,K;
scanf("%d%d%d",&L,&R,&K);
cnt=0;
choose(1,n,L,R);
if(op==1)///查询k在区间[l,r]的排名
{
int ans=0;
for(int i=1;i<=cnt;i++)
{
ans+=treap[use[i]].rank(K)-1;
}
printf("%d\n",ans+1);
}
else if(op==4)///查询区间[l,r]内k的前驱
{
int ans=0;
for(int i=1;i<=cnt;i++)
{
int tem=treap[use[i]].pre(K);
if(tem!=-1) ans=max(ans,tem);
}
printf("%d\n",ans);
}
else if(op==5)///查询区间[l,r]内k的后继
{
int ans=inf;
for(int i=1;i<=cnt;i++)
{
int tem=treap[use[i]].suc(K);
if(tem!=-1) ans=min(ans,tem);
}
printf("%d\n",ans);
}
else if(op==2)///查询区间[l,r]内排名为k的数
{
int l=-1,r=1e9;
while(l<r)
{
int mid=(l+r)>>1,ans=0;
for(int i=1;i<=cnt;i++) ans+=treap[use[i]].rank(mid)-1;
if(ans+1<=K) l=mid+1;
else r=mid;
}
printf("%d\n",l-1);
}
}
}
}
return 0;
}