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

Sample Output

2
4
3
4
9

HINT

1.n和m的数据范围:n,m<=50000
2.序列中每个数的数据范围:[0,1e8]
3.虽然原题没有,但事实上5操作的k可能为负数

Solution

外层树状数组,内层值域线段树,在每个节点记count,修改时分别修改,查询时用r和l-1对应的一大堆节点做差(有点像主席树)

Code

  1. #include<bits/stdc++.h>
  2. typedef unsigned char uchar;
  3. typedef unsigned int uint;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. typedef double db;
  7. typedef long double ldb;
  8. #define xx first
  9. #define yy second
  10. template<typename T> inline T max(T a,T b){return a>b?a:b;}
  11. template<typename T> inline T min(T a,T b){return a<b?a:b;}
  12. template<typename T> inline T abs(T a){return a>0?a:-a;}
  13. template<typename T> inline void repr(T &a,T b){if(a<b)a=b;}
  14. template<typename T> inline void repl(T &a,T b){if(a>b)a=b;}
  15. template<typename T> T gcd(T a,T b){if(b)return gcd(b,a%b);return a;}
  16. #define mp(a,b) std::make_pair(a,b)
  17. #define pb push_back
  18. #define lb(x) ((x)&(-(x)))
  19. #define sqr(x) ((x)*(x))
  20. struct node
  21. {
  22. node *lc,*rc;
  23. int cnt;
  24. node();
  25. }_null,*null=&_null;
  26. node::node(){lc=rc=null;cnt=0;}
  27. int pf;
  28. void erase(node *x)
  29. {
  30. if(x->lc!=null)erase(x->lc);
  31. if(x->rc!=null)erase(x->rc);
  32. delete x;
  33. }
  34. void modify(node *&x,int l,int r,int p,int v)
  35. {
  36. if(x==null)x=new node;
  37. x->cnt+=v;
  38. if(!x->cnt)
  39. {
  40. erase(x);
  41. x=null;
  42. return;
  43. }
  44. if(l!=r)
  45. {
  46. int f=(l+r)/2;
  47. if(p<=f)
  48. modify(x->lc,l,f,p,v);
  49. else
  50. modify(x->rc,f+1,r,p,v);
  51. }
  52. }
  53. struct group
  54. {
  55. node *x[50];
  56. int mul[50],sz;
  57. inline int cnt()
  58. {
  59. int ret=0;
  60. for(int i=0;i<sz;i++)ret+=x[i]->cnt*mul[i];
  61. return ret;
  62. }
  63. inline group* lc(group *f)
  64. {
  65. f->sz=sz;
  66. for(int i=0;i<sz;i++)f->x[i]=x[i]->lc,f->mul[i]=mul[i];
  67. return f;
  68. }
  69. inline group* rc(group *f)
  70. {
  71. f->sz=sz;
  72. for(int i=0;i<sz;i++)f->x[i]=x[i]->rc,f->mul[i]=mul[i];
  73. return f;
  74. }
  75. };
  76. int cnt(group *x,int l,int r,int ql,int qr)
  77. {
  78. if(l==ql&&r==qr)return x->cnt();
  79. int t=(l+r)/2,ans=0;
  80. group ch;
  81. if(ql<=t)ans+=cnt(x->lc(&ch),l,t,ql,min(t,qr));
  82. if(qr>t)ans+=cnt(x->rc(&ch),t+1,r,max(ql,t+1),qr);
  83. return ans;
  84. }
  85. int kth(group *x,int l,int r,int rk)
  86. {
  87. if(l==r)return l;
  88. group ch;
  89. x->lc(&ch);
  90. int lcnt;
  91. if((lcnt=ch.cnt())>=rk)
  92. return kth(&ch,l,(l+r)/2,rk);
  93. else
  94. return kth(x->rc(&ch),(l+r)/2+1,r,rk-lcnt);
  95. }
  96. int gmax(group *x,int l,int r,int p)
  97. {
  98. if(!x->cnt())return 0;
  99. if(l==r)return l;
  100. int t=(l+r)/2;
  101. group ch;
  102. if(p<=t)return gmax(x->lc(&ch),l,t,p);
  103. if(r==p)
  104. {
  105. x->rc(&ch);
  106. if(ch.cnt())return gmax(&ch,t+1,r,p);
  107. return gmax(x->lc(&ch),l,t,t);
  108. }
  109. return max(gmax(x->lc(&ch),l,t,t),gmax(x->rc(&ch),t+1,r,p));
  110. }
  111. int gmin(group *x,int l,int r,int p)
  112. {
  113. if(!x->cnt())return 0x7fffffff;
  114. if(l==r)return l;
  115. int t=(l+r)/2;
  116. group ch;
  117. if(p>t)return gmin(x->rc(&ch),t+1,r,p);
  118. if(l==p)
  119. {
  120. x->lc(&ch);
  121. if(ch.cnt())return gmin(&ch,l,t,p);
  122. return gmin(x->rc(&ch),t+1,r,t+1);
  123. }
  124. return min(gmin(x->lc(&ch),l,t,p),gmin(x->rc(&ch),t+1,r,t+1));
  125. }
  126. #define nl 0
  127. #define nr 100000001
  128. node *root[50001];
  129. int v[50001];
  130. int main()
  131. {
  132. int n,m;
  133. scanf("%d%d",&n,&m);
  134. for(int i=0;i<=n;i++)
  135. root[i]=new node;
  136. for(int i=1;i<=n;i++)
  137. {
  138. scanf("%d",v+i);
  139. for(int j=i;j<=n;j+=lb(j))
  140. modify(root[j],nl,nr,v[i],1);
  141. }
  142. while(m--)
  143. {
  144. int opt,a,b,c;
  145. scanf("%d%d%d",&opt,&a,&b);
  146. if(opt==3)
  147. {
  148. for(int j=a;j<=n;j+=lb(j))
  149. modify(root[j],nl,nr,v[a],-1);
  150. v[a]=b;
  151. for(int j=a;j<=n;j+=lb(j))
  152. modify(root[j],nl,nr,v[a],1);
  153. }
  154. else
  155. {
  156. scanf("%d",&c);
  157. group tmp;
  158. tmp.sz=0;
  159. for(int j=b;j;j^=lb(j))
  160. tmp.mul[tmp.sz]=1,tmp.x[tmp.sz++]=root[j];
  161. for(int j=a-1;j;j^=lb(j))
  162. tmp.mul[tmp.sz]=-1,tmp.x[tmp.sz++]=root[j];
  163. if(opt==1)
  164. {
  165. printf("%d\n",cnt(&tmp,nl,nr,nl,min(nr,c-1))+1);
  166. }
  167. else if(opt==2)
  168. {
  169. printf("%d\n",kth(&tmp,nl,nr,c));
  170. }
  171. else if(opt==4)
  172. {
  173. printf("%d\n",gmax(&tmp,nl,nr,min(nr,c-1)));
  174. }
  175. else
  176. {
  177. printf("%d\n",gmin(&tmp,nl,nr,max(nl,c+1)));
  178. }
  179. }
  180. }
  181. }