POJ2182 Lost Cows 树状数组

题意:有编号1~n乱序排列的奶牛,给出了每一个奶牛前小于自己编号的奶牛数目

维护一个树状数组,下标是编号,值为$0/1$标识是否存在,很显然最后一个牛的编号是知道的,我们在树状数组上二分出前缀和为小于这个牛编号的奶牛数目的位置,这个位置代表的编号就是这头奶牛的编号。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ull unsigned long long
#define ll long long
#define R register int
#define pause (for(R i=1;i<=10000000000;++i))
#define OUT freopen("out.out","w",stdout);
using namespace std;
namespace Fread {
    static char B[1<<15],*S=B,*D=B;
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    inline int g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } inline bool isempty(const char& ch) {return ch<=36||ch>=127;}
    inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));}
}using Fread::g; using Fread::gs; 
const int N=8192;
int n,c[N+1],a[N+1],pos,ans[N+1];
inline int lbt(int x) {return x&-x;}
inline void add(int pos) {for(;pos<=N;pos+=lbt(pos)) --c[pos];}
inline int query(int vl) { R cnt=0,p=0;
    for(R k=12;~k;--k) {if(cnt+c[(1<<k)+p]<=vl) p+=1<<k,cnt+=c[p];} return p;
}
signed main() {
#ifdef JACK
    freopen("NOIPAK++.in","r",stdin);
#endif    
    n=g(); for(R i=2;i<=n;++i) a[i]=g(); 
    for(R i=1;i<N;++i) c[i]=1;
    for(R i=1;i<N;++i) c[i+lbt(i)]+=c[i]; 
    for(R i=n;i;--i) ans[i]=pos=query(a[i])+1,add(pos);
    for(R i=1;i<=n;++i) printf("%d\n",ans[i]);
}

2019.06.27

上一篇:MySQL导入数据提示Lost connection to MySQL server


下一篇:mysql登陆时出现ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication