"POJ2104" k-th number chairman tree? Functional line-segment tree? Can I persist a line tree? ...... It's one of them anyway.

Source: Internet
Author: User

Test instructions: Interval static section K large.

Exercises

The line tree can be persisted.


To persist a segment tree:

Basic idea: We maintain the segment tree after inserting each node.

Plain Writing (mle+tle) we copy a segment tree and then insert it for each insertion, which guarantees "persistence."

but obviously, time complexity and spatial complexity are all n^2. 233.

So there's an optimization: we find that only LOGN nodes are changed each time, so we just need to create these points and point to the original version of the node.

Spatial complexity Nlogn.


Then the problem is the interval k large, for the interval [a, b], inserted into the segment tree, the node size-(A-1) Time point size, is obviously [a, b] in this interval after inserting the value of the result, according to this can be queried.

The code is too water, do not understand to see the code yourself.


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 101000# Define LOGN 20#define inf 0x3f3f3f3f#define ls s[x].son[0] #define RS s[x].son[1]using namespace Std;int pos[n],n,m;int src [N],rank;int crs[n];struct lsh{int X,id;bool operator < (const LSH &a) Const{return x<a.x;}} Lsh[n];int cnt;struct functional_segment_tree{int son[2];int size,l,r;} s[n*logn];inline void pushup (int x) {s[x].size=s[ls].size+s[rs].size;} int build (int last,int l,int r,int d) {int x=++cnt;s[x].size=s[last].size,s[x].l=l,s[x].r=r;if (l==r) {s[x].size++; return x;} int mid=l+r>>1;if (D&LT;=MID) ls=build (s[last].son[0],l,mid,d), Rs=s[last].son[1];else rs=build (s[last].son[1 ],MID+1,R,D), ls=s[last].son[0];p ushup (x); return x;} void query (int last,int now,int l,int r,int k) {if (l==r) {printf ("%d\n", Crs[l]); return;} int mid=l+r>>1,temp=s[s[now].son[0]].size-s[s[last].son[0]].size;if (temp<k) query (S[last].son[1],s[now] . son[1],mid+1,r,k-temp); else Query (S[laST].SON[0],S[NOW].SON[0],L,MID,K);} int main () {//freopen ("test.in", "R", stdin), int i,a,b,c;scanf ("%d%d", &n,&m), for (i=1;i<=n;i++) scanf ("%d" , &lsh[i].x), Lsh[i].id=i;sort (lsh+1,lsh+n+1); Src[lsh[1].id]=++rank,crs[rank]=lsh[1].x;for (i=2;i<=n;i++) { if (lsh[i].x!=lsh[i-1].x) Crs[++rank]=lsh[i].x;src[lsh[i].id]=rank;} for (i=1;i<=n;i++) Pos[i]=build (Pos[i-1],1,rank,src[i]), while (m--) {scanf ("%d%d%d", &a,&b,&c); query (pos[a-1],pos[b],1,n,c);} return 0;}



"POJ2104" k-th number chairman tree? Functional line-segment tree? Can I persist a line tree? ...... It's one of them anyway.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.