Pagini recente » Cod sursa (job #2756347) | Cod sursa (job #2187115) | Cod sursa (job #843399) | Cod sursa (job #878155) | Cod sursa (job #2231648)
#include <bits/stdc++.h>
#define MAX 400000
#define oo -10000000000
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
int n, k, v[MAX], heap[MAX], posInTree[MAX];
void buildHeap(int st, int dr, int idx)
{
if(st==dr)
{
posInTree[st] = idx;
heap[idx]=v[st];
return;
}
int mij=(st+dr)/2;
int twice = (idx<<1);
buildHeap(st, mij, twice);
buildHeap(mij+1, dr, twice+1);
heap[idx] = max(heap[twice], heap[twice+1]);
}
int getMax(int x, int y, int st, int dr, int idx)
{
if(st<=dr)
{
int mij=(st+dr)/2;
int twice=(idx<<1);
if(x<=st && y>=dr)
return heap[idx];
else if(y<st || x>dr)
return oo;
else return max(getMax(x, y, st, mij, twice), getMax(x, y, mij+1, dr, twice+1));
}
}
void updateTree(int pos, int val)
{
int idx = posInTree[pos];
heap[idx] = val;
while(idx>1)
{
int newIdx = (idx>>1);
if(idx&1)
heap[newIdx] = max(heap[idx], heap[idx-1]);
else heap[newIdx] = max(heap[idx], heap[idx+1]);
idx=newIdx;
}
}
int main()
{
f>>n>>k;
for(int i=1; i<=n; i++)
f>>v[i];
buildHeap(1, n, 1);
/*for(int i=1; i<=30; i++)
cout<<heap[i]<<" ";
cout<<'\n';*/
/*for(int i=1; i<=n; i++)
cout<<posInTree[i]<<" ";
cout<<'\n';*/
for(int i=1; i<=k; i++)
{
int op, x, y;
f>>op>>x>>y;
if(op==0)
g<<getMax(x, y, 1, n, 1)<<'\n';
else
{
updateTree(x, y);
/*for(int i=1; i<=30; i++)
cout<<heap[i]<<" ";
cout<<'\n';*/
}
}
}