Pagini recente » Cod sursa (job #1878702) | Cod sursa (job #417743) | Cod sursa (job #1487250) | Rating Alexandru Calin (calinalexandru) | Cod sursa (job #1012384)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct elem {
int value;
int leftIndex;
int rightIndex;
struct elem *left;
struct elem *right;
} ELEM;
ELEM *root;
int n,m;
int values[100002];
int position, value;
int maxx(int a, int b){
if (a>b)
return a;
else
return b;
}
ELEM *setupIT(int left, int right) {
if (left <= right){
ELEM *p = (ELEM*) malloc(sizeof(ELEM));
p->leftIndex = left;
p->rightIndex = right;
if (left == right){
p->value = values[left];
} else {
p->left = setupIT(left, (left+right)/2);
p->right = setupIT((left+right)/2 + 1, right);
p->value = maxx(p->left->value, p->right->value);
}
return p;
} else {
return NULL;
}
}
int intersects(int left1, int right1, int left2, int right2){
if (right1 < left2 || right2 < left1){
return 0;
}
return 1;
}
int queryIT(ELEM *p, int left, int right){
if (left <= p->leftIndex && p->rightIndex <= right){
return p->value;
}
else {
int valueLeft = -999999, valueRight = -9999999;
if (p->left != NULL && intersects(p->left->leftIndex, p->left->rightIndex, left, right)){
valueLeft = queryIT(p->left, left, right);
}
if (p->right != NULL && intersects(p->right->leftIndex, p->right->rightIndex, left, right)){
valueRight = queryIT(p->right, left, right);
}
return maxx(valueLeft, valueRight);
}
}
void updateIT(ELEM *p) {
if (p->leftIndex == p->rightIndex){
p->value = value;
}
else {
if (p->left != NULL && p->left->rightIndex >= position){
updateIT(p->left);
} else if (p->right != NULL){
updateIT(p->right);
}
p->value = maxx(p->left->value, p->right->value);
}
}
int main()
{
freopen("arbint.in", "r", stdin);
freopen("arbint.out", "w", stdout);
scanf("%d %d", &n, &m);
for (int i=1; i<=n; i++){
scanf("%d", &values[i]);
}
//////////solve//////
//setup IT
root = (ELEM*) malloc(sizeof(ELEM));
root->leftIndex = 1;
root->rightIndex = n;
root->left = NULL;
root->right = NULL;
if (n > 1){
root->left = setupIT(1, n/2);
root->right = setupIT(n/2+1, n);
root->value = maxx(root->left->value, root->right->value);
} else {
root->value = values[0];
}
//repl -> Read Evaluate Print Loop
int op, a,b;
for (int i=0; i<m; i++){
scanf("%d %d %d", &op, &a, &b);
if (op == 0){
int maxValue = queryIT(root, a, b);
printf("%d\n", maxValue);
}
else if (op == 1){
position = a;
value = b;
updateIT(root);
}
}
return 0;
}