BOJ 1275: 커피솝 2 작성일 2020-03-02 https://www.acmicpc.net/problem/1275 3월동안 210문제 풀기 (7/210) 간단한 구간 트리 문제. 펜윅 트리로 풀어봄 long long 때문에 겁나게 틀림 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#include <bits/stdc++.h> using namespace std; typedef long long IType; class Fenwick { int N, maxSegment = 1; vector<IType> tree; public: Fenwick(int n): N(n), tree(n+1) { while(maxSegment < n) maxSegment *= 2; maxSegment /= 2; } void update(int p, IType val) { for(int i = p; i <= N; i += i & -i) tree[i] += val; } IType query(int p) { IType res = 0; for(int i = p; i > 0; i -= i & -i) res += tree[i]; return res; } int findBound(IType target) { int pos = 0; for(int segSize = maxSegment; segSize > 0; segSize /= 2) if(tree[pos + segSize] < target) pos += segSize, target -= tree[pos]; return pos; } }; int main() { int N, Q, x, y, a, b; scanf("%d %d", &N, &Q); vector<IType> arr(N+1); Fenwick fwt(N); for(int i = 1; i <= N; i++) { scanf("%lld", &arr[i]); fwt.update(i, arr[i]); } for(int i = 0; i < Q; i++) { scanf("%d %d %d %d", &x, &y, &a, &b); if(x > y) swap(x,y); printf("%lld\n", fwt.query(y) - fwt.query(x-1)); fwt.update(a,b-arr[a]); arr[a] = b; } }