标签:
struct NODE
{
int value;
int left,right;
} node[maxn];
int father[MAX];
void BuildTree(int i,int left,int right)
{
node[i].left = left;
node[i].right = right;
node[i].value = 0;
if (left == right)
{
father[left] = i;
return;
}
int mid = (left + right)/2;
BuildTree(i<<1, left, mid);
BuildTree(i<<1|1, mid + 1, right);
}
void UpdataTree(int ri)
{
if (ri == 1)return;
int fi = ri / 2;
int a = node[fi<<1].value;
int b = node[fi<<1|1].value;
node[fi].value = max(a,b);
UpdataTree(ri/2);
}
int Max = -9999;
void Query(int i,int l,int r)
{
if (node[i].left == l && node[i].right == r)
{
Max = max(Max,node[i].value);
return;
}
int ans = 0;
i = i << 1;
if (l <= node[i].right)
{
if (r <= node[i].right)
Query(i, l, r);
else
Query(i, l, node[i].right);
}
i += 1;
if (r >= node[i].left)
{
if (l >= node[i].left)
Query(i, l, r);
else
Query(i, node[i].left, r);
}
}
模板题目HDU1754:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 4000000;
const int MAX = 1000003;
struct NODE
{
int value;
int left,right;
} node[maxn];
int father[MAX];
void BuildTree(int i,int left,int right)
{
node[i].left = left;
node[i].right = right;
node[i].value = 0;
if (left == right)
{
father[left] = i;
return;
}
int mid = (left + right)/2;
BuildTree(i<<1, left, mid);
BuildTree(i<<1|1, mid + 1, right);
}
void UpdataTree(int ri)
{
if (ri == 1)return;
int fi = ri / 2;
int a = node[fi<<1].value;
int b = node[fi<<1|1].value;
node[fi].value = max(a,b);
UpdataTree(ri/2);
}
int Max = -9999;
void Query(int i,int l,int r)
{
if (node[i].left == l && node[i].right == r)
{
Max = max(Max,node[i].value);
return;
}
int ans = 0;
i = i << 1;
if (l <= node[i].right)
{
if (r <= node[i].right)
Query(i, l, r);
else
Query(i, l, node[i].right);
}
i += 1;
if (r >= node[i].left)
{
if (l >= node[i].left)
Query(i, l, r);
else
Query(i, node[i].left, r);
}
}
int main()
{
#ifdef xxz
freopen("in.txt","r",stdin);
#endif // xxz
int n,m,grade;
while(~scanf("%d%d",&n,&m))
{
BuildTree(1,1,n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&grade);
node[father[i]].value = grade;
UpdataTree(father[i]);
}
while(m--)
{
char ch[3];
int a,b;
scanf("%s %d %d",ch,&a,&b);
if (ch[0] == 'Q')
{
Max = 0;
Query(1, a, b);
printf("%d\n",Max);
}
else
{
node[father[a]].value = b;
UpdataTree(father[a]);
}
}
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013445530/article/details/43867005