티스토리 뷰
https://www.acmicpc.net/problem/11505
11505번: 구간 곱 구하기
첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)과 M(1 ≤ M ≤ 10,000), K(1 ≤ K ≤ 10,000) 가 주어진다. M은 수의 변경이 일어나는 횟수이고, K는 구간의 곱을 구하는 횟수이다. 그리고 둘째 줄부터 N+1번째 줄
www.acmicpc.net
//https://www.acmicpc.net/problem/11505
// 구간 곱 구하기
// 비재귀 세그먼트 트리 구현
class NonRecursiveSegmentTree {
constructor(inputArray) {
// build
const inputArrayLength = inputArray.length;
this.n = 2 ** Math.ceil(Math.log2(inputArrayLength));
this.tree = new Array(2 * this.n).fill(BigInt(1));
for (let i = 0; i < inputArrayLength; i++) {
this.tree[this.n + i] = BigInt(inputArray[i]);
}
for (let i = this.n - 1; i > 0; --i) {
this.tree[i] = (this.tree[i << 1] * this.tree[(i << 1) | 1]) % BigInt(1000000007);
}
}
update(target, value) {
target += this.n;
this.tree[target] = BigInt(value);
while (target > 1) {
this.tree[target >> 1] = (this.tree[target] * this.tree[target ^ 1]) % BigInt(1000000007);
target >>= 1;
}
}
// l <= x < r 구간에 대한 쿼리
query(l, r) {
let res = BigInt(1);
l += this.n;
r += this.n;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = (res * this.tree[l++]) % BigInt(1000000007);
if (r & 1) res = (res * this.tree[--r]) % BigInt(1000000007);
}
return res;
}
}
const input = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n');
const N = input.shift().split(' ')[0];
const nums = input.splice(0, N);
const segmentTree = new NonRecursiveSegmentTree(nums);
const answer = [];
input.forEach((cmd) => {
const [c, a, b] = cmd.split(' ');
if (c == '1') segmentTree.update(Number(a) - 1, BigInt(b));
else answer.push(segmentTree.query(Number(a) - 1, Number(b)));
});
console.log(answer.join('\n'));728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
| Node.js) 백준 14438번: 수열과 쿼리 17 (1) | 2024.02.26 |
|---|---|
| Node.js) 백준 1275번: 커피숍2 (1) | 2024.02.26 |
| Node.js) 백준 2357번: 최솟값과 최댓값 (1) | 2024.02.26 |
| Node.js) 백준 1918번: 후위 표기식 (0) | 2024.02.25 |
| Node.js) 백준 1655번: 가운데를 말해요 (0) | 2024.02.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 그래프
- node.js
- create databases;
- 투포인터
- BFS
- 동적프로그래밍
- MOD
- 다이나밍프로그래밍
- 서버점검
- 은둔청년체험
- 투포인터 연습
- 개발자면접
- 면접비
- 로드나인
- 다이나믹프로그래밍
- create db
- 최소공통조상
- DB 생성
- 면접질문
- 롱베케이션
- KMP
- 서버개발
- MySQL
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함
