Pagini recente » Cod sursa (job #2073713) | Cod sursa (job #82987) | Cod sursa (job #2331829) | Cod sursa (job #1453664) | Cod sursa (job #652155)
Cod sursa(job #652155)
#include <fstream>
#include <iostream>
#define MAXN 100002
using namespace std;
typedef unsigned int uint32;
template<typename T, int maxsize>
class BinaryIndexedTree_Max
{
public:
BinaryIndexedTree_Max() :
m_uSize(0)
{}
T Query(uint32 left, uint32 right) const
{
if (left>right)
return 0;
if (right == left)
return values[right];
T maximum = 0;
uint32 pos = right;
while (pos>=left)
{
if (pos-LSB(pos)+1 >= left)
{
if (tree[pos] > maximum)
maximum = tree[pos];
pos = pos-LSB(pos);
}
else
{
if (values[pos] > maximum)
maximum = values[pos];
pos--;
}
}
return maximum;
}
void Update(uint32 pos, const T val)
{
values[pos] = val;
//const T subIntervalMax = Query(pos-LSB(pos)+1, pos-1);
//tree[pos] = max(val, subIntervalMax);
const uint32 originalPos = pos;
while (pos <= m_uSize)
{
tree[pos] = max(Query(pos-LSB(pos)+1, originalPos-1), Query(originalPos, pos));
pos += LSB(pos);
}
}
inline void SetSize(const uint32 size)
{
m_uSize = size;
}
private:
inline uint32 LSB(const uint32 num) const
{
return (num & -num);
}
uint32 m_uSize;
T values[maxsize];
T tree[maxsize];
};
BinaryIndexedTree_Max<uint32, MAXN> bitMaxTree;
int main()
{
uint32 n,m;
fstream fin("arbint.in", fstream::in);
fstream fout("arbint.out", fstream::out);
fin >> n >> m;
bitMaxTree.SetSize(n);
for (uint32 i=1; i<=n; ++i)
{
uint32 x;
fin >> x;
bitMaxTree.Update(i,x);
}
for (uint32 i=0; i<m; ++i)
{
uint32 opt, a, b;
fin >> opt;
switch (opt)
{
case 0:
{
fin >> a >> b;
fout << bitMaxTree.Query(a, b) << "\n";
}; break;
case 1:
{
fin >> a >> b;
bitMaxTree.Update(a, b);
}; break;
}
}
return 0;
}