Cod sursa(job #3288332)

Utilizator lucky1992Ion Ion lucky1992 Data 21 martie 2025 15:27:28
Problema Arbori de intervale Scor 30
Compilator java Status done
Runda Arhiva educationala Marime 3.01 kb
import java.io.*;
import java.util.StringTokenizer;

public class Main {

  public static class MyScanner implements Closeable {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner(String file) throws FileNotFoundException {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(file)), 1 << 16);
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine(){
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }

    @Override
    public void close() throws IOException {
      br.close();
    }
  }

  private static int[] tree;

  public static void build(int node, int left, int right, MyScanner scanner) {
    if (left == right) {
      tree[node] = scanner.nextInt();
      return;
    }

    int mid = (left + right) >>> 1;
    build(2*node, left, mid, scanner);
    build(2*node+1, mid+1, right, scanner);
    tree[node] = Math.max(tree[2*node], tree[2*node+1]);
  }

  public static void update(int node, int left, int right, int pos, int val) {
    if (left == right) {
      tree[node] = val;
      return;
    }

    int mid = (left + right) >>> 1;
    if (pos <= mid) {
      update(2*node, left , mid, pos, val);
    } else {
      update(2*node+1, mid+1, right, pos, val);
    }
    tree[node] = Math.max(tree[2*node], tree[2*node+1]);
  }

  public static int query(int node, int left, int right, int a, int b) {
    if (a <= left && right <= b) {
      return tree[node];
    }

    int mid = (left + right) >>> 1;
    int leftMax = a <= mid ? query(2 * node, left, mid, a, b) : Integer.MIN_VALUE;
    int rightMax = mid < b ? query(2 * node + 1, mid+1, right, a, b) : Integer.MIN_VALUE;
    return Math.max(leftMax, rightMax);
  }


  public static void main(String[] args) throws IOException {

    try ( MyScanner scanner = new MyScanner("arbint.in");
          PrintWriter writer = new PrintWriter(new FileOutputStream("arbint.out"))) {
      int N = scanner.nextInt();
      int M = scanner.nextInt();

      int closestPowerOfTwo = 1;
      while (closestPowerOfTwo*2 < N - 1) {
        closestPowerOfTwo *= 2;
      }

      tree = new int[3*N+1];
      build(1, 1, N, scanner);

      for (int i = 1; i <= M; i++) {
        int op = scanner.nextInt();
        switch (op) {
          case 0:
            writer.write(query(1, 1, N, scanner.nextInt(), scanner.nextInt()) + "\n");
            break;
          case 1:
            int poz = scanner.nextInt();
            int val = scanner.nextInt();
            update(1, 1, N, poz, val);
            break;
        }
      }
    }
  }
}