[Solution] corral book title

[Solution] corral book title

Laughter is universal problem

How many segments should be divided into at least one set of intersecting segments such that all the output is not a collection of programs.

Obviously we did not make all indices covering a line of plus 1, the last query global maximum is the answer, this is too obviously a. Direct Fenwick tree \ (O (the n-\ the n-log) \) , there may be \ (O (n) \) algorithm, there is no one to teach it to me.

Direct simulation after a moment and then get this number of collections, you can output program.

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>

using namespace std;  typedef long long ll;
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57)f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}
const int maxn=50005;
const int Lb=1000005;


int seg[Lb];
#define lowbit(x) ((x)&(-x))
int n,m;


inline void add(const int&pos,const int&tag){
      for(register int t=pos;t<=Lb;t+=lowbit(t)) seg[t]+=tag;
}

inline int q(const int&pos){
      register int ret=0;
      for(register int t=pos;t;t-=lowbit(t)) ret+=seg[t];
      return ret;
}

set< int > s;
vector < int > ve[Lb],be[Lb];
int arc[maxn];
int sav[Lb];
int cnt,len;

int main(){
#ifndef ONLINE_JUDGE
      freopen("in.in","r",stdin);
      
#endif
      n=qr();
      for(register int t=1,t1,t2;t<=n;++t){
        t1=qr();t2=qr();
        be[t1].push_back(t);
        ve[t2+1].push_back(t);
        add(t1,1);add(t2+1,-1);
      }
      int ans=0;
      for(register int t=1;t<=1000000;++t)
        ans=max(ans,q(t));
      for(register int t=1;t<=ans;++t) s.insert(t);
      for(register int t=1;t<=1000000;++t) {
        for(register int i=0,ed=ve[t].size();i<ed;++i)
          s.insert(arc[ve[t][i]]);
        for(register int i=0,ed=be[t].size();i<ed;++i){
          arc[be[t][i]]=*s.begin();
          s.erase(s.begin());
        }
      }
      arc[0]=ans;
      for(register int t=0;t<=n;++t) printf("%d\n",arc[t]);
      return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/11223732.html