Poj 2528-mayor's posters

Source: Internet
Author: User

Question address: http://poj.org/problem? Id = 2528

 

 

PS: ActuallyCodeStill very watery. There is a place to optimize but I don't know how to optimize it.

 

This questionReMany times, it seems that the question data is out of scope? But I useIf (n> maxn) while (1 );And does notTle ~~
My scope is actually enabled.4 * maxn... Later8 * maxn(It is strange that after the following error is found, it cannot be corrected.ReNow~~Bytes~
)

 

However, this is still true for wa ~~ Read discuss , finally, a set of error data is found. ~~~ the result is discretization ~~

Because it is covered by line segments, it is overwritten in order.3Line Segments1 ~ 10,1 ~ 4
,7 ~ 10, Where5 ~ 6Is the first line segment. After discretization1 ~ 4,1 ~ 2,3 ~ 4Therefore, the first line segment is completely covered.~~~~~Yan cried~~

 

 

 

Previously, I thought that the method was awkward, so I wrote another one. There are two methods.

 

Method 1: Each line segment hasVal. Overwrite a line segment. Each update overwrites the repeated parts of a previously overwritten line segment.Val. At the end of the calculation, the number of different queriesVal.

Method 2: Each line segment stores a length. Each update marks the interval as the label of the updated line segment. In the next update, if there is a duplicate part, find the existing line segment label of the duplicate part, and subtract the length of the Line Segment represented by this label from the length of this repetition interval. If there is still a remaining length, it indicates that it is not completely overwritten.

In the final statistics, you only need to find the numberLenGreater0.

 

 

 

 

 

// Method 1: # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define LL (v <1) # define RR (v <1 | 1) # define tmid (L + r)> 1) Using namespace STD; const int maxn = 11000; int flag [maxn * 16]; bool vis [maxn]; int N, NN, CNT, sum; int s [maxn * 2], Res [maxn * 2], dir [maxn * 2]; int CMP (int A, int B) {return s [a] <s [B];} void deal () {int I; nn = n <1; for (I = 0; I <nn; I ++) dir [I] = I; sort (Dir, Dir + NN, CMP ); // for (I = 0; I <nn; I ++) cout <Dir [I] <""; cout <Endl; Res [dir [0] = CNT = 1; for (I = 1; I <nn; I ++) if (s [dir [I] = s [dir [I-1]) RES [dir [I] = CNT; else if (s [dir [I]> S [dir [I-1] + 1) RES [dir [I] = (CNT + = 2 ); else res [dir [I] = (CNT + = 1); // for (I = 0; I <n; I ++) cout <res [I] <"" <res [I + N] <Endl; memset (VIS, false, sizeof (VIS ));} void make_tree (int l, int R, int v) {flag [v] =-1; if (L = r) return; make_tree (L, tmid, LL ); make_tree (tmid + 1, R, RR);} void Update (INT Val, int Li, I NT Ri, int L, int R, int v) {If (LI> r | RI <L) return; If (LI <= L & R <= RI) {flag [v] = val; return;} If (L = r) while (1); If (flag [v]! =-2) {flag [ll] = Flag [RR] = Flag [v]; flag [v] =-2;} Update (Val, Li, RI, tmid + 1, R, RR); Update (Val, Li, RI, L, tmid, LL);} int query (int l, int R, int V) {If (flag [v] =-1) return 0; If (flag [v]> = 0) {If (vis [flag [v]) return 0; vis [flag [v] = true; return 1;} return query (L, tmid, LL) + query (tmid + 1, R, RR);} int main () {int T, I; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 0; I <n; I ++) scanf ("% d", & S [I], & S [n + I]); Deal (); make_tree (1, CNT, 1); for (I = 0; I <n; I ++) Update (I, Res [I], Res [I + N], 1, CNT, 1); printf ("% d \ n", query (1, CNT, 1);} return 0 ;} /* 335 64 56 831 101 36 1051 42 68 103 47 10 */

 

 

 

// Method 2: # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define LL (v <1) # define RR (v <1 | 1) # define tmid (L + r)> 1) Using namespace STD; const int maxn = 11000; int flag [maxn * 16]; int Len [maxn]; int N, NN, CNT, sum; int s [maxn * 2], Res [maxn * 2], dir [maxn * 2]; int CMP (int A, int B) {return s [a] <s [B];} void deal () {int I; nn = n <1; for (I = 0; I <nn; I ++) dir [I] = I; sort (Dir, Dir + NN, CMP ); // for (I = 0; I <nn; I ++) cout <<Dir [I] <"; cout <Endl; Res [dir [0] = CNT = 1; for (I = 1; I <nn; I ++) if (s [dir [I] = s [dir [I-1]) RES [dir [I] = CNT; else if (s [dir [I]> S [dir [I-1] + 1) RES [dir [I] = (CNT + = 2 ); else res [dir [I] = (CNT + = 1); // for (I = 0; I <n; I ++) cout <res [I] <"" <res [I + N] <Endl; memset (Len, 0, sizeof (LEN ));} void make_tree (int l, int R, int v) {flag [v] =-1; if (L = r) return; make_tree (L, tmid, LL ); make_tree (tmid + 1, R, RR);} void Update (INT Val, int Li, int ri, Int L, int R, int v) {If (RI <L | LI> r) return; If (LI <= L & R <= RI) {If (flag [v]> = 0) Len [flag [v]-= r-L + 1; else if (flag [v] =-2) {Update (Val, Li, RI, L, tmid, LL); Update (Val, Li, RI, tmid + 1, R, RR );} flag [v] = val; return;} If (flag [v]! =-2) {flag [ll] = Flag [RR] = Flag [v]; flag [v] =-2;} Update (Val, Li, RI, l, tmid, LL); Update (Val, Li, RI, tmid + 1, R, RR);} int main () {int T, I; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); for (I = 0; I <n; I ++) scanf ("% d", & S [I], & S [n + I]); Deal (); make_tree (1, CNT, 1 ); for (I = 0; I <n; I ++) Update (I, Res [I], Res [I + N], 1, CNT, 1 ), len [I] = res [I + N]-res [I] + 1; for (sum = 0, I = 0; I <n; I ++) if (LEN [I]> 0) sum ++; printf ("% d \ n", sum);} 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.