Poj-2585-mayor ' s posters-update + (hash) discretization idea

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2528

This topic is a very classical discretization to update the segment tree problem, you can try to do more times, fully understand;

Transferred from: http://www.notonlysuccess.com/index.php/segment-tree-complete/

Test instructions: Posters on the wall, posters can cover each other, ask to see a few posters at the end
Idea: This data range is very large, directly engage in Timeout + hyper-memory, need discretization:
Discretization is simply to use the values we need , such as interval [1000,2000],[1990,2012] We don't use [-∞,999][1001,1989][1991,1999][2001,2011][2013, +∞] These values, so I just need to 1000,1990,2000,2012 enough, to map it to 0,1,2,3, the complexity is greatly lowered
So discretization to save all the values that need to be used, after sorting, mapping to 1~n, so the complexity will be much smaller
The difficulty of this problem is that each number actually represents a unit length (and a point), so that ordinary discretization will cause a lot of errors (including my previous code, POJ This problem data is very weak)
The following two simple examples should be presented to illustrate the drawbacks of common discretization:
1-10 1-4 5-10
1-10 1-4 6-10
To solve this flaw, we can add some processing to the sorted array, for example [1,2,6,10]
If the adjacent digit spacing is greater than 1, add any number to it, such as add [1,2,3,6,7,10], and then do the line segment tree.
Segment tree Feature: Update: Segment replace query: Simple hash

Well, now this code can only over POJ data, not really perfect ... I do not know why has been re, welcome guidance;

#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <cmath> #include <stack> #include <set> #include <vector>#  Include<algorithm> #define LL Long longusing namespace std;//According to the question array open 10005, to report re, pit ah; int pos[100005][2];         For Discretization, the left and right nodes of the interval after the discretization; bool Vis[100005];int ans,mm;        Ans is used to count the final structure, MM for the first few posters, struct node1//for discretization of the structure; {int p,n; P storage node, n Save the number of posters;}line[100005<<1];struct node2//segment tree structure; {int l,r,c;} Node[100005<<2];bool cmp (node1 x,node1 y)//increment sort by left node; {return X.P&LT;Y.P;}    void build (int l,int r,int RT)//achievements; {node[rt].l=l;    Node[rt].r=r;           node[rt].c=0;        staining marks; if (l!=r) {int mid= (L+R) >>1;        Build (l,mid,rt<<1);    Build (mid+1,r,rt<<1|1);        }}void Insert (int l,int r,int c,int RT)//update; {if (node[rt].l==l&&node[rt].r==r) {node[rt].c=c; Return    } if (node[rt].c>0&&node[rt].c!=c) {//Description to update the next node, equivalent to the pushdown I wrote earlier;        node[rt<<1].c=node[rt].c;        node[rt<<1|1].c=node[rt].c;    node[rt].c=0;    } int mid= (NODE[RT].L+NODE[RT].R) >>1;    if (r<=mid) Insert (l,r,c,rt<<1);    else if (l>mid) Insert (l,r,c,rt<<1|1);        else{Insert (l,mid,c,rt<<1);    Insert (mid+1,r,c,rt<<1|1); }}void query (int rt)//interval lookup; {if (node[rt].c!=0) {if (!vis[node[rt].c]) {//stats are dyed how many different Yan            color; ans++;        Vis[node[rt].c]=true;    } return;    } query (rt<<1); Query (rt<<1|1);}    int main () {int t,n,a,b;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);    for (int i=0;i<n;i++) {scanf ("%d%d", &a,&b);            The length of a A/b interval is discretized into a smaller interval line[i<<1].p=a;            line[i<<1|1].p=b;    line[i<<1].n=-(i+1); The left node is represented by a negative number;ine[i<<1|1].n=i+1;    The right node is represented by a positive number;} sort (line,line+2*n,cmp); sort; int temp=line[0].p;        Mm=1;                for (int i=0;i<2*n;i++) {if (line[i].p!=temp) {mm++;            TEMP=LINE[I].P;   }//if (I>0&&line[i].p> (line[i-1].p+1)) mm++;            This statement is actually I want to solve the discretization bug, but the result has been re, resulting in only the weaker data poj; if (line[i].n<0) pos[-line[i].n-1][0]=mm;        else pos[line[i].n-1][1]=mm;        } build (1,mm,1);        for (int i=0;i<n;i++) {Insert (pos[i][0],pos[i][1],i+1,1);        } memset (Vis,false,sizeof (VIS));        ans=0;        Query (1);    printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj-2585-mayor ' s posters-update + (hash) discretization idea

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.