1682. [HAOI2014] poster, 1682haoi2014
1682. [HAOI2014] Post a poster
★★☆Input file:ha14d.in
Output file:ha14d.out
Simple comparison
Time Limit: 1 s memory limit: 256 MB
[Description]
Bytetown city to run for mayor, all the voters can speak freely to the candidate for mayor election. To achieve unified management, the City Council has prepared an electoral wall for voters to post posters.
The posting rules are as follows:
1. The electoral wall is a rectangle of N Units in length. Each unit is recorded as a grid;
2. The height of all posters must be the same as that of the electoral wall;
3. Each poster is represented by "a B", that is, A poster is posted from the grid A to the grid B;
4. The posters that are pasted later can cover the posters or part of the posters that have been pasted earlier.
Now you can determine how many posters can be seen on the electoral wall after all the posters are posted.
[Input format]
Line 1: n m represents the length of the electoral wall and the number of posters, respectively.
Next line M: Ai Bi indicates the position where each poster is posted
[Output format]
Output The number of posters that can be viewed on the electoral wall after all posters are pasted.
[Example input]
100 5
1 4
2 6
8 10
3 4
7 10
[Sample output]
4
Tip]
[Constraints]
1 0 <= N <= 10000000 1 <= M <= 1000 1 <= Ai <= Bi <= 10000000
All data is integers. There is a space between the data
I took a look at it and immediately experienced a brute force attack with 10 minutes. After the T3 attack, I came back and looked at the question. It felt like the line segment tree was naked.
Then we wrote the line segment tree in 15 minutes.
But !!!
Line Segment tree !!
Actually !!
!!
Violent !!
Slow !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
And !!
Also !!
Memory explosion !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
This is embarrassing. Looking at the scenes where every time the camera program is violent, the first thing is to run the line segment tree and wait for another second to get the result. What else can I say ,,
Is it because the line segment tree I wrote is too ugly ???
=. =
Violent thinking: Simulate every poster
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=10000001; 7 int n,m,x,y; 8 int read(int & n) 9 {10 char c='/';int x=0,flag=0;11 while(c<'0'||c>'9')12 {c=getchar();13 if(c=='-')flag=1;}14 while(c>='0'&&c<='9')15 {x=x*10+c-48;c=getchar();}16 if(flag)n=-x;17 else n=x;18 return n;19 }20 int a[MAXN];21 int vis[MAXN];22 int now=1;23 int ans=0;24 int main()25 {26 freopen("a.in","r",stdin);27 freopen("b.out","w",stdout);28 read(n);read(m);29 for(int i=1;i<=m;i++)30 {31 read(x);read(y);32 if(x>y)swap(x,y);33 for(int j=x;j<=y;j++)34 a[j]=i;35 }36 for(int i=1;i<=n;i++)37 {38 if(vis[a[i]]==0&&a[i]!=0)39 {40 ans++;41 vis[a[i]]=1;42 }43 }44 printf("%d",ans);45 return 0;46 }
Positive Solution: The teacher gave me a scanning thread but didn't understand it.
First, we read the l and r of each poster,
Then scan in reverse order. The default ans is one, because the last one will certainly be visible.
For each I always enumeration, it is like a float. If n can be greater than n after continuous cropping
Ans ++
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=1001; 8 int n,m,ans=1; 9 int vis[MAXN];10 struct node11 {12 int l;13 int r;14 int id;15 }a[MAXN];16 int read(int & n)17 {18 char c='/';int flag=0,x=0;19 while(c<'0'||c>'9')20 {if(c=='-')flag=1;21 c=getchar();}22 while(c>='0'&&c<='9')23 {x=x*10+(c-48);24 c=getchar();}25 if(flag)n=-x;26 else n=x;27 }28 void water(int ll,int rr,int now,int pos)29 {30 if(vis[pos])31 return ;32 while(now<=m&&(rr<=a[now].l||ll>=a[now].r))33 now++;34 if(now>m)35 {36 vis[pos]=1;37 ans++;38 return ;39 }40 if(ll<a[now].l&&rr>a[now].l)41 water(ll,a[now].l,now+1,pos);42 if(rr>a[now].r&&ll<a[now].r)43 water(a[now].r,rr,now+1,pos);44 45 }46 int main()47 {48 freopen("ha14d.in","r",stdin);49 freopen("ha14d.out","w",stdout);50 read(n);read(m);51 for(int i=1;i<=m;i++)52 {53 read(a[i].l);54 read(a[i].r);55 a[i].r=a[i].r+1;56 a[i].id=i;57 }58 for(int i=m-1;i>=1;i--)59 {60 water(a[i].l,a[i].r,i+1,i);61 }62 printf("%d",ans);63 return 0;64 }