Test instructions: Poster has a long wall, presumably10000000 So long, there are some posters will be affixed to the wall, of course, the order of posters have successively, asked you when the last poster is affixed to the time can not find out on this wall how many different posters? analysis: Because the poster will be affixed to the front of the cover off, not too easy to find out, but if from the last one upside-down, as long as the wall to judge whether this section is completely covered on it, because the wall is relatively long, so need to discretization.
************************************************************************Note: The return value in the up and new time should be under the update or directly return how to update??
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
usingnamespaceStd
ConstintMAXN =40005;
intHASH[MAXN];//record the data after discretization
structpost{intL, R;} P[MAXN];//Record Poster
structTree
{
intL, R;
BOOLIscover;//record whether this interval is covered
intMid () {return(L+R)/2;}
}tree[maxn*4];
voidUp (intRoot//update up, if the left and right subtrees are overwritten, then he will be overwritten
{
if(Tree[root]. L! = Tree[root]. R
if(tree[root<<1].iscover && tree[root<<1|1].iscover)
Tree[root].iscover =true;
}
voidBuild (intRootintLintR
{
Tree[root]. L = L, Tree[root]. R = r;
Tree[root].iscover =false;
if(L = = R)return;
Build (root<<1, L, Tree[root]. Mid ());
Build (root<<1|1, Tree[root]. Mid () +1, R);
}
//If there is a position within the interval to return true, return false
BOOLInsert (intRootintLintR
{
if(Tree[root].iscover)returnfalse;
if(Tree[root]. L = = L && tree[root]. R = = r)
{
Tree[root].iscover =true;
returntrue;
}
BOOLAns
if(R <= Tree[root]. Mid ())
Ans = Insert (root<<1, L, R);
Elseif(L > Tree[root]. Mid ())
Ans = Insert (root<<1|1, L, R);
Else
{
BOOLLson = Insert (root<<1, L, Tree[root]. Mid ());
BOOLRson = Insert (root<<1|1, Tree[root]. Mid () +1, R);
Ans = Lson | Rson;
}
Up (root);
returnAns
}
intMain ()
{
intT
scanf"%d", &t);
while(t--)
{
intI, N, nh=0;
scanf"%d", &n);
for(i=1; i<=n; i++)
{
scanf"%d%d", &P[I].L, &P[I].R);
hash[nh++] = P[I].L, hash[nh++] = p[i].l-1;
hash[nh++] = P[I].R, hash[nh++] = p[i].r+1;
}
Sort (Hash, HASH+NH);
NH = unique (hash, HASH+NH)-hash;
Build (1,1, NH);
intAns =0;
for(I=n; i>0; i--)
{
intL = Lower_bound (hash, HASH+NH, P[I].L)-hash;
intR = Lower_bound (hash, HASH+NH, P[I].R)-hash;
if(Insert (1, L, r) = =true)
Ans + =1;
}
printf"%d\n", ans);
}
return0;
}
D-mayor ' s posters-2528 (interval coverage)