Pagini recente » IAP #7: Community Ladder | Cod sursa (job #192544) | Cod sursa (job #1683084) | Cod sursa (job #2728053) | Cod sursa (job #1545514)
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public class Main {
public static Scanner input;
public static PrintStream output;
public static void openFiles() throws IOException {
input = new Scanner(new FileReader("arbint.in"));
output = new PrintStream("arbint.out");
}
static public class Node<T extends Number & Comparable<? super T>> {
private T data;
private boolean maybe;
private int exception = -1;
Node() {
data = null;
maybe = false;
}
Node(T x){
data = x;
maybe = true;
}
Node<T> maximum(Node<T> b) {
if ( this.maybe == false )
return b;
if ( b.maybe == false )
return this;
if ( data.compareTo(b.data) > 0 ) {
return this;
}
return b;
}
T fold() {
if ( maybe == true )
return data;
return (T)(new Integer(exception));
}
public String toString() {
return fold().toString();
}
}
static public class SegTree<T extends Number & Comparable<? super T> > {
private Node<T> data;
private int begin, end;
private SegTree<T> leftSon, rightSon;
SegTree(int begin,int end,T data) {
this.begin = begin;
this.end = end;
this.data = new Node<>(data);
if ( begin == end ) {
leftSon = null;
rightSon = null;
return;
}
int middle = (begin + end) / 2;
leftSon = new SegTree<>(begin, middle, data);
rightSon = new SegTree<>(middle+1, end, data);
}
public void update(int pos,Node<T> data) {
if ( pos < begin || pos > end ) {
return;
}
if ( begin == end ) {
this.data = data;
return;
}
leftSon.update(pos,data);
rightSon.update(pos,data);
this.data = leftSon.data.maximum( rightSon.data );
}
public String toString() {
String ans = data.toString();
if ( leftSon != null ) ans += leftSon.toString();
if ( rightSon != null ) ans += rightSon.toString();
return ans;
}
public void print() {
System.out.println( toString() );
}
private Node<T> _query(int begin, int end) {
if ( this.end < begin || this.begin > end ) {
return new Node();
}
if ( begin <= this.begin && this.end <= end ) {
return this.data;
}
return leftSon._query(begin, end) . maximum( rightSon._query(begin, end) );
}
public T query(int begin, int end) {
return _query(begin, end).fold();
}
}
static public enum operation {
UPDATE ,
QUERY ,
NOTHING;
}
public static operation operationFromInt(int x){
switch ( x ) {
case 0:
return operation.QUERY;
case 1:
return operation.UPDATE;
default:
return operation.NOTHING;
}
}
public static void main(String[] args) throws IOException {
openFiles();
int n = input.nextInt();
int m = input.nextInt();
SegTree<Integer> tree = new SegTree<>(1,n,new Integer(0));
for (int i = 0; i < n ; ++i) {
Integer x = new Integer(input.nextInt());
tree.update(i + 1, new Node<Integer>(x) );
}
for (int i = 0; i < m; ++i) {
int type = input.nextInt();
switch ( operationFromInt(type) ) {
case QUERY:
int begin = input.nextInt();
int end = input.nextInt();
Integer ans = tree.query(begin, end);
output.println( ans );
break;
case UPDATE:
int p = input.nextInt();
Integer x = new Integer(input.nextInt());
tree.update(p, new Node<Integer>(x) );
break;
case NOTHING:
break;
}
}
}
}