Pagini recente » Cod sursa (job #721248) | Cod sursa (job #919680) | Cod sursa (job #3348252) | Cod sursa (job #2015315) | Cod sursa (job #2683371)
import java.io.*;
import java.util.Scanner;
public class Main {
static Node root;
private static int n;
private static int m;
public static void main(String []args) {
File inputFile = new File("arbint.in");
File outputFile = new File("arbint.out");
try {
FileInputStream inputStream = new FileInputStream(inputFile);
Scanner scanner = new Scanner(inputStream);
FileOutputStream outputStream = new FileOutputStream(outputFile);
PrintWriter writer = new PrintWriter(outputStream);
n = scanner.nextInt(); // the number of elements
m = scanner.nextInt(); // the number of commands
int left = 1;
int right = n;
root = new Node(1, n);
for (int i = 0; i < n; i++){
CreateNode(root, left,right);
}
for (int i = 1; i <= n; i++){
Update(root, i, scanner.nextInt());
}
int command;
int firstNumber;
int secondNumber;
for (int i = 0; i < m; i++) {
command = scanner.nextInt();
firstNumber = scanner.nextInt();
secondNumber = scanner.nextInt();
if (command == 1)
Update(root, firstNumber, secondNumber);
if (command == 0)
writer.println(OutputMax(root, firstNumber, secondNumber));
}
writer.close();
outputStream.close();
} catch(IOException e) {
}
}
private static int OutputMax(Node root, int firstNumber, int secondNumber) {
if (root.leftBorder == firstNumber && root.rightBorder == secondNumber)
return root.maxValue;
if (root.children == null)
return root.maxValue;
if (secondNumber <= root.children[0].rightBorder)
return OutputMax(root.children[0], firstNumber, secondNumber);
if (firstNumber >= root.children[1].leftBorder)
return OutputMax(root.children[1], firstNumber, secondNumber);
int leftKid = OutputMax(root.children[0], firstNumber, root.children[0].rightBorder);
int rightKid = OutputMax(root.children[1], root.children[1].leftBorder, secondNumber);
return Math.max(leftKid, rightKid);
}
private static void Update(Node root, int firstNumber, int secondNumber) {
if (root.children == null) {
root.maxValue = secondNumber;
return;
}
if (root.children[0].rightBorder >= firstNumber){
Update(root.children[0], firstNumber, secondNumber);
root.maxValue = Math.max(root.children[0].maxValue, root.children[1].maxValue);
}
else{
Update(root.children[1], firstNumber, secondNumber);
root.maxValue = Math.max(root.children[0].maxValue, root.children[1].maxValue);
}
}
private static void CreateNode(Node root, int left, int right) {
int middle = (left + right) / 2;
if (middle == left) {
root.children[0] = new Node(left);
if (middle + 1 == right)
root.children[1] = new Node(right);
}
else{
root.children[0] = new Node(left, middle);
CreateNode(root.children[0], left, middle);
if (middle + 1 == right)
root.children[1] = new Node(right);
else {
root.children[1] = new Node(middle + 1, right);
CreateNode(root.children[1], middle + 1, right);
}
}
}
static class Node{
public Node [] children;
public int maxValue;
public int leftBorder;
public int rightBorder;
public Node(int l, int r) {
maxValue = 0;
leftBorder = l;
rightBorder = r;
children = new Node[2];
}
public Node(int l){
// Leaf
maxValue = 0;
children = null;
leftBorder = rightBorder = l;
}
}
}