您的位置:首页 > 其它

hdu1754 单点更新

2015-08-04 15:25 253 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1754

中文题 不解释题意

线段树 单点更新,维护区间的最大值

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <math.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
#define sf(a) scanf("%d",&a)
#define sff(a,b) scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define LL long long
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
const int INF = 0x7FFFFFFF;
const int MAXN = 200100;
const double PI = acos(-1.0);
const double esp = 1e-10;
using namespace std;
struct node
{
int l,r,Max;
}Tree[MAXN<<2];
void build_tree(int l,int r,int root)
{
Tree[root].l = l;
Tree[root].r = r;
if(Tree[root].l == Tree[root].r)
{
sf(Tree[root].Max);
return ;
}
int mid = (l + r)>>1;
build_tree(lson);
build_tree(rson);
Tree[root].Max = max(Tree[root << 1].Max , Tree[root << 1|1].Max);
}
void updata(int l,int r,int root,int k)
{
if(Tree[root].l == l && Tree[root].r == r)
{
Tree[root].Max = k;
return ;
}
int mid = (Tree[root].l + Tree[root].r)>>1;
if(r <= mid) updata(l,r,root<<1,k);
else if(mid < l) updata(l,r,root<<1|1,k);
else
{
updata(lson,k);
updata(rson,k);
}
Tree[root].Max = max(Tree[root<<1].Max, Tree[root<<1|1].Max);
}
int Query(int l,int r,int root)
{
if(Tree[root].l == l && Tree[root].r == r)
{
return Tree[root].Max;
}
int ans = -1;
int mid = (Tree[root].l + Tree[root].r) >> 1;
if(r <= mid) ans = max(ans,Query(l,r,root<<1));
else if(mid < l) ans = max(ans,Query(l,r,root<<1|1));
else
{
ans = max(ans,Query(l,mid,root<<1));
ans = max(ans,Query(mid+1,r,root<<1|1));
}
return ans;
}
int main()
{
int n,m,x,y;
char c;
while(~sff(n,m))
{
build_tree(1,n,1);
while(m--)
{
cin >> c;
sff(x,y);
if(c=='Q')
{
printf("%d\n",Query(x,y,1));
}
else if(c=='U')
{
updata(x,x,1,y);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: