[Data structure-segment tree] POJ 2528

Source: Internet
Author: User

Put a poster on one wall, the order of the stickers given, the number of posters to be seen at the end.

Pure line Tree simulation problem.

But the data range gives 10^7, hyper-memory.

In fact, here is a small trick, although the width of the wall is very large, but the number of posters is only 10000, so the number of 10^7 really few, so that only the number of useless to "delete", the remaining number from small to large mapping for the new number, so that the space complexity is greatly reduced.

For example, a sample of the topic:

1 42 68 103 47 10
Number of uses: 1 2 3 4 6 7 8 10
You can map them to:
1 2 3 4 6 7 8 10
1 2 3 4 5 6 7 8

This will only open to 8 when the achievement of the line.

#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespacestd;Const intmaxn=20010;intseg[maxn*2];//the node number of the segment tree starts at 0. intlnext[maxn*2],rnext[maxn*2];intl[maxn*2],r[maxn*2];inttot;intBuildtree (intllintRR) {    intcur=tot++; L[cur]=ll; R[cur]=RR; if(ll==RR) {Seg[cur]=0; //printf ("%d**\n", Seg[cur]);lnext[cur]=rnext[cur]=-1; returncur; }    intMid= (LL+RR) >>1; Lnext[cur]=Buildtree (Ll,mid); Rnext[cur]=buildtree (mid+1, RR); Seg[cur]=0; returncur;}BOOLAddintCurintllintRrintval) {    if(rnext[cur]!=-1&&lnext[cur]&&seg[rnext[cur]]&&Seg[lnext[cur]]) {Seg[cur]=1; }    if(Seg[cur])return false; if(ll>RR) swap (LL,RR); if(l[cur]==ll&&r[cur]==RR) {Seg[cur]=Val; return true; }    BOOLs1=0, s2=0; intMID = (L[cur]+r[cur]) >>1; if(ll>=mid+1) S1=Add (Rnext[cur],ll,rr,val); Else if(rr<=mid) S2=Add (Lnext[cur],ll,rr,val); Else{S1=Add (Lnext[cur],ll,mid,val); S2=add (rnext[cur],mid+1, Rr,val); }    if(rnext[cur]!=-1&&lnext[cur]&&seg[rnext[cur]]&&Seg[lnext[cur]]) {Seg[cur]=1; }    returns1| |S2;}intdata[10005][2];inty[20005],cur,ying[10000002];intMain () {intT,n,ans; scanf ("%d",&T);  while(T--&AMP;&AMP;SCANF ("%d", &n)! =EOF) {ans=0; cur=1; tot=0;  for(intI=0; i<n;i++) {scanf ("%d%d", &data[i][0],&data[i][1]); Y[cur++]=data[i][0]; Y[cur++]=data[i][1]; } y[0]=-1; Sort (y+1, y+cur); intCc=1;  for(intI=1; i<cur;i++) {            if(y[i]==y[i-1])Continue; Ying[y[i]]=cc++; } buildtree (1, cc-1);  for(inti=n-1; i>=0; i--) {data[i][0]=ying[data[i][0]]; data[i][1]=ying[data[i][1]]; if(Add (0, data[i][0],data[i][1],1)) ans++; } printf ("%d\n", ans); }}

[Data structure-segment tree] POJ 2528

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.