Description
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.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates
started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
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.
Input
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
The picture below illustrates the case of the sample input.
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
题目意思为:第一行输入t,有t块,每一块有一个数n,表示接下来有n个段更改,每一段是一个种类,查询时,种类相同的只记作一种。
解题:在离散化时,注意本身不相邻的两个点可在中间多加一个或两个点来隔开,这个小巧门可以用来做这一类似的题(如:给上色等)。
#include<iostream> #include<stdio.h> #include<algorithm> #include<map> using namespace std; #define N 200100 struct fanwen { int L,R; }T[N]; struct Tree { int type; bool b; }tree[4*N]; int node[N+5],init[N+5]; int cmp(int a,int b){return a<b;} int resort(int m) { int i,n=0; sort(init,init+m,cmp); node[++n]=init[0]; for(i=1;i<m;i++) if(node[n]!=init[i]) { if(node[n]+1<init[i])//加点隔开两个不相邻的两个点 {node[++n]=node[n-1]+1;} node[++n]=init[i]; } return n; } int search(int n,int ans) { int l=1,r=n,m; while(l<=n){ m=(l+r)/2; if(node[m]==ans) break; if(node[m]<ans) l=m+1; if(node[m]>ans) r=m-1; } return m; } void builde(int l,int r,int k) { int m=(l+r)/2; tree[k].type=0; tree[k].b=true; if(l==r) return ; builde(l,m,k*2); builde(m+1,r,k*2+1); } void updata(int l,int r,int k,int L,int R,int t) { int m=(l+r)/2; if(L<=l&&r<=R) { tree[k].type=t; tree[k].b=true; return ; } if(tree[k].b==true) { tree[k*2].b=tree[k*2+1].b=true; tree[k*2].type=tree[k*2+1].type=tree[k].type; } if(L<=m) updata(l,m,k*2,L,R,t); if(R>m) updata(m+1,r,k*2+1,L,R,t); tree[k].b=false; } int kind; map<int,int>typ; void find_kind(int l,int r,int k) { int m=(l+r)/2; if(tree[k].b==true){ if(!typ[tree[k].type]){ typ[tree[k].type]=1; kind++; } return ; } find_kind(l,m,k*2); find_kind(m+1,r,k*2+1); } int main() { int t,n,m,i; scanf("%d",&t); while(t--) { scanf("%d",&m); for(i=1,n=0;i<=m;i++) { scanf("%d%d",&T[i].L,&T[i].R); init[n++]=T[i].L; init[n++]=T[i].R; } n=resort(n); builde(1,n,1); for(i=1;i<=m;i++) updata(1,n,1,search(n,T[i].L),search(n,T[i].R),i); typ.clear(); kind=0; typ[0]=1; find_kind(1,n,1); printf("%d\n",kind); } }