poj_2528 Mayor ' s poster (segment tree + discretization)

Source: Internet
Author: User

Please point me in the question.
The following:
This problem compared with the previous topic is focused on a mapping of pre-processing, the topic to give the interval of 10000000, and at most only 10,000 points, if the direct contribution of the words are too empty. The left and right nodes of these intervals correspond to a maximum of 4x10000 points, which is much smaller than the previous 10000000, and the correspondence between the intervals will not change.

As an example:
Interval: [2,6],[4,8],[6,10]
We carry out the following correspondence:
2 4 6) 8 10
1 2 3) 4 5
The original interval becomes [1,3],[2,4],[3,5]. You can see that the coverage relationship between them doesn't change, but it's a lot more compact.
But note that after the corresponding, there should be a de-heavy operation, to prevent errors.

There is a point to note that the correspondence of the interval is slightly less aware of the situation where color loss occurs, as follows:
[1,10],–[1,4],–[6,10]
When we hand-simulation will find that we are just 1,4,6,10, the corresponding, that is, 1,2,3,4, the original set of 4,6 is considered to be adjacent elements, so 5 color loss, and finally got the wrong result.
In order to prevent this from happening, we interpolate and insert extraneous values between two nonadjacent nodes, but we can avoid this situation effectively.

Array range analysis, 10,000 intervals corresponding to a maximum of 20,000 nodes, and then interpolated to get up to 40000+ nodes, corresponding to the Segtree array reached 4x40000+.

And the segment tree part of the implementation is relatively simple, lazy tag, the last statistic still visible in different colors can be.

Reference Blog

Code implementation:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#define MAX 20010#define Lchild Root<<1,l,mid#define Rchild Root<<1|1,mid+1,r#define MID (x, y) (x+y) >>1using namespace STD;intTintNintmm[max<<2];intshow[max<<3];intL[max],r[max];intsegtree[max<<3];voidPushdown (introot);intQueryintRootintLintRintAintb);voidUpdateintRootintLintRintAintBintFlag);intMain () {scanf("%d", &t); while(t--) {scanf("%d", &n);intpos =1;intTMP =2;//Note show[0] to mark 1 in advance        memset(Segtree,0,sizeof(Segtree));memset(Show,0,sizeof(show)); show[0] =1; for(inti =1; I <= N; i++) {scanf("%d%d", &l[i],&r[i]);            mm[pos++] = L[i];        mm[pos++] = R[i]; }//de-re-operationSort (mm+1, Mm+pos); for(inti =2; I < POS; i++) {if(Mm[i]! = mm[i-1]) {mm[tmp++] = Mm[i]; }        }//InterpolationPOS = tmp; for(inti = pos-1; i >1; i--) {if(Mm[i]! = mm[i-1]+1) {mm[pos++] = mm[i-1]+1; }} sort (mm+1, Mm+pos);//lowe_bound Find a B location         for(inti =1; I <= N; i++) {intb; A = Lower_bound (mm+1, Mm+pos,l[i])-mm; b = Lower_bound (mm+1, Mm+pos,r[i])-mm; Update1,1, pos-1, a,b,i); }intres = query (1,1, pos-1,1, pos-1);printf("%d\n", res); }return 0;}voidUpdateintRootintLintRintAintBintFlag) {if(A > R | | b < l) {return; }if(A <= l && R <= B) {Segtree[root] = flag;return; }if(Segtree[root]! =0) {pushdown (root); }intMid = Mid (l,r);    Update (LCHILD,A,B,FLAG); Update (RCHILD,A,B,FLAG);return;}intQueryintRootintLintRintAintb) {if(A > R | | b < l) {return 0; }//The interval is of the same color, and show array tag has been counted    if(segtree[root]!=0){if(Show[segtree[root]]! =1) {Show[segtree[root]] =1;return 1; }return 0; }//non-marked leaf node    if(L = = r) {return 0; }intMid = Mid (l,r);returnQuery (lchild,a,b) +query (rchild,a,b);}voidPushdown (intRoot) {segtree[root<<1] = Segtree[root]; segtree[root<<1|1] = Segtree[root]; Segtree[root] =0;return;}

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

poj_2528 Mayor ' s poster (segment tree + discretization)

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.