Pagini recente » Cod sursa (job #3283936) | Cod sursa (job #2402624) | Cod sursa (job #1202736) | Cod sursa (job #2497128) | Cod sursa (job #313469)
Cod sursa(job #313469)
//Code by Patcas Csaba
//Time complexity:
//Space complexity:
//Method:
//Working time:
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <iostream>
#include <sstream>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <time.h>
#include <assert.h>
using namespace std;
#define in_file "aib.in"
#define out_file "aib.out"
#define VI vector <int>
#define FORN(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define S size()
#define B begin()
#define E end()
#define X first
#define RS resize
#define Y second
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(),x.end()
#define repeat do{
#define until(x) }while(!(x));
int n, m;
VI aib;
void update(int ind, int val)
{
while (ind<=n)
{
aib[ind]+=val;
ind+=(ind & -ind);
}
}
int query(int ind)
{
int ans=0;
while (ind)
{
ans+=aib[ind];
ind-=(ind & -ind);
}
return ans;
}
int main()
{
FILE* fin=fopen(in_file,"r");
FILE* fout=fopen(out_file,"w");
fscanf(fin,"%d%d",&n,&m);
aib.RS(n+1);
FOR(i,1,n)
{
int x;
fscanf(fin,"%d",&x);
update(i,x);
}
FORN(i,m)
{
int x, y, z;
fscanf(fin,"%d%d",&x,&y);
if (x!=2) fscanf(fin,"%d",&z);
if (x==0) update(y,z);
else
if (x==1)
{
int s1=query(z);
int s2=query(y-1);
fprintf(fout,"%d\n",s1-s2);
}
else
{
int pow2=1, i, sum;
while (pow2<=n && aib[pow2]<=y) pow2<<=1;
pow2>>=1;
sum=aib[pow2];
for (i=pow2; pow2; pow2>>=1)
if (i+pow2<=n && sum+aib[i+pow2]<=y)
{
i+=pow2;
sum+=aib[i];
}
if (sum==y) fprintf(fout,"%d\n",i);
else fprintf(fout,"-1\n");
}
}
fclose(fin);
fclose(fout);
return 0;
}