POJ 2528 Mayor ' s posters

Source: Internet
Author: User
Tags hash

N (n<=10000) individuals posted posters in turn, giving each poster the range Li,ri (1<=li<=ri<=10000000). Find out how many posters you can see at the end. Input First line: sample number t The second line: the person who posted the poster n The third line: the scope of each person to post the poster next n lines: Each person posts the scope of the poster output for each input, outputs the number of posters that can be seen at the end. The following figure is a sample explanation
Sample Input

1
5
1 4
2 6
8 ten
3 4
7 10
Sample Output
4

Just learn the line segment tree, always thought that the line segment tree is only the problem of interval, condom template on the line, but this problem sets, took two days to write out.   The first segment tree most solved is the 1E4~1E6 data range, but this problem range is too large, the violence is not timeout is super memory, so can only be discretized processing. How can discretization be more reasonable? Because the range of the subject is too large to use the line segment tree, we can make a fuss over the narrowing interval. For example: 1 4, 5 10, these two intervals, we can let these four numbers, one for each number to represent them, do not want to be sure the subscript is the most appropriate, so 1 corresponds to 4, 3 corresponds to 5, 4 corresponds to 10, the interval corresponds to 1 2,3 4; So discretization is not over. Because there is such a situation such as: 1 10,1 4,6 10, according to just that correspondence, the interval corresponds to, 1 4,1 2,3 4, after the corresponding can only see two colors, but this situation can see three colors, because there is a color between 4~6, but because of discretization to neglect, The solution is that we can add another interval of 5 between the 4~6, so that 5 represents a color, not overlooked. For specific discretization, see the following code analysis:

		First, all the endpoints are sorted into coordinates, 
		int t = 0;
		scanf ("%d", &n);
		for (int i = 1; I <= N; i++) {
			scanf ("%d%d", &s[i].left, &s[i].right);
			NUM[++T] = s[i].left;//subscript starting from 1 
			num[++t] = s[i].right;
		}
		
Next deal with the situation that is discretized and neglected. After the order, if each endpoint is greater than 1, then add an endpoint between the two endpoints, but if the two endpoints equal is meaningless, because discretization is one by one corresponds, there is no need to repeat the number of one by one corresponding, so also need a step to heavy, go overweight, and then the previous step of the discretization process.

		Go to re 
		-process int m = unique (num+1, num+t+1)-num;
		/* Here need to pay more attention, because the subscript, subscript one by one corresponding, so subscript either starting from
0, or starting from 1 to one by one corresponds, otherwise it will lead to discretization error 
		*/ 
		int p = m-1; 
		for (int i = 1; i < P; i++) {
			if (Num[i+1]-num[i] > 1)//Because it is sorted in ascending order and de-processing, so num[i+1] must be greater than num[i] 
			num[m++] = num [i]+1;//to add a number between the two 
		}
		//After processing, then one-step sorting sort 
		(num+1, num+m);
This discretization is good, and then is the operation of the line tree, in order to find the corresponding endpoint faster, directly with two points to find it. For details, see the code:

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace
Std
const int MAXN = 1E5+5;
	struct node{int left;
int right; 
}S[MAXN];
BOOL HASH[MAXN];
int num[maxn<<4];

int value[maxn<<4];
	void pushdown (int i) {//delay tag value[i<<1] = value[i<<1|1] = Value[i];
Value[i] =-1; } void Update (int numl, int numr, int C, int l, int r, int i) {if (numl <= l && numr >= R) {//Find interval Valu
		E[i] = C;
	return;
	} if (Value[i]! =-1) pushdown (i);//update next interval int m = (l+r) >>1;
	if (NUMR <= m) Update (NUML, NUMR, C, L, M, i<<1);
	else if (Numl > M) Update (NUML, NUMR, C, m+1, R, I<<1|1);
		else {Update (numl, M, C, L, M, i<<1);
	Update (m+1, NUMR, C, m+1, R, I<<1|1);
} return;
} int ans;
			void Query (int i, int l, int r) {if (value[i]! =-1) {if (hash[value[i]] = = false) {ans++;
		Hash[value[i]] = true;
	} return;
	} if (L = = r) return; int m = (l+r) >>1;
	Query (I<<1, L, M);
Query (I<<1|1, m+1, R);
	} int main () {int T;
	int N;
	scanf ("%d", &t);
		while (t--) {memset (value,-1, sizeof (value));
		Memset (hash, false, sizeof (hash));
		int t = 0;
		scanf ("%d", &n);
			for (int i = 1; I <= N; i++) {scanf ("%d%d", &s[i].left, &s[i].right);
		NUM[++T] = s[i].left;//subscript starting from 1 num[++t] = s[i].right;
		}//Processing a sort (num+1, num+t+1);
		Go to re-process int m = unique (num+1, num+t+1)-num; 
		int p = m-1;
		Processing two for (int i = 1; i < P; i++) {if (Num[i+1]-num[i] > 1) num[m++] = num[i]+1;
		} sort (num+1, num+m);  for (int i = 1; I <= N; i++) {int L = lower_bound (num+1, Num+m, s[i].left)-num;//binary find int r = Lower_bound (num+1,
			Num+m, s[i].right)-num;
		Update (L, R, I, 1, m-1, 1);//updated} ans = 0;
	Query (1, 1, m-1);//Querying printf ("%d\n", ans);
} 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.