Cod sursa(job #852119)

Utilizator ericptsStavarache Petru Eric ericpts Data 10 ianuarie 2013 21:22:25
Problema Marbles Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>
#include <algorithm>
using namespace std;
const int maxn = 100001;
struct bila
{
    int dist;
    int cul;
}v[maxn];

int hi_c,n;
int cup2[65][maxn];
int logN;
bool comp(const bila &a,const bila &b)
{
    return a.dist <= b.dist;
}
int bsearch(const int &val)
{
    int i,step = logN;
    for(i = 0 ; step ; step >>= 1)
    {
        if((i | step) <= n && v[i|step].dist <= val)
            i |= step;
    }
    return i;
}
int main()
{
ifstream in("marbles.in");
ofstream out("marbles.out");

    int m;
    in >> n >> m;
    for(logN = 1 ; logN < n ; logN <<= 1);
    int i,j;
    for(i =1 ;i <=n ; ++ i)
    {
        in >> v[i].dist >> v[i].cul;
        if(v[i].cul > hi_c)
            hi_c = v[i].cul;
    }
    sort(v+1,v+n+1,comp);
    for(i = 1 ; i <= hi_c; ++ i)
    {
        for(j = 1 ; j <= n ; ++ j)
        {
            cup2[i][j] = cup2[i][j-1];
            if(v[j].cul == i)
                ++cup2[i][j];
        }
    }
    int tip,x,y;
    while(m--)
    {
        in >> tip >> x >> y;
        if(tip == 0)
        {
            j = bsearch(x);
            if(v[j].dist == x)
                v[j].dist += y;
        }
        else
        {
            i = bsearch(x);
            j = bsearch(y);
            if(v[i].dist == x)
                --i;
            y = 0;
            for(x = 1 ; x <= hi_c; ++ x)
                y = max(y,cup2[x][j] - cup2[x][i]);
            out << y << "\n";
        }
    }
    return 0;
}