https://www.acmicpc.net/problem/1275
1275번: 커피숍2
첫째 줄에 수의 개수 N과 턴의 개수 Q가 주어진다.(1 ≤ N, Q ≤ 100,000) 둘째 줄에는 처음 배열에 들어가 있는 정수 N개가 주어진다. 세 번째 줄에서 Q+2번째 줄까지는 x y a b의 형식으로 x~y까지의 합
www.acmicpc.net
쉽네여~
import java.util.*;
import java.io.*;
public class Main {
public static long tree[];
public static long buildingTree(int start,int end ,int node, long arr[]){
if(start==end){
return tree[node]=arr[start];
}
int mid=(start+end)/2;
return tree[node]=buildingTree(start,mid,node*2,arr)+buildingTree(mid+1,end,node*2+1,arr);
}
public static void update( int start, int end, int node, long newValue, int findIndex){
if( end < findIndex || findIndex < start){
return;
}
if(start==end){
tree[node]=newValue;
return;
}
int mid=(start+end)/2;
update( start, mid,node*2, newValue, findIndex);
update(mid+1, end,node*2+1, newValue, findIndex);
tree[node]=tree[node*2]+tree[node*2+1];
}
public static long select(int start ,int end, int node, int left, int right){
if( left > end|| right < start ){
return 0;
}
if( left<= start && end<=right ){
return tree[node];
}
int mid=(start+end)/2;
return select(start,mid,node*2,left,right)+select(mid+1,end,node*2+1,left,right);
}
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
long arr[]=new long[n];
tree=new long[n*4];
for(int i=0;i<n;i++){
long tmp=scan.nextLong();
arr[i]=tmp;
}
buildingTree(0,n-1,1,arr);
for(int i=0; i<m; i++ ){
int x=scan.nextInt();
int y=scan.nextInt();
int a=scan.nextInt();
long b=scan.nextLong();
if(x>y){
int tmp=y;
y=x;
x=tmp;
}
long sum=select(0,n-1,1,x-1,y-1);
update(0,n-1,1, b,a-1);
System.out.println(sum);
}
}
}
'알고리즘 공부' 카테고리의 다른 글
백준 - 구간 합 구하기2 (0) | 2024.07.17 |
---|---|
백준 수열과 쿼리 37 (2) | 2024.07.15 |
백준 가계부 (0) | 2024.04.10 |
백준 쵯소값과 최댓값 (0) | 2024.04.08 |
백준 구간 합 구하기 (0) | 2024.04.04 |