可持久化线段树(主席树)

来源:互联网 发布:淘宝开放平台api 编辑:程序博客网 时间:2024/05/17 08:47

 函数式(现在又称主席式。。。)数据结构从来都没写过,感觉这个东西可以挖掘出不少东西出来,于是开一组专题。

先根据 Seter 留下的文本做一些记录。。

主席树大概是一种离线结构,我以前反正没看到过这东西,所以就自己给他起名字了!如果谁知道这东西的真名,请告诉我!

现在我们知道,主席树的全名应该是 函数式版本的线段树。加上附带的一堆 technology。。
。。总之由于原名字太长了,而且 “主席” 两个字念起来冷艳高贵,以后全部称之为主席树好了。。。

主席树的主体是线段树,准确的说,是很多棵线段树,存的是一段数字区间出现次数(所以要先离散化可能出现的数字)。举个例子,假设我每次都要求整个序列内的第 k 小,那么对整个序列构造一个线段树,然后在线段树上不断找第 k 小在当前数字区间的左半部分还是右半部分。这个操作和平衡树的 Rank 操作一样,只是这里将离散的数字搞成了连续的数字。

先假设没有修改操作:

对于每个前缀 S1…i,保存这样一个线段树 Ti,组成主席树。这样不是会 MLE 么?最后再讲。

注意,这个线段树对一条线段,保存的是这个数字区间的出现次数,所以是可以互相加减的!还有,由于每棵线段树都要保存同样的数字,所以它们的大小、形态也都是一样的!这实在是两个非常好的性质,是平衡树所不具备的。

对于询问 (i,j),我只要拿出 Tj 和 Ti-1,对每个节点相减就可以了。说的通俗一点,询问 i..j 区间中,一个数字区间的出现次数时,就是这些数字在 Tj 中出现的次数减去在 Ti-1 中出现的次数。

那么有修改操作怎么办呢?

如果将询问看成求一段序列的数字和,那么上面那个相当于求出了前缀和。加入修改操作后,就要用树状数组等来维护前缀和了。于是那个 “很好的性质” 又一次发挥了作用,由于主席树可以互相加减,所以可以用树状数组来套上它。做法和维护前缀和长得基本一样,不说了。

这段指出了主席树的主要性质。。

  • 线段树的每个结点,保存的是这个区间含有的数字的个数。
  • 主席树的每个结点,也就是每颗线段树的大小和形态也是一样的,也就是主席树之间可以相互进行加减运算。。

同时我们也枚举一下主席树的一些局限:

  • 主席树是一种离线结构。。(必须预先知道所有数字的范围。。这在一些应用中会成为障碍。。。
  • 存在 MLE 问题。。(如果按照定义里面的方法去写,对每个结点都需要开一整可线段树,至少都是 O(n2) 级别的空间。。

——————————————————
开始填坑。由于每棵线段树的大小形态都是一样的,而且初始值全都是 0,那每个线段树都初始化不是太浪费了?所以一开始只要建一棵空树即可。

然后是在某棵树上修改一个数字,由于和其他树相关联,所以不能在原来的树上改,必须弄个新的出来。难道要弄一棵新树?不是的,由于一个数字的更改只影响了一条从这个叶子节点到根的路径,所以只要只有这条路径是新的,另外都没有改变。比如对于某个节点,要往右边走,那么左边那些就不用新建,只要用个指针链到原树的此节点左边就可以了,这个步骤的前提也是线段树的形态一样。

假设s是数字个数,这个步骤的空间复杂度显然是 O(logs)。用树状数组去套它,共有 2logn 棵树被修改,m 个操作再加上一开始的空树和 n 个数字,总共就是 O((n+m)lognlogs)。Fotile 大神说如果加上垃圾回收的话,可以去掉一个 log…… ym

至此主席树结构已经分析清楚了。。

  1. const int N = 50009, M = 10009, NN = 2500009;  
  2.    
  3. int l[NN], r[NN], c[NN], total;  
  4.    
  5. PII A[N+M]; int B[N+M], Q[M][3];  
  6. int S[N], C[N], Null;  
  7. int n, m, An, Tn;  
  8. .. .  

这里仍然用池子法保存线段树的结点,NN 是一个估计值。。(大概是 nlogn 。。。
l[], r[], c[], total 分别是左孩子下标,右孩子下标,该区间的数字个数和当前已分配的结点数。
(线段树的区间可以在递归的时候实时计算,可以不予保存。。。

PII A[]; int B[]; A、B 用于离散化。。。 B 记录相应数字的 Rank 值。
int Q[]; 记录询问。。。

int S[], C[], Null; 是主席树下标。。
分别对应前缀和,树状数组 和 初始时的空树。

An 是 A 数组的长度,由初始 n 个数字和修改后的数字决定,
Tn 是所有出现的不同数字的个数,用于建立线段树,由对 A 数组去重后得到。

  1. #define lx l[x]  
  2. #define rx r[x]  
  3. #define ly l[y]  
  4. #define ry r[y]  
  5. #define cx c[x]  
  6. #define cy c[y]  
  7.    
  8. #define mid ((ll+rr)>>1)  
  9. #define lc lx, ll, mid  
  10. #define rc rx, mid+1, rr  
  11.    
  12.    
  13. void Build(int &x, int ll, int rr){  
  14.     x = ++total; if (ll < rr) Build(lc), Build(rc);  
  15. }  
  16.    
  17. int Insert(int y, int p, int d){  
  18.    
  19.     int x = ++total, root = x;  
  20.    
  21.     c[x] = c[y] + d; int ll = 0, rr = Tn;  
  22.    
  23.     while (ll < rr){  
  24.         if (p <= mid){  
  25.             lx = ++total, rx = ry;  
  26.             x = lx, y = ly, rr = mid;  
  27.         }  
  28.         else {  
  29.             lx = ly, rx = ++total;  
  30.             x = rx, y = ry, ll = mid + 1;  
  31.         }  
  32.         c[x] = c[y] + d;  
  33.     }  
  34.    
  35.     return root;  
  36. }  

这里是主席树主要支持的操作。。前面一堆 Macro 方便敲代码。。。
注意由于 l[], r[], 还有 m 全部有冲突干脆用 ll, rr, mid 代替区间下标。。。

这里 Insert() 过程就是前面所说的插入单链的操作。。。以 y 为基础,形成一棵新的主席树 x。
。。。没有修改的链仍然指向 y 的相对应部分。复杂度为树高。。。。

。。。下面是 int main(); 函数代码了。。。完整程序在最后。。。。

  1. int main(){  
  2.    
  3. #ifndef ONLINE_JUDGE  
  4.     freopen("in.txt""r", stdin);  
  5.     //freopen("out.txt", "w", stdout);  
  6. #endif  
  7.    
  8. #define key first  
  9. #define id second  
  10.    
  11.     RD(n, m); REP(i, n) A[i] = MP(RD(), i);  
  12.    
  13.     An = n; char cmd; REP(i, m){  
  14.         RC(cmd); if(cmd == 'Q') RD(Q[i][0], Q[i][1], Q[i][2]);  
  15.         else RD(Q[i][0]), Q[i][2] = 0, A[An++] = MP(RD(), An);  
  16.     }  
  17.    
  18.     sort(A, A + An), B[A[0].id] = Tn = 0;  
  19.     FOR(i, 1, An){  
  20.         if(A[i].key != A[i-1].key) A[++Tn].key = A[i].key;  
  21.         B[A[i].id] = Tn;  
  22.     }  
  23.    
  24.     Build(Null, 0, Tn); REP_1(i, n) C[i] = Null;  
  25.    
  26.     S[0] = Null; REP(i, n){  
  27.         S[i+1] = Insert(S[i], B[i], 1);  
  28.     }  
  29.    
  30.     An = n;  
  31.    
  32.     REP(i, m) if (Q[i][2]){  
  33.         OT(A[Query(Q[i][0], Q[i][1], Q[i][2])].key);  
  34.     }else{  
  35.         Modify(Q[i][0], B[Q[i][0]-1], -1);  
  36.         Modify(Q[i][0], B[Q[i][0]-1] = B[An++], 1);  
  37.     }  
  38. }  

待讨论问题:


  1. 由于每棵线段树的大小形态都是一样的,而且初始值全都是 0,那每个线段树都初始化不是太浪费了?所以一开始只要建一棵空树即可。

    事实上由于当一个结点的 c[] 结果为 0 时。。无论它向左还是向右递归下去结果都会是0 。。。
    所以初始时甚至不是建立空树。。而是只需要一个空结点即可。。。(如何实现这层。。。

  2. 仍然没有将 Lazy Evalutaion 的理念做到完美。。。这题中的 Lazy Evaluation 体现在两个方面。。
    主席树的单链修改和主席树与主席树之间的运算。。

    主席树与主席树之间的运算如果全部做的话一次是 O(n)。。。但是因为询问时只需要计算一条到叶子的路径。。。
    。。。上面的代码中,这种运算是通过 Struct Pack; 结构体现的。。。一种多颗主席树结点的打包。。
    。。全部在同一层的同一个位置。。向同一个方向递归下降。。一个 Pack; 的值为该结构内所有结点值的和。。

    。。现在实现的不足之处:

    • 无法很好的处理减运算。。因此这里才还要保存了 S[] 前缀和,让 C[] 逐个逐个修改。。。
      。。。然后每次回答询问的时候还要一并带上 S[] 。。Orz。(虽然说有助于预处理。。
    • 对计算过后结果无法有效利用。。(换句话说没有体现出主席式 Memorization 的一面。。。
    • 对单个链的插入操作也不是要即时处理的。。(。。如何用数据结构把所有操作先 Hold 下来 。。?。。这样的好处是有一些操作对结果没有影响是可以忽略。。另外把一堆操作放在一起当做一个操作处理的话有时也能得到 Bonus 。。。
  3. Fotile 大神说如果加上垃圾回收的话,可以去掉一个 log…… ym

    (另外正一个误。。。加上回收并不能去掉一个 log 。。。。


完整代码:树状数组套主席树

  1. #include <algorithm>  
  2. #include <iostream>  
  3. #include <iomanip>  
  4. #include <sstream>  
  5. #include <cstring>  
  6. #include <cstdio>  
  7. #include <string>  
  8. #include <vector>  
  9. #include <bitset>  
  10. #include <queue>  
  11. #include <stack>  
  12. #include <cmath>  
  13. #include <ctime>  
  14. #include <list>  
  15. #include <set>  
  16. #include <map>  
  17.    
  18. using namespace std;  
  19.    
  20. #define REP(i, n) for (int i=0;i<int(n);++i)  
  21. #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
  22. #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
  23. #define REP_1(i, n) for (int i=1;i<=int(n);++i)  
  24. #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)  
  25. #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)  
  26. #define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)  
  27. #define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)  
  28. #define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)  
  29. #define REP_N(i, n) for (i=0;i<int(n);++i)  
  30. #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)  
  31. #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)  
  32. #define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)  
  33. #define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)  
  34. #define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)  
  35. #define REP_1_N(i, n) for (i=1;i<=int(n);++i)  
  36. #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)  
  37. #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)  
  38. #define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)  
  39. #define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)  
  40. #define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)  
  41. #define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)  
  42. #define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)  
  43. #define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)  
  44.    
  45. #define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
  46. #define DO(n) while(n--)  
  47. #define DO_C(n) int n____ = n; while(n____--)  
  48. #define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)  
  49. #define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)  
  50. #define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)  
  51. #define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)  
  52. #define REP_2(i, j, n, m) REP(i, n) REP(j, m)  
  53. #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)  
  54.    
  55. #define ALL(A) A.begin(), A.end()  
  56. #define LLA(A) A.rbegin(), A.rend()  
  57. #define CPY(A, B) memcpy(A, B, sizeof(A))  
  58. #define INS(A, P, B) A.insert(A.begin() + P, B)  
  59. #define ERS(A, P) A.erase(A.begin() + P)  
  60. #define BSC(A, X) find(ALL(A), X) // != A.end()  
  61. #define CTN(T, x) (T.find(x) != T.end())  
  62. #define SZ(A) int(A.size())  
  63. #define PB push_back  
  64. #define MP(A, B) make_pair(A, B)  
  65.    
  66. #define Rush int T____; RD(T____); DO(T____)  
  67. #pragma comment(linker, "/STACK:36777216")  
  68. //#pragma GCC optimize ("O2")  
  69. #define Ruby system("ruby main.rb")  
  70. #define Haskell system("runghc main.hs")  
  71. #define Pascal system("fpc main.pas")  
  72.    
  73. typedef long long LL;  
  74. typedef double DB;  
  75. typedef unsigned UINT;  
  76. typedef unsigned long long ULL;  
  77.    
  78. typedef vector<int> VI;  
  79. typedef vector<char> VC;  
  80. typedef vector<string> VS;  
  81. typedef vector<LL> VL;  
  82. typedef vector<DB> VD;  
  83. typedef set<int> SI;  
  84. typedef set<string> SS;  
  85. typedef set<LL> SL;  
  86. typedef set<DB> SD;  
  87. typedef map<intint> MII;  
  88. typedef map<string, int> MSI;  
  89. typedef map<LL, int> MLI;  
  90. typedef map<DB, int> MDI;  
  91. typedef map<intbool> MIB;  
  92. typedef map<string, bool> MSB;  
  93. typedef map<LL, bool> MLB;  
  94. typedef map<DB, bool> MDB;  
  95. typedef pair<intint> PII;  
  96. typedef pair<intbool> PIB;  
  97. typedef vector<PII> VII;  
  98. typedef vector<VI> VVI;  
  99. typedef vector<VII> VVII;  
  100. typedef set<PII> SII;  
  101. typedef map<PII, int> MPIII;  
  102. typedef map<PII, bool> MPIIB;  
  103.    
  104. /** I/O Accelerator **/  
  105.    
  106. /* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */  
  107.    
  108. template<class T> inline void RD(T &);  
  109. template<class T> inline void OT(const T &);  
  110.    
  111. inline int RD(){ int x; RD(x); return x;}  
  112. template<class T> inline T& _RD(T &x){ RD(x); return x;}  
  113. inline void RC(char &c){scanf(" %c", &c);}  
  114. inline void RS(char *s){scanf("%s", s);}  
  115.    
  116. template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}  
  117. template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}  
  118. template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}  
  119. template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}  
  120. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}  
  121. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}  
  122. template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}  
  123. template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}  
  124. template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}  
  125. template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}  
  126. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}  
  127. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}  
  128.    
  129. template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}  
  130. template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}  
  131. template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}  
  132. template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}  
  133. template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}  
  134. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}  
  135. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}  
  136.    
  137.    
  138. template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){  
  139.     while (!Q.empty()) Q.pop();  
  140. }  
  141.    
  142. template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){  
  143.     while (!Q.empty()) Q.pop();  
  144. }  
  145.    
  146. template<class T> inline void CLR(T &A){A.clear();}  
  147. template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}  
  148. template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}  
  149. template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}  
  150. template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}  
  151. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}  
  152. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}  
  153. template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}  
  154. template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}  
  155. template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}  
  156. template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}  
  157. template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}  
  158. template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}  
  159. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}  
  160. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}  
  161.    
  162. template<class T> inline void SRT(T &A){sort(ALL(A));}  
  163. template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}  
  164.    
  165. /** Add - On **/  
  166.    
  167. const int MOD = 1000000007;  
  168. const int INF = 1000000000;  
  169. const DB EPS = 1e-2;  
  170. const DB OO = 1e15;  
  171. const DB PI = 3.14159265358979323846264; //M_PI;  
  172.    
  173. // <<= ` 0. Daily Use .,  
  174.    
  175. template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}  
  176. template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}  
  177. template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}  
  178. template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}  
  179. template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}  
  180. template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}  
  181. template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}  
  182. template<class T> inline T max(T a, T b, T c, T d){return max(min(a, b), max(c, d));}  
  183. template<class T> inline T sqr(T a){return a*a;}  
  184. template<class T> inline T cub(T a){return a*a*a;}  
  185. int Ceil(int x, int y){return (x - 1) / y + 1;}  
  186.    
  187. // <<= ` 1. Bitwise Operation .,  
  188. inline bool _1(int x, int i){return x & 1<<i;}  
  189. inline bool _1(LL x, int i){return x & 1LL<<i;}  
  190. inline LL _1(int i){return 1LL<<i;}  
  191. //inline int _1(int i){return 1<<i;}  
  192. inline LL _U(int i){return _1(i) - 1;};  
  193. //inline int _U(int i){return _1(i) - 1;};  
  194.    
  195.    
  196. template<class T> inline T low_bit(T x) {  
  197.     return x & -x;  
  198. }  
  199.    
  200. template<class T> inline T high_bit(T x) {  
  201.     T p = low_bit(x);  
  202.     while (p != x) x -= p, p = low_bit(x);  
  203.     return p;  
  204. }  
  205.    
  206. inline int count_bits(int x){  
  207.     x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);  
  208.     x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);  
  209.     x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);  
  210.     x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);  
  211.     x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);  
  212.     return x;  
  213. }  
  214.    
  215. inline int count_bits(LL x){  
  216.     x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1);  
  217.     x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2);  
  218.     x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4);  
  219.     x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8);  
  220.     x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16);  
  221.     x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32);  
  222.     return x;  
  223. }  
  224.    
  225. int reverse_bits(int x){  
  226.     x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);  
  227.     x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);  
  228.     x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);  
  229.     x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);  
  230.     x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);  
  231.     return x;  
  232. }  
  233.    
  234. LL reverse_bits(LL x){  
  235.     x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);  
  236.     x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);  
  237.     x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);  
  238.     x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);  
  239.     x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);  
  240.     x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);  
  241.     return x;  
  242. }  
  243.    
  244. // <<= ` 2. Modular Arithmetic Basic .,  
  245.    
  246. inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}  
  247. inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}  
  248. inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}  
  249. inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}  
  250. inline void MUL(int &a, int b){a = (LL)a * b % MOD;}  
  251. inline int pdt(int a, int b){return (LL)a * b % MOD;}  
  252.    
  253. inline int pow(int a, int b){  
  254.     int c = 1;  
  255.     while (b) {  
  256.         if (b&1) MUL(c, a);  
  257.         MUL(a, a), b >>= 1;  
  258.     }  
  259.     return c;  
  260. }  
  261.    
  262. template<class T>  
  263. inline int pow(T a, int b){  
  264.     T c(1);  
  265.     while (b) {  
  266.         if (b&1) MUL(c, a);  
  267.         MUL(a, a), b >>= 1;  
  268.     }  
  269.     return c;  
  270. }  
  271.    
  272. inline int _I(int b){  
  273.     int a = MOD, x1 = 0, x2 = 1, q;  
  274.     while (true){  
  275.         q = a / b, a %= b;  
  276.         if (!a) return (x2 + MOD) % MOD;  
  277.         DEC(x1, pdt(q, x2));  
  278.    
  279.         q = b / a, b %= a;  
  280.         if (!b) return (x1 + MOD) % MOD;  
  281.         DEC(x2, pdt(q, x1));  
  282.     }  
  283. }  
  284.    
  285. inline void DIV(int &a, int b){MUL(a, _I(b));}  
  286. inline int qtt(int a, int b){return pdt(a, _I(b));}  
  287.    
  288. inline int sum(int a, int b, int MOD){  
  289.     a += b; if (a >= MOD) a -= MOD;  
  290.     return a;  
  291. }  
  292.    
  293. inline int phi(int n){  
  294.     int res = n;  
  295.     for (int i=2;sqr(i)<=n;++i) if (!(n%i)){  
  296.         DEC(res, qtt(res, i));  
  297.         do{n /= i;} while(!(n%i));  
  298.     }  
  299.     if (n != 1)  
  300.         DEC(res, qtt(res, n));  
  301.     return res;  
  302. }  
  303.    
  304. // <<= '9. Comutational Geometry .,  
  305.    
  306. struct Po; struct Line; struct Seg;  
  307.    
  308. inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}  
  309. inline int sgn(DB x, DB y){return sgn(x - y);}  
  310.    
  311. struct Po{  
  312.     DB x, y;  
  313.     Po(DB _x = 0, DB _y = 0):x(_x), y(_y){}  
  314.    
  315.     friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}  
  316.     friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}  
  317.    
  318.     friend bool operator ==(Po, Po);  
  319.     friend bool operator !=(Po, Po);  
  320.     friend Po operator +(Po, Po);  
  321.     friend Po operator -(Po, Po);  
  322.     friend Po operator *(Po, DB);  
  323.     friend Po operator /(Po, DB);  
  324.    
  325.     bool operator < (const Po &rhs) const{return sgn(x, rhs.x) < 0 || sgn(x, rhs.x) == 0 && sgn(y, rhs.y) < 0;}  
  326.     Po operator-() const{return Po(-x, -y);}  
  327.     Po& operator +=(Po rhs){x += rhs.x, y += rhs.y; return *this;}  
  328.     Po& operator -=(Po rhs){x -= rhs.x, y -= rhs.y; return *this;}  
  329.     Po& operator *=(DB k){x *= k, y *= k; return *this;}  
  330.     Po& operator /=(DB k){x /= k, y /= k; return *this;}  
  331.    
  332.     DB length_sqr(){return sqr(x) + sqr(y);}  
  333.     DB length(){return sqrt(length_sqr());}  
  334.    
  335.     DB atan(){  
  336.         return atan2(y, x);  
  337.     }  
  338.    
  339.     void input(){  
  340.         scanf("%lf %lf", &x, &y);  
  341.     }  
  342. };  
  343.    
  344. bool operator ==(Po a, Po b){return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}  
  345. bool operator !=(Po a, Po b){return sgn(a.x - b.x) != 0 || sgn(a.y - b.y) != 0;}  
  346. Po operator +(Po a, Po b){return Po(a.x + b.x, a.y + b.y);}  
  347. Po operator -(Po a, Po b){return Po(a.x - b.x, a.y - b.y);}  
  348. Po operator *(Po a, DB k){return Po(a.x * k, a.y * k);}  
  349. Po operator *(DB k, Po a){return a * k;}  
  350. Po operator /(Po a, DB k){return Po(a.x / k, a.y / k);}  
  351.    
  352. struct Line{  
  353.     Po a, b;  
  354.     Line(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}  
  355.     Line(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}  
  356.     Line(Seg);  
  357.    
  358.     friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}  
  359. };  
  360.    
  361. struct Seg{  
  362.     Po a, b;  
  363.     Seg(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}  
  364.     Seg(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}  
  365.     Seg(Line l);  
  366.    
  367.     friend ostream& operator <<(ostream& out, Seg p){return out << p.a << "-" << p.b;}  
  368.     DB length(){return (b - a).length();}  
  369. };  
  370.    
  371. Line::Line(Seg l):a(l.a), b(l.b){}  
  372. Seg::Seg(Line l):a(l.a), b(l.b){}  
  373.    
  374. #define innerProduct dot  
  375. #define scalarProduct dot  
  376. #define dotProduct dot  
  377. #define outerProduct det  
  378. #define crossProduct det  
  379.    
  380. inline DB dot(DB x1, DB y1, DB x2, DB y2){return x1 * x2 + y1 * y2;}  
  381. inline DB dot(Po a, Po b){return dot(a.x, a.y, b.x, b.y);}  
  382. inline DB dot(Po p0, Po p1, Po p2){return dot(p1 - p0, p2 - p0);}  
  383. inline DB dot(Line l1, Line l2){return dot(l1.b - l1.a, l2.b - l2.a);}  
  384. inline DB det(DB x1, DB y1, DB x2, DB y2){return x1 * y2 - x2 * y1;}  
  385. inline DB det(Po a, Po b){return det(a.x, a.y, b.x, b.y);}  
  386. inline DB det(Po p0, Po p1, Po p2){return det(p1 - p0, p2 - p0);}  
  387. inline DB det(Line l1, Line l2){return det(l1.b - l1.a, l2.b - l2.a);}  
  388.    
  389. template<class T1, class T2> inline DB dist(T1 x, T2 y){return sqrt(dist_sqr(x, y));}  
  390.    
  391. inline DB dist_sqr(Po a, Po b){return sqr(a.x - b.x) + sqr(a.y - b.y);}  
  392. inline DB dist_sqr(Po p, Line l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}  
  393. inline DB dist_sqr(Po p, Seg l){  
  394.     Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;  
  395.     if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));  
  396.     else return min(v1.length_sqr(), v2.length_sqr());  
  397. }  
  398.    
  399. inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}  
  400. inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}  
  401.    
  402. inline DB dist_sqr(Line l1, Line l2){  
  403.     if (sgn(det(l1, l2)) != 0) return 0;  
  404.     return dist_sqr(l1.a, l2);  
  405. }  
  406. inline DB dist_sqr(Line l1, Seg l2){  
  407.     Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);  
  408.     return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();  
  409. }  
  410.    
  411. bool isIntersect(Seg l1, Seg l2){  
  412.    
  413.     //if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;  
  414.    
  415.     return  
  416.         min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&  
  417.         min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&  
  418.         min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&  
  419.         min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&  
  420.     sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&  
  421.     sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;  
  422.    
  423. }  
  424.    
  425. inline DB dist_sqr(Seg l1, Seg l2){  
  426.     if (isIntersect(l1, l2)) return 0;  
  427.     else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));  
  428. }  
  429.    
  430. inline bool isOnExtremePoint(const Po &p, const Seg &l){  
  431.     return p == l.a || p == l.b;  
  432. }  
  433.    
  434. inline bool isOnseg(const Po &p, const Seg &l){  
  435.    
  436.     //if (p == l.a || p == l.b) return false;  
  437.    
  438.     return sgn(det(p, l.a, l.b)) == 0 &&  
  439.         sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;  
  440. }  
  441.    
  442. inline Po intersect(const Line &l1, const Line &l2){  
  443.     return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));  
  444. }  
  445.    
  446. // perpendicular foot  
  447. inline Po intersect(const Po & p, const Line &l){  
  448.     return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);  
  449. }  
  450.    
  451. inline Po rotate(Po p, DB alpha, Po o = Po()){  
  452.     p.x -= o.x, p.y -= o .y;  
  453.     return Po(p.x * cos(alpha) - p.y * sin(alpha), p.y * cos(alpha) + p.x * sin(alpha)) + o;  
  454. }  
  455.    
  456. // <<= ' A. Random Event ..  
  457.    
  458. inline int rand32(){return (bool(rand() & 1) << 30) | (rand() << 15) + rand();}  
  459. inline int random32(int l, int r){return rand32() % (r - l + 1) + l;}  
  460. inline int random(int l, int r){return rand() % (r - l + 1) + l;}  
  461. int dice(){return rand() % 6;}  
  462. bool coin(){return rand() % 2;}  
  463.    
  464. // <<= ' 0. I/O Accelerator interface .,  
  465.    
  466. template<class T> inline void RD(T &x){  
  467.     //cin >> x;  
  468.     scanf("%d", &x);  
  469.     //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';  
  470.     //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';  
  471. }  
  472.    
  473. template<class T> inline void OT(const T &x){  
  474.     printf("%d\n", x);  
  475. }  
  476.    
  477. /* .................................................................................................................................. */  
  478.    
  479. const int N = 50009, M = 10009;  
  480.    
  481. const int NN = 2500009;  
  482.    
  483. int l[NN], r[NN], c[NN], total;  
  484.    
  485. PII A[N+M]; int B[N+M], Q[M][3];  
  486. int S[N], C[N], Null;  
  487. int n, m, An, Tn;  
  488.    
  489. #define lx l[x]  
  490. #define rx r[x]  
  491. #define ly l[y]  
  492. #define ry r[y]  
  493. #define cx c[x]  
  494. #define cy c[y]  
  495.    
  496. #define mid ((ll+rr)>>1)  
  497. #define lc lx, ll, mid  
  498. #define rc rx, mid+1, rr  
  499.    
  500.    
  501. void Build(int &x, int ll, int rr){  
  502.     x = ++total; if (ll < rr) Build(lc), Build(rc);  
  503. }  
  504.    
  505. int Insert(int y, int p, int d){  
  506.    
  507.     int x = ++total, root = x;  
  508.    
  509.     c[x] = c[y] + d; int ll = 0, rr = Tn;  
  510.    
  511.     while (ll < rr){  
  512.         if (p <= mid){  
  513.             lx = ++total, rx = ry;  
  514.             x = lx, y = ly, rr = mid;  
  515.         }  
  516.         else {  
  517.             lx = ly, rx = ++total;  
  518.             x = rx, y = ry, ll = mid + 1;  
  519.         }  
  520.         c[x] = c[y] + d;  
  521.     }  
  522.    
  523.     return root;  
  524. }  
  525.    
  526.    
  527. struct Pack{  
  528.     VI L;  
  529.    
  530.     inline Pack(){}  
  531.     inline Pack(int x){L.PB(x);}  
  532.    
  533.     inline void operator += (int x){  
  534.         L.PB(x);  
  535.     }  
  536.    
  537.     inline operator int() const{  
  538.         int res = 0; REP(i, SZ(L)) res += c[l[L[i]]];  
  539.         return res;  
  540.     }  
  541.    
  542.     void lt(){  
  543.         REP(i, SZ(L)) L[i] = l[L[i]];  
  544.     }  
  545.     void rt(){  
  546.         REP(i, SZ(L)) L[i] = r[L[i]];  
  547.     }  
  548.    
  549. };  
  550.    
  551.    
  552.    
  553. void Modify(int x, int p, int d){  
  554.     while (x <= n) C[x] = Insert(C[x], p, d), x += low_bit(x);  
  555. }  
  556.    
  557. Pack Query(int x){  
  558.     Pack res; while (x) res += C[x], x ^= low_bit(x);  
  559.     return res;  
  560. }  
  561.    
  562. int Query(int ll, int rr, int k){  
  563.    
  564.     --ll; Pack a = Query(rr), b = Query(ll), c = S[rr], d = S[ll];  
  565.    
  566.     int t; ll = 0, rr = Tn;  
  567.    
  568.     while (ll < rr){  
  569.         if ((t = a - b + c - d) >= k){  
  570.             a.lt(), b.lt(), c.lt(), d.lt();  
  571.             rr = mid;  
  572.         }  
  573.         else {  
  574.             a.rt(), b.rt(), c.rt(), d.rt();  
  575.             k -= t, ll = mid + 1;  
  576.         }  
  577.     }  
  578.       
  579.     return ll;  
  580. }  
  581.    
  582.    
  583. int main(){  
  584.    
  585. #ifndef ONLINE_JUDGE  
  586.     freopen("in.txt""r", stdin);  
  587.     //freopen("out.txt", "w", stdout);  
  588. #endif  
  589.    
  590. #define key first  
  591. #define id second  
  592.    
  593.     RD(n, m); REP(i, n) A[i] = MP(RD(), i);  
  594.    
  595.     An = n; char cmd; REP(i, m){  
  596.         RC(cmd); if(cmd == 'Q') RD(Q[i][0], Q[i][1], Q[i][2]);  
  597.         else RD(Q[i][0]), Q[i][2] = 0, A[An++] = MP(RD(), An);  
  598.     }  
  599.    
  600.     sort(A, A + An), B[A[0].id] = Tn = 0;  
  601.     FOR(i, 1, An){  
  602.         if(A[i].key != A[i-1].key) A[++Tn].key = A[i].key;  
  603.         B[A[i].id] = Tn;  
  604.     }  
  605.    
  606.     Build(Null, 0, Tn); REP_1(i, n) C[i] = Null;  
  607.    
  608.     S[0] = Null; REP(i, n){  
  609.         S[i+1] = Insert(S[i], B[i], 1);  
  610.     }  
  611.       
  612.     An = n;  
  613.    
  614.     REP(i, m) if (Q[i][2]){  
  615.         OT(A[Query(Q[i][0], Q[i][1], Q[i][2])].key);  
  616.     }else{  
  617.         Modify(Q[i][0], B[Q[i][0]-1], -1);  
  618.         Modify(Q[i][0], B[Q[i][0]-1] = B[An++], 1);  
  619.     }  
  620. }  
  621.    
原文来自:http://blog.csdn.net/metalseed/article/details/8045038
0 0