Cod sursa(job #1522295)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 11 noiembrie 2015 15:50:08
Problema Marbles Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <cstdio>
#include <algorithm>
#include <cctype>
#define MAXN 100000
#define BUF_SIZE 4096
struct mycreation{
    int a, b;
};
mycreation v[MAXN];
int e[64][MAXN];
int pos=BUF_SIZE;
char buf[BUF_SIZE];
FILE *fin;
inline char nextch(){
    if(pos==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fin);
        pos=0;
    }
    return buf[pos++];
}
inline int read(){
    int x=0, s=1;
    char ch=nextch();
    while((!isdigit(ch))&&(ch!='-')){
        ch=nextch();
    }
    if(ch=='-'){
        s=-1;
        ch=nextch();
    }
    while(isdigit(ch)){
        x=10*x+ch-'0';
        ch=nextch();
    }
    return x*s;
}
bool cmp(const mycreation x, const mycreation y){
    return x.a<y.a;
}
int main(){
    int n, m, i, a, b, t, rez, ans, left, right, j;
    FILE *fout;
    fin=fopen("marbles.in", "r");
    fout=fopen("marbles.out", "w");
    n=read();
    m=read();
    for(i=1; i<=n; i++){
        v[i].a=read();
        v[i].b=read();
    }
    std::sort(v+1, v+n+1, cmp);
    for(i=0; i<64; i++){
        for(j=1; j<=n; j++){
            e[i][j]=e[i][j-1];
            if(v[j].b-1==i){
                e[i][j]++;
            }
        }
    }
    for(i=0; i<m; i++){
        t=read();
        a=read();
        b=read();
        if(t==0){
            rez=0;
            for(int pas=1<<16; pas; pas>>=1){
                if((rez+pas<=n)&&(v[rez+pas].a<=a)){
                    rez+=pas;
                }
            }
            v[rez].a+=b;
        }else{
            rez=0;
            for(int pas=1<<16; pas; pas>>=1){
                if((rez+pas<=n)&&(v[rez+pas].a<a)){
                    rez+=pas;
                }
            }
            left=rez+1;
            rez=0;
            for(int pas=1<<16; pas; pas>>=1){
                if((rez+pas<=n)&&(v[rez+pas].a<=b)){
                    rez+=pas;
                }
            }
            right=rez;
            ans=0;
            if(left<=right){
                for(j=0; j<64; j++){
                    if(ans<e[j][right]-e[j][left-1]){
                        ans=e[j][right]-e[j][left-1];
                    }
                }
            }
            fprintf(fout, "%d\n", ans);
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}