Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class SegTree {
- static int[] segTree;
- static int build(int l, int r, int i, int[] ar) {
- if(l == r) {
- segTree[i] = ar[l];
- return segTree[i];
- }
- int mid = (l + r) / 2;
- segTree[i] = Math.min(build(l, mid, i * 2, ar), build(mid + 1, r, i * 2 + 1, ar));
- return segTree[i];
- }
- static void update(int l, int r, int i, int index, int v) {
- if(l == r) {
- segTree[i] = v;
- return;
- }
- int mid = (l + r) / 2;
- if(index <= mid) {
- update(l, mid, i * 2, index, v);
- }else {
- update(mid + 1, r, i * 2 + 1, index, v);
- }
- segTree[i] = Math.min(segTree[i * 2], segTree[i * 2 + 1]);
- }
- static int query(int l, int r, int ql, int qr, int i) {
- if(l > qr || r < ql) {
- return Integer.MAX_VALUE;
- }
- if(ql <= l && qr >= r) {
- return segTree[i];
- }
- int mid = (l + r) / 2;
- return Math.min(query(l, mid, ql, qr, i * 2), query(mid + 1, r, ql, qr, i * 2 + 1));
- }
- public static void main(String[] args) throws IOException{
- readLine();
- int n = nextInt(), m = nextInt();
- int[] ar = new int[n];
- int size = 0;
- int counter = n;
- while(counter > 0) {
- size += n;
- counter >>= 1;
- }
- segTree = new int[size + 1];
- for(int i = 0; i < n; i ++) {
- readLine();
- ar[i] = nextInt();
- }
- build(0, n - 1, 1, ar);
- for(int i = 0; i < m; i ++) {
- readLine();
- char c = nextChar();
- switch(c) {
- case 'M':
- update(0, n - 1, 1, nextInt(), nextInt());
- break;
- case 'Q':
- System.out.println(query(0, n - 1, nextInt(), nextInt(), 1));
- }
- }
- }
- static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- static StringTokenizer st;
- static void readLine() throws IOException{
- st = new StringTokenizer(in.readLine());
- }
- static int nextInt() {
- return Integer.parseInt(st.nextToken());
- }
- static char nextChar() {
- return st.nextToken().charAt(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment