题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754点击打开链接
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
5 6 5 9HintHuge input,the C function scanf() will work better than cin
单点更新区间查询 储存最大值的信息即可
#include
using namespace std;
#define maxn 200000+5
struct xjy
{
int num;
int maxx;
int left;
int right;
int target;
int lazy;
};
xjy tree[maxn<<2];
int a[maxn];
int ans=0;
void build(int i,int left ,int right)
{
if(left==right)
{
tree[i].num=a[left];
tree[i].maxx=tree[i].num;
tree[i].left=left;
tree[i].right=right;
return ;
}
int mid=(left+right)>>1;
build(i<<1,left,mid);
build (i<<1|1,mid+1,right);
tree[i].maxx=max(tree[i<<1].maxx,tree[i<<1|1].maxx);
tree[i].left=left;
tree[i].right=right;
}
void queryregion(int i,int left,int right)
{
if(tree[i].lazy)
{
tree[i<<1].target+=tree[i].target;
tree[i<<1|1].target+=tree[i].target;
tree[i].lazy=0;
tree[i<<1].lazy=tree[i<<1|1].lazy=1;
tree[i<<1].maxx+=tree[i].target;
tree[i<<1|1].maxx+=tree[i].target;
tree[i].target=0;
}
if(left==tree[i].left&&right==tree[i].right)
{
ans=max(ans,tree[i].maxx);
return;
}
int mid=(tree[i].left+tree[i].right)>>1;
if(right<=mid)
queryregion(i<<1,left,right);
else if(left>mid)
queryregion(i<<1|1,left,right);
else
{
queryregion(i<<1,left,mid);
queryregion(i<<1|1,mid+1,right);
}
tree[i].maxx=max(tree[i<<1].maxx,tree[i<<1|1].maxx);
}
void updatepoint(int i,int left,int right,int val)
{
if(left==tree[i].left&&right==tree[i].right)
{
tree[i].num=val;
tree[i].maxx=val;
return ;
}
if(tree[i].lazy)
{
tree[i<<1].target+=tree[i].target;
tree[i<<1|1].target+=tree[i].target;
tree[i].lazy=0;
tree[i<<1].lazy=tree[i<<1|1].lazy=1;
tree[i<<1].maxx+=tree[i].target;
tree[i<<1|1].maxx+=tree[i].target;
tree[i].target=0;
}
int mid=(tree[i].left+tree[i].right)>>1;
if(right<=mid)
updatepoint(i<<1,left,right,val);
else if(left>mid)
updatepoint(i<<1|1,left,right,val);
else
{
updatepoint(i<<1,left,mid,val);
updatepoint(i<<1|1,mid+1,right,val);
}
tree[i].maxx=max(tree[i<<1].maxx,tree[i<<1|1].maxx);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i> s;
if(s[0]=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
if(l>r)
swap(l,r);
ans=0;
queryregion(1,l,r);
printf("%d\n",ans);
}
else if(s[0]=='U')
{
int i,j;
scanf("%d%d",&i,&j);
updatepoint(1,i,i,j);
}
}
}
}