Pagini recente » Cod sursa (job #649958) | Cod sursa (job #654400) | Cod sursa (job #1202890) | Cod sursa (job #1302531) | Cod sursa (job #205217)
Cod sursa(job #205217)
#include <stdio.h>
#define NMAX 100002
int a[NMAX],c[NMAX],n,m,i,j,k,w,p,q,s1,s2,suma,indice;
int main()
{FILE *fin,*fout;
fin = fopen("aib.in" , "r");
fout = fopen("aib.out" , "w");
fscanf(fin , "%d %d" , &n , &m);
for (i = 1 ; i <= n ;i++)
{fscanf(fin , "%d" , &a[i]);
c[i] = 0;
k= i & (i ^ (i - 1));
for (j = i-k+1; j <= i;j++)
c[i] += a[j];
}
for (i = 1; i <= m; i++)
{fscanf(fin , "%d" , &w);
if (w == 2) fscanf(fin , "%d" , &suma);
else fscanf(fin , "%d %d" , &p ,&q);
if (w == 0)
while (p <= n)
{c[p] = c[p] + q;
k= p & ( p^ (p - 1));
p = p + k;
}
if (w == 1) {s1 = 0;
while (q > 0)
{s1 += c[q];
k= q & ( q^ (q - 1));
q = q - k;
}
s2 = 0;
p--;
while (p > 0)
{s2 += c[p];
k = p &(p ^ (p - 1));
p = p - k;
}
fprintf(fout, "%d\n" ,s1 - s2);
}
if (w == 2) {s1 = 0;
indice = 0;
while (s1 < suma){
s1 = 0;
indice++;
q = indice;
while (q > 0)
{s1 += c[q];
k= q & ( q^ (q - 1));
q = q - k;
}
}
if (s1 == suma) fprintf(fout , "%d\n" , indice);
else fprintf(fout , "-1\n");
}
}
fclose (fin);
fclose (fout);
return 0;
}