Segment tree + discretized POJ 2528 Mayor ' s posters

Source: Internet
Author: User

Topic Portal

Test instructions: Put a poster on one wall, in order, and ask how many different posters are in the end (those that are not covered or just partially covered)

Analysis: This data range is very large, directly engaged in time-out + hyper-memory, need discretization: discretization is simply to take the values we need to use, such as interval [1000,2000],[1990,2012] We do not use [-∞,999][1001,1989][ 1991,1999][2001,2011][2013,+∞] These values, so I just need to 1000,1990,2000,2012 is enough, to map it to 0,1,2,3, the complexity is greatly lowered, so discretization to save all the required values, Once sorted, they are mapped to 1~n, which makes the complexity much smaller. The difficulty of this problem is that each number actually represents a unit length (not a point), so that ordinary discretization will cause many errors (including my previous code, POJ the data is very weak). The following two simple examples should be presented to illustrate the drawbacks of common discretization:

Example one: 1-10 1-4 5-10
Example two: 1-10 1-4 6-10
Common discretization has become a [1,4][1,2][3,4]

Match (example one):
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. --copy from Notonlysuccess
Harvesting: discretization Techniques

Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 |    1const int N = 1e4 + 10;const int INF = 0x3f3f3f3f;int ans;struct ST {int col[n<<4];    BOOL Vis[n];        void init (void) {memset (col,-1, sizeof (COL));    Memset (Vis, false, sizeof (VIS));            } void Push_down (int rt) {if (Col[rt]! =-1) {col[rt<<1] = col[rt<<1|1] = Col[rt];        COL[RT] =-1;            }} void Updata (int ql, int qr, int c, int l, int r, int rt) {if (QL <= l && r <= qr) {    COL[RT] = c;        return;        } push_down (RT);        int mid = (L + r) >> 1;        if (QL <= mid) Updata (QL, QR, C, Lson);    if (QR > Mid) updata (QL, QR, C, Rson); } void query (int l, int r, int rt) {if (Col[rt]! =-1) {if (!vis[col[rt]]) {ans  ++; vis[COL[RT]] = true;        } return;        } if (L = = r) return;        int mid = (L + r) >> 1;        Query (Lson);    Query (Rson);   }}st;int L[n], r[n];int x[n<<2];int main () {int T, N;        scanf ("%d", &t), while (T--) {scanf ("%d", &n);        int tot = 0;            for (int i=0; i<n; ++i) {scanf ("%d%d", &l[i], &r[i]);            x[tot++] = L[i];        x[tot++] = R[i];        } sort (X, x+tot); int k = 1;        for (int i=1; i<tot; ++i) {if (X[i]! = X[i-1]) x[k++] = X[i];        } for (int i=k-1; i>=1;-i) {if (X[i]! = X[i-1] + 1) x[k++] = x[i-1] + 1;        }sort (X, x+k);        St.init ();            for (int i=0; i<n; ++i) {int QL = Lower_bound (x, X+k, L[i])-X;            int qr = Lower_bound (x, X+k, R[i])-X;        St.updata (QL, QR, I, 0, K, 1); }ans = 0;st.query (0, K, 1);p rintf ("%d\n", ans); return 0;}

  

Segment tree + discretized POJ 2528 Mayor ' s posters

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.