Cod sursa(job #1564255)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 9 ianuarie 2016 16:13:44
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
#include <cstdio>
#include <cctype>
#include <algorithm>
#define MAXN 100000
#define MAXL 100000
struct mycreation{
    int st, dr;
}e[MAXN+1];
int k, pos=0, aib[MAXN+1], ind[MAXN+1], q[MAXN+1];
bool v[MAXN+1], t[MAXN+1];
char buf[6*(1<<20)], u[MAXL+1];
inline int read(){
    int x=0;
    while(!isdigit(buf[pos])){
        pos++;
    }
    while(isdigit(buf[pos])){
        x=10*x+buf[pos]-'0';
        pos++;
    }
    return x;
}
inline mycreation get(){
    while(!isdigit(buf[pos])){
        pos++;
    }
    mycreation a;
    a.st=pos;
    a.dr=pos;
    pos++;
    while(isdigit(buf[pos])){
        a.dr++;
        pos++;
    }
    return a;
}
inline void update(int p){
    while(p<=k){
        aib[p]++;
        p+=p&(-p);
    }
}
bool cmp(const int a, const int b){
    if(e[a].dr-e[a].st!=e[b].dr-e[b].st){
        return e[a].dr-e[a].st<e[b].dr-e[b].st;
    }
    int i=0;
    while((i<=e[a].dr-e[a].st)&&(buf[i+e[a].st]==buf[i+e[b].st])){
        i++;
    }
    return ((i<=e[a].dr-e[a].st)&&(buf[i+e[a].st]<buf[i+e[b].st]));
}
inline int cauta(int p){
    int rez=0;
    for(int pas=1<<16; pas; pas>>=1){
        if((rez+pas<=k)&&(cmp(ind[rez+pas], p))){
            rez+=pas;
        }
    }
    return rez+1;
}
int main(){
    int r, n, i, a, rez, pas, p;
    FILE *fin, *fout;
    fin=fopen("nums.in", "r");
    fout=fopen("nums.out", "w");
    fread(buf, 6*(1<<20), 1, fin);
    k=0;
    r=0;
    n=read();
    for(i=0; i<n; i++){
        t[i]=read();
        if(t[i]==1){
            k++;
            ind[k]=k;
            e[k]=get();
        }else{
            q[r++]=read();
        }
    }
    std::sort(ind+1, ind+k+1, cmp);
    a=0;
    r=0;
    for(i=0; i<n; i++){
        if(t[i]==1){
            a++;
            p=cauta(a);
            if(v[p]==0){
                v[p]=1;
                update(p);
            }
        }else{
            rez=0;
            for(pas=1<<16; pas; pas>>=1){
                if((rez+pas<=k)&&(aib[rez+pas]<q[r])){
                    rez+=pas;
                    q[r]-=aib[rez];
                }
            }
            rez++;
            p=0;
            for(int j=e[ind[rez]].st; j<=e[ind[rez]].dr; j++){
                u[p++]=buf[j];
            }
            u[p++]='\n';
            fwrite(u, p, 1, fout);
            r++;
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}