https://www.acmicpc.net/source/15427883

 

로그인

 

www.acmicpc.net


쿼리에서 b > c 인경우가 있다.


#include <iostream>
using namespace std;

#define swap(a,b) {int t = b; b = a; a = t;}
#define MAX_N 1000001
typedef long long ll;

ll tree[MAX_N * 4];

ll update_tree(int now, int target, int start, int end, int value) {
     if (target > end || target < start) return tree[now];
     if (start == end) {
          return tree[now] = value;
     }
     int mid = (start + end) / 2;
     return tree[now] = (
          update_tree(now * 2, target, start, mid, value) +
          update_tree(now * 2 + 1, target, mid + 1, end, value)
          );
}

ll query(int now, int left, int right, int start, int end) {
     if (left > end || right < start) return 0;
     if (left <= start && end <= right) {
          return tree[now];
     }
     int mid = (start + end) / 2;
     return (
          query(now * 2, left, right, start, mid) +
          query(now * 2 + 1, left, right, mid + 1, end)
          );
}

int main() {
     ios::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     int N, M;
     cin >> N >> M;
     for (int i = 0; i < M; i++) {
          int a, b, c;
          cin >> a >> b >> c;
          if (a) { // modify
               update_tree(1, b-1, 0, N - 1, c);
          }
          else { // sum
               if (c < b) swap(b, c);
               cout << query(1, b - 1, c - 1, 0, N - 1) << "\n";
          }
     }
}

'알고리즘 > [C++]BOJ' 카테고리의 다른 글

[백준BOJ] 11437_LCA (C++)  (0) 2019.10.02
[백준] 2336_굉장한 학생 (C++)  (0) 2019.09.30
[백준] 3392_화성지도 (C++)  (0) 2019.09.30

+ Recent posts