POJ--2528 -- mayor's posters [Line Segment tree + discretization]

Source: Internet
Author: User

A poster is pasted on a wooden board. Each time a poster is attached to a horizontal coordinate range, the poster can be covered in the order it is given, finally, I can see several posters.


They all say this is the starting Question of line tree .... As a result, I still got out of the box, not the online segment tree, but the discretization part.

I saw a very elegant discretization method before, but unfortunately I couldn't find it. This is the case: after deduplication, the next value of each vertex is added to the discrete array (if this value is not used before), which avoids missing the data that is not overwritten in the middle.

In this case, the values of lowwer_bound may be 0, while the value of lowwer_bound is left bounded by 1. Each update will inevitably miss the value of 0, later, I found the update interval and gave them more than 1, and finally passed the example. Then I started re-Xiangxiang. I was sure that the part of the line segment tree was okay. The problem was due to the discretization part. Even so, I still couldn't find where the re was. When I couldn't find it, I simply re-wrote it. When I wrote it to lowwer_bound, I thought that the maximum number of lowwer_bound arrays in the RE Code seemed to be the upper limit of arrays before discretization. OJ took a look at it .. Change to AC

I have two problems with this question: one is that the return value of lowwer_bound may be 0, and the line segment tree starts from 1. You need to set the operation interval to + 1, or set the line segment tree to start from 0. The second is the upper limit of the array in lowwer_bound. It is written before discretization, and no error is found in the RE.

#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 2000010#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131//#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int n;int x[MAXN];int sum[MAXN<<2];int l[MAXN],r[MAXN];map<int,int>mp;int ans;void pushdown(int rt){    if(sum[rt]){        sum[rt<<1] = sum[rt<<1|1] = sum[rt];        sum[rt] = 0;    }}void build(int l,int r,int rt){    sum[rt] = 0;    if(l==r)    return ;    int m = (l+r)>>1;    build(lson);    build(rson);}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R){        sum[rt] = c;        return ;    }    pushdown(rt);    int m = (l+r)>>1;    if(L<=m)    update(L,R,c,lson);    if(R>m)     update(L,R,c,rson);}void query(int L,int R,int l,int r,int rt){    if(l==r){        if(sum[rt]&&mp[sum[rt]]!=0){            ans++;        }        mp[sum[rt]] = 0;        sum[rt] = 0;        return ;    }    pushdown(rt);    int m = (l+r)>>1;    if(L<=m)    query(L,R,lson);    if(R>m)     query(L,R,rson);}int main(){    int t,i,m;    scanf("%d",&t);    while(t--){        ans = 0;        m = 0;        if(!mp.empty()) mp.clear();        scanf("%d",&n);        if(n==0)    continue;        for(i=0;i<n;i++){            scanf("%d%d",&l[i],&r[i]);            x[m++] = l[i];            x[m++] = r[i];        }        sort(x,x+m);        int xl = unique(x,x+m) - x;        x[xl++] = x[xl-1] + 1;        for(i=xl-1;i>0;i--){            if(x[i]!=x[i-1]+1)  x[xl++] = x[i-1] + 1;        }        sort(x,x+xl);        build(1,xl,1);        for(i=0;i<n;i++){            int L = lower_bound(x,x+xl,l[i]) - x;            int R = lower_bound(x,x+xl,r[i]) - x;            update(L+1,R+1,i+1,1,xl,1);            mp[i+1] = 1;        }        query(1,xl,1,xl,1);        printf("%d\n",ans);    }    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.