BZOJ4444: [SCOI2015] Flag Program

Source: Internet
Author: User

The coordinates are discretized first, because the intervals are not contained, and can be understood as the minimum number of fighters required for each start output.

Will ring times long, broken loop into a chain, set $f[i]$ to indicate that the interval left endpoint does not exceed the $i$ when the right end of the maximum value, can be deduced by $o (n) $ recursion.

So if $f[i]$ is seen as an ancestor of $i$, it actually forms a tree rooted in $2n$.

First of all, the violence calculated the answer of number 1th, $t$, set $l=t-1$.

Then DFS this tree, using a stack to store each point to a point on the root path in order of depth.

For a point, you only need to start with $l$ to enumerate the answers violently, and then $o (1) $ test.

Because the answer of each point is not more than 1, the time complexity of the whole algorithm is $o (n) $ after the discretization is removed.

#include <cstdio> #include <algorithm> #define N 400010int n,m,i,x,y,l,a[n/2][2],b[n],st[n/2],f[n*2],g[n* 2],nxt[n*2],q[n*2],t,ans[n];inline void Read (int&a) {char c;while (!) ( ((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')); a=c-' 0 '; while (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*= Ten) +=c-' 0 ';}  inline int lower (int x) {int l=1,r=m,mid,t;  while (l<=r) if (b[mid= (l+r) >>1]<=x) l= (t=mid) +1;else r=mid-1; return t;} inline void up (Int&x,int y) {if (x<y) x=y;}  void Dfs (int x) {q[++t]=x; if (x<=m) for (int i=l;;  i++) if (q[t-i]>=x+m) {ans[x]=i;break;}  for (int i=g[x];i;i=nxt[i]) DFS (i); t--;}  int main () {read (n), read (m);  for (m=0,i=1;i<=n;i++) read (a[i][0]), read (a[i][1]), b[++m]=a[i][0],b[++m]=a[i][1];    For (Std::sort (b+1,b+m+1), i=1;i<=n;i++) {st[i]=x=lower (a[i][0]), Y=lower (a[i][1]);    if (x<y) up (F[x],y), Up (f[x+m],y+m);  else up (F[1],y), Up (F[x],y+m), Up (f[x+m],m+m);  } for (i=1;i<=m+m;i++) up (f[i],f[i-1]);  for (i=1;i<m+m;i++) nxt[i]=g[f[i]],g[f[i]]=i; For(L=-1,i=1;i<=m;i=f[i])  l++;  DFS (M+M);  for (i=1;i<=n;i++) printf ("%d", Ans[st[i]]); return 0;}

  

BZOJ4444: [SCOI2015] Flag Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.