Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Atasamentele paginii SumDiv2 | Cod sursa (job #1461426) | Cod sursa (job #2533375)
#include <bits/stdc++.h>
const int MV = 1e5 ;
std::fstream in ("aib.in", std::ios::in) ;
std::fstream out ("aib.out", std::ios::out) ;
class BinaryIndexedTree {
private :
std::vector<int> aib ;
int sz ;
public :
BinaryIndexedTree(int _ = 0) {
this ->sz = _ ;
aib.resize(sz + 1) ;
}
void update(int poz, int val) {
for (int i = poz ; i <= sz ; i += (i & -i)) {
aib[i] += val ;
}
}
int query(int poz) {
int ret(0) ;
for (int i = poz ; i > 0 ; i -= (i & -i)) {
ret += aib[i] ;
}
return ret ;
}
int BinarySearch(int val) {
int ret(0) ;
for (int step(1 << 30) ; step && val ; step >>= 1) {
if (ret + step <= sz && aib[ret + step] < val) {
val -= aib[ret + step] ;
ret |= step ;
}
}
return ret + 1 ;
}
};
int v[MV + 5] ;
int main() {
int n, m ;
in >> n >> m ;
BinaryIndexedTree aib(n) ;
int val, type, a, b ;
for (int i = 1 ; i <= n ; ++ i) {
in >> v[i] ;
aib.update(i, v[i]) ;
}
for (int i = 1 ; i <= m ; ++ i) {
in >> type ;
if (type == 0) {
in >> a >> b ;
aib.update(a, b) ;
} else if (type == 1) {
in >> a >> b ;
out << aib.query(b) - aib.query(a - 1) << '\n' ;
} else {
in >> a ;
out << aib.BinarySearch(a) << '\n' ;
}
}
}