Http://poj.org/problem? Id = 2828 reverse sequence of the Line Segment tree

Source: Internet
Author: User

The Nima line segment tree has finally made some progress and is decisive. I have never known what to record with the line segment tree in specific problems. It seems that there are too few questions I have done, I have seen very few types. I have to work hard in the future. This time I have to finish the line tree album at home .... Weak.

At the beginning of this question, I did not know what the line segment tree recorded, how to use the line segment tree to express it, or how to do it. I later read the report of other people's problem solving and understood it for a long time, only then can we understand that we have to work hard and think more;

Because this question is a matter of queuing, the final result will be known only after all the information is entered. The line segment tree is used to indicate the number of gaps in the recorded range, and then the last one is inserted;

#include<cstdio>#include<algorithm>#include<cmath>#include<string.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1 | 1const int N=200002;int num[N<<2];int res[N<<2];void PushUp(int rt){ num[rt]=num[rt<<1]+num[rt<<1 | 1];}void build(int l,int r,int rt){  res[rt]=0;   if(l==r){num[rt]=1;return;}   int mid=(l+r)>>1;  build(lson);  build(rson);   PushUp(rt);}void update(int p,int val,int l,int r,int rt){    if(l==r)    {num[rt]--;     res[l]=val;return;    }    int mid=(l+r)>>1;    if(num[rt<<1]>=p)update(p,val,lson);    else update(p-num[rt<<1],val,rson);   PushUp(rt);}pair<int,int>a[N];int main(){ int n,i,val;  while(~scanf("%d",&n))  {     build(1,n,1);     for(int i=0;i<n;i++)     {      scanf("%d%d",&a[i].first,&a[i].second);     } for(int i=n-1;i>=0;i--)        {         update(a[i].first+1,a[i].second,1,n,1);        }       printf("%d",res[1]);     for(int i=2;i<=n;i++)      { printf(" %d",res[i]);}      printf("\n");  } return 0;}

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.