Poj 2528 Mayor & amp; #39; s posters (dynamic line segment tree)

Source: Internet
Author: User

Poj 2528 Mayor & #39; s posters (dynamic line segment tree)

Portal: Click to open the link


Question:

Given ~ The range of 10000000, and then there are N operations (N <= 10000), the I operation is ~ The r range covers I. How many colors are there in the last one.


Solution:

At first, I thought about discretization, but I thought it was a little troublesome. Then I asked my teammates who worked on data structures. Then he talked about the dynamic line segment tree. The idea is as follows:

Define an ID. Then, the root node 1 indicates the period in charge of the 1-MAXN color. Then every time, it is a dynamic building. When the left subinterval of an interval does not exist. Create it and record the ID of the Left subinterval and right subinterval of each interval.

Finally, we use a dfs set to record the total number of colors.


Note:

When the left subinterval and right subinterval of an interval are updated by it, it must be cleared.

And the maxn. The START is unknown.


#include 
 
  #include 
  
   using namespace std;#define maxn 2000000int lson[maxn],rson[maxn],color[maxn];int cnt,root;void build(){    cnt = 2;    root = 1;    lson[root] = 0;    rson[root] = 0;    color[root] = 0;}void pushdown(int id){    if(!lson[id])    {        lson[id] = cnt++;        lson[lson[id]] = 0;        rson[lson[id]] = 0;        color[lson[id]] = 0;    }    if(!rson[id])    {        rson[id] = cnt++;        lson[rson[id]] = 0;        rson[rson[id]] = 0;        color[rson[id]] = 0;    }    if(color[id])    {        color[lson[id]] = color[id];        color[rson[id]] = color[id];        color[id] = 0;    }}void op(int id,int ls,int rs,int l,int r,int c){    if(ls >= l && rs <= r)    {        color[id] = c;        return;    }    pushdown(id);    int mid = (ls+rs)>>1;    if(l <= mid) op(lson[id],ls,mid,l,r,c);    if(mid < r) op(rson[id],mid+1,rs,l,r,c);}set
   
     ans;void dfs(int id){    if(color[id])    {        ans.insert(color[id]);        return;    }    if(lson[id]) dfs(lson[id]);    if(rson[id]) dfs(rson[id]);}int main(){    int T;    scanf("%d",&T);    for(int ks = 1;ks <= T;ks++)    {        int n;        scanf("%d",&n);        ans.clear();        build();        for(int i = 1;i <= n;i++)        {            int l,r;            scanf("%d %d",&l,&r);            op(root,1,10000000,l,r,i);        }        dfs(root);        printf("%d\n",ans.size());    }    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.