Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int N, Size = 1;
- vector < int > Tree;
- void init(int N) {
- while(Size < N)
- Size <<= 1;
- Tree.resize((Size << 1) | 1);
- }
- int sum(int st, int dr) {
- st += Size, dr += Size;
- int s = 0;
- while(st <= dr) {
- if(st & 1)
- s += Tree[st++];
- if(!(dr & 1))
- s += Tree[dr--];
- st >>= 1;
- dr >>= 1;
- }
- return s;
- }
- void add(int k, int x) {
- k += Size;
- Tree[k] += x;
- for(k >>= 1; k > 0; k >>= 1)
- Tree[k] = Tree[k << 1] + Tree[(k << 1) | 1];
- }
- int main() {
- cin >> N;
- init(N);
- for(int i = 1; i <= N; ++i) {
- int x;
- cin >> x;
- add(i, x);
- }
- cout << sum(1, N);
- }
Advertisement
Add Comment
Please, Sign In to add comment