-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathaib.cpp
64 lines (63 loc) · 1.2 KB
/
aib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# include <fstream>
# include <algorithm>
# include <vector>
# include <queue>
# include <cstring>
# define LB(p) ((-p)&p)
# define NR 100005
# define mod 1999999973
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int i,j,n,m,S,ci,cs,mij,a,b,tip,c,ok,x;
int AIB[NR];
void update (int poz, int val)
{
for (int i=poz; i<=n; i+=LB(i))
AIB[i]+=val;
}
int suma (int poz)
{
int S=0;
for (int i=poz; i>=1; i-=LB(i))
S+=AIB[i];
return S;
}
int main ()
{
f>>n>>m;
for (i=1; i<=n; ++i)
{
f>>x;
update (i, x);
}
for (i=1; i<=m; ++i)
{
f>>tip;
if (tip==0) //update
{
f>>a>>b;
update (a, b);
}
else if (tip==1) //suma
{
f>>a>>b;
g<<suma(b)-suma(a-1)<<"\n";
}
else
{
f>>a;
ci=1; cs=n; ok=0;
while (ci<=cs)
{
mij=(ci+cs)/2;
S=suma(mij);
if (S==a) {g<<mij<<"\n"; ok=1; break;}
else if (S<a) ci=mij+1;
else cs=mij-1;
}
if (! ok) g<<"-1\n";
}
}
return 0;
}