Description
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:Input
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.Output
For each input data set print the number of visible posters after all the posters are placed.Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4首先想到離散化,正確的離散化的方法:
#include#include #include using namespace std; //其實離散後總共的單位區間最多可達到20000+19999個 //所以cover開到maxn*2*2*4 const int maxn=10000+10; int cover[maxn*16],x[maxn],y[maxn],s[maxn*2]; unsigned short Hash[10000005]; //unsigned short才夠用,short不夠用,會re void pushDown(int k){ if(cover[k]>=0){ cover[k*2]=cover[k*2+1]=cover[k]; cover[k]=-1; } } void Insert(int a,int b,int c,int k,int l,int r){ if(a<=l && r<=b){ cover[k]=c;return ; } pushDown(k); int m=(l+r)/2; if(a<=m) Insert(a,b,c,k*2,l,m); if(b>m) Insert(a,b,c,k*2+1,m+1,r); } bool vis[maxn]; void ask(int k,int l,int r){ if(cover[k]>=0){ vis[cover[k]]=true; return ; } if(l==r) return ; pushDown(k); int m=(l+r)/2; ask(k*2,l,m); ask(k*2+1,m+1,r); } int main() { int i,T,n; scanf(%d,&T); while(T--){ scanf(%d,&n); memset(Hash,0,sizeof(Hash)); int tot=0; for(i=0;i