Cod sursa(job #1259291)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 9 noiembrie 2014 21:41:57
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int nmax=100000;
int arint[4*nmax+1];

void update(int nod, int st, int dr, int poz, int val)
{
    if(st==dr)arint[nod]=val;
    else
    {
        int mid=(st+dr)/2;
        if(poz<=mid)update(2*nod, st, mid, poz, val);
        else update(2*nod+1, mid+1, dr, poz, val);
        arint[nod]=max(arint[2*nod], arint[2*nod+1]);
    }
}

int ans;

void querry(int nod, int st, int dr, int x, int y)
{
    int mid=(st+dr)/2;
    if(st>=x && y>=dr)
	{
        ans=max(ans, arint[nod]);
        return;
    }
    mid=(st+dr)/2;
    if(x<=mid)querry(2*nod, st, mid, x, y);
    if(y>mid)querry(2*nod+1, mid+1, dr, x, y);
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d", &x);
		update(1, 1, n, i, x);
	}
	for(int i=0;i<m;i++)
	{
		bool type;
		int x, y;
		scanf("%d%d%d", &type, &x, &y);
		if(!type)
		{
			ans=0;
			querry(1, 1, n, x, y);
			printf("%d\n", ans);
		}
		else update(1, 1, n, x, y);
	}
    return 0;
}