Pagini recente » Cod sursa (job #1272489) | Cod sursa (job #85637) | Cod sursa (job #402657) | Cod sursa (job #92983) | Cod sursa (job #168169)
Cod sursa(job #168169)
#include<stdio.h>
FILE*fin=fopen("marbles.in","r");
FILE*fout=fopen("marbles.out","w");
#define maxn 100001
int a[maxn],b[maxn],nb[65][maxn],n,m;
void rec(int nod,int dim)
{
int nd=nod,max=a[nod],st,dr;
st=nod<<1;dr=st+1;
if(st<=dim&&a[st]>max){max=a[st];nd=st;}
if(dr<=dim&&a[dr]>max){max=a[dr];nd=dr;}
if(nd!=nod)
{
a[nd]^=a[nod]^=a[nd]^=a[nod];
b[nd]^=b[nod]^=b[nd]^=b[nod];
rec(nd,dim);
}
}
void ord()
{
int i,dim=n;
for(i=n/2;i>=1;i--)
rec(i,n);
while(dim>1)
{
a[1]^=a[dim]^=a[1]^=a[dim];
b[1]^=b[dim]^=b[1]^=b[dim];
dim--;
rec(1,dim);
}
}
int main()
{
int i,st,dr,mij,k,j,t,start,stop,max,bila;
fscanf(fin,"%d%d",&n,&m);
for(i=1;i<=n;i++)
fscanf(fin,"%d%d",&a[i],&b[i]);
ord();
for(i=1;i<=64;i++)
nb[i][0]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=64;j++)
nb[j][i]=nb[j][i-1];
nb[b[i]][i]++;
}
for(k=1;k<=m;k++)
{
fscanf(fin,"%d%d%d",&t,&i,&j);
if(t)
{
st=1;dr=n;
while(st<dr)
{
mij=(st+dr)/2;
if(a[mij]<i) st=mij+1;
else dr=mij;
}
start=st;
st=1;dr=n;
while(st<dr-1)
{
mij=(st+dr)/2;
if(a[mij]>j) dr=mij-1;
else st=mij;
}
if(a[dr]<=j) stop=dr;
else stop=st;
max=-1;
for(i=1;i<=64;i++)
if(nb[i][stop]-nb[i][start-1]>max)
{
max=nb[i][stop]-nb[i][start-1];
bila=i;
}
fprintf(fout,"%d\n",max);
}
else
{
st=1;dr=n;
while(st<dr)
{
mij=(st+dr)/2;
if(a[mij]==i){st=mij;break;}
else if(a[mij]<i) st=mij+1;
else dr=mij-1;
}
a[st]+=j;
}
}
fclose(fin);
fclose(fout);
return 0;
}