[UVA][線段樹] 10587 - Mayor's posters
Problem G: Mayor's posters
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 ≤ n ≤ 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 ≤ i ≤ n, 1 ≤ li ≤ ri ≤ 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.
For each input data set print the number of visible posters after all the posters are placed.
The picture below illustrates the case of the sample input.
Sample input
1 5 1 4 2 6 8 10 3 4 7 10
Output for sample input
4
Author: Adapted from VI AMPwPZ by P. Rudnicki
題目描述:
給定每個海報張貼的區間,按照順序貼上去,求最後能看到幾種海報。
如果能看到同一張海報,卻被分開到好幾個地方,只能算看到一種。
題目解法:
需要離散化,離散化時特別小心,需要多離散化一些點,否則會有下方給的測資錯誤。
#include <stdio.h>
#include <map>
#include <string.h>
using namespace std;
int Tree[1048576];
int query(int k, int l, int r, int ql, int qr) {
if(l > r) return 1;
if(ql <= l && r <= qr)
return Tree[k];
if(Tree[k]) return 1;
int m = (l + r)/2;
if(m >= qr)
return query(k<<1, l, m, ql, qr);
if(m < ql)
return query(k<<1|1, m+1, r, ql, qr);
return query(k<<1, l, m, ql, qr) &&
query(k<<1|1, m+1, r, ql, qr);
}
void modify(int k, int l, int r, int ql, int qr) {
if(l > r) {
Tree[k] = 1;
return;
}
if(ql <= l && r <= qr) {
Tree[k] = 1;
return ;
}
if(Tree[k]) return ;
int m = (l + r)/2;
if(m >= qr)
modify(k<<1, l, m, ql, qr);
else if(m < ql)
modify(k<<1|1, m+1, r, ql, qr);
else {
modify(k<<1, l, m, ql, qr);
modify(k<<1|1, m+1, r, ql, qr);
}
Tree[k] = Tree[k<<1]&Tree[k<<1|1];
}
int main() {
int testcase, n;
int i, j, k, L[10005], R[10005];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
map<int, int> RR;
for(i = 0; i < n; i++) {
scanf("%d %d", &L[i], &R[i]);
RR[L[i]] = 1;
RR[L[i]-1] = 1;
RR[L[i]+1] = 1;
RR[R[i]] = 1;
RR[R[i]-1] = 1;
RR[R[i]+1] = 1;
}
int size = 1;
for(map<int, int>::iterator it = RR.begin();
it != RR.end(); it++)
it->second = size++;
memset(Tree, 0, sizeof(Tree));
int ret = 0;
for(i = n-1; i >= 0; i--) {
int l = RR[L[i]], r = RR[R[i]];
// printf("[%d, %d]\n", l, r);
int v = query(1, 1, size, l, r);
if(v == 0)
ret++;
modify(1, 1, size, l, r);
}
printf("%d\n", ret);
}
return 0;
}
/*
1
3
1 10
1 3
6 10
*/