现在的位置: 首页 > 综合 > 正文

hdu2665 可持续化线段树

2018年04月12日 ⁄ 综合 ⁄ 共 3402字 ⁄ 字号 评论关闭
分类: 数据结构 

 可持续化线段树。
我是看这个看懂的http://hi.baidu.com/wyl8899/item/e00796a9cb2df73d020a4d68


可持续化线段树,主要思想就是利用历史信息,减少时间和内存花销。
比方有两棵线段树,但是他们只有一个节点信息不同。仔细一想,在这两颗线段树上,对应的 [l,r]节点  只有log(n)个节点不同。那么,除了不同的节点,其他节点信息,他们完全可以共用。


关于这题解法,我就复制别人的话了(  好懒啊 o(╯□╰)o )

考虑把权值离散化,然后用线段树解决问题。
对于一个询问(x,y,k),如果已经把a[x]..a[y]建成了一颗线段树,并维护区间和,显然二分就可以了。
(怎么二分? ...看左儿子有没有k个然后递归 自己YY一下就出来了)
但是很遗憾我们没办法这么搞...这还不如裸奔...
接下来思考一个问题。
记T[x,y]为a[x]..a[y]建成的线段树,那么T[x,y]和T[1,x-1],T[1,y]有什么关系?
对于T[x,y]中的一个节点[L,R],我们一定可以在T[1,x-1]和T[1,y]中找到也代表[L,R]的对应节点。
考察这三个节点存储的区间和的关系,马上可以发现:
T[x,y]中任意节点[L,R]的区间和等于T[1,y]中对应节点的区间和减去T[1,x-1]中对应节点的区间和。
这就好办多了,我们只需要把所有的T[1,x](1<=x<=N)建出来了就可以了。
......
发现什么问题没有? 空间和时间都不允许你建N棵线段树的。
函数式数据结构的思想这时候派上用场了。
考察T[1,x]和T[1,x-1]——在T[1,x-1]加入a[x]就得到T[1,x]了。
单点修改的时候要改变信息的节点的数量是O(logn)。
于是我们"大胆地重用以前的信息",只新建这些节点,然后这些节点的左右儿子可以指向前一棵树的节点。
最终的总节点个数是O(nlogn),预处理复杂度O(nlogn),回答每个询问O(logn),常数还是有一点的。


  1. //#pragma comment(linker, "/STACK:102400000,102400000")  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<vector>  
  5. #include<queue>  
  6. #include<cmath>  
  7. #include<cctype>  
  8. #include<string>  
  9. #include<algorithm>  
  10. #include<iostream>  
  11. #include<ctime>  
  12. #include<map>  
  13. #include<set>  
  14. using namespace std;  
  15. #define MP(x,y) make_pair((x),(y))  
  16. #define PB(x) push_back(x)  
  17. typedef long long LL;  
  18. //typedef unsigned __int64 ULL;  
  19. /* ****************** */  
  20. const int INF=1000111222;  
  21. const double INFF=1e200;  
  22. const double eps=1e-8;  
  23. const LL mod=1000000007;  
  24. const int NN=100010;  
  25. const int MM=100010;  
  26. /* ****************** */  
  27.   
  28. int a[NN],tf[NN],root[NN];  
  29. struct TR  
  30. {  
  31.     int ls,rs,sum;  
  32. }tr[NN*20];  
  33. int cnt;  
  34.   
  35. int build(int l,int r)  
  36. {  
  37.     int id=++cnt;  
  38.     tr[id].sum=0;  
  39.     if(l!=r)  
  40.     {  
  41.         int mid=(l+r)>>1;  
  42.         tr[id].ls=build(l,mid);  
  43.         tr[id].rs=build(mid+1,r);  
  44.     }  
  45.     return id;  
  46. }  
  47.   
  48. void push_up(int R)  
  49. {  
  50.     tr[R].sum=tr[ tr[R].ls ].sum + tr[ tr[R].rs ].sum;  
  51. }  
  52.   
  53. int update(int x,int pre,int l,int r)  
  54. {  
  55.     int id=++cnt;  
  56.     tr[id].ls=tr[pre].ls;  
  57.     tr[id].rs=tr[pre].rs;  
  58.     tr[id].sum=tr[pre].sum;  
  59.     if(l==r)  
  60.     {  
  61.         tr[id].sum++;  
  62.         return id;  
  63.     }  
  64.     int mid=(l+r)>>1;  
  65.     if(x<=mid)  
  66.         tr[id].ls=update(x,tr[pre].ls,l,mid);  
  67.     else  
  68.         tr[id].rs=update(x,tr[pre].rs,mid+1,r);  
  69.     push_up(id);  
  70.     return id;  
  71. }  
  72.   
  73. int query(int id1,int id2,int k,int l,int r)  
  74. {  
  75.     if(l==r)  
  76.         return l;  
  77.     int x=tr[ tr[id2].ls ].sum;  
  78.     x-=tr[ tr[id1].ls ].sum;  
  79.     int mid=(l+r)>>1;  
  80.   
  81.   //  printf("now==[%d,%d]\n",l,r);  
  82.    // printf("[%d,%d] have %d\n",l,mid,x);  
  83.   
  84.     if(x>=k)  
  85.         return query(tr[id1].ls,tr[id2].ls,k,l,mid);  
  86.     else  
  87.         return query(tr[id1].rs,tr[id2].rs,k-x,mid+1,r);  
  88. }  
  89.   
  90. int main()  
  91. {  
  92.     int cas,n,m;  
  93.     int i,tol,x,st,en,k;  
  94.     scanf("%d",&cas);  
  95.     while(cas--)  
  96.     {  
  97.         scanf("%d%d",&n,&m);  
  98.         for(i=1;i<=n;i++)  
  99.         {  
  100.             scanf("%d",&a[i]);  
  101.             tf[i]=a[i];  
  102.         }  
  103.         sort(tf+1,tf+1+n);  
  104.         tol=unique(tf+1,tf+1+n)-tf-1;  
  105.         cnt=0;  
  106.         root[0]=build(1,tol);  
  107.   
  108.         for(i=1;i<=n;i++)  
  109.         {  
  110.             x=lower_bound(tf+1,tf+1+tol,a[i])-tf;  
  111.             root[i]=update(x,root[i-1],1,tol);  
  112.          //   printf("(%d,%d)%c",a[i],x,i==n?'\n':' ');  
  113.         }  
  114.         while(m--)  
  115.         {  
  116.             scanf("%d%d%d",&st,&en,&k);  
  117.             x=query(root[st-1],root[en],k,1,tol);  
  118.             printf("%d\n",tf[x]);  
  119.         }  
  120.     }  
  121.     return 0;  
  122. }  

抱歉!评论已关闭.